<p>I have a set of base filenames, for each name 'f' there are exactly two files, 'f.in' and 'f.out'.  I want to write a batch file (in Windows XP) which goes through all the filenames, for each one it should:</p>

<ul>
<li>Display the base name 'f'</li>
<li>Perform an action on 'f.in'</li>
<li>Perform another action on 'f.out'</li>
</ul>

<p>I don't have any way to list the set of base filenames, other than to search for *.in (or *.out) for example.</p>

[{'answer_id': 39636, 'author': 'Nathan Fritz', 'author_id': 4142, 'author_profile': 'https://Stackoverflow.com/users/4142', 'pm_score': 3, 'selected': False, 'text': "<p>Easiest way, as I see it, is to use a for loop that calls a second batch file for processing, passing that second file the base name.</p>\n\n<p>According to the for /? help, basename can be extracted using the nifty ~n option. So, the base script would read:</p>\n\n<pre><code>for %%f in (*.in) do call process.cmd %%~nf\n</code></pre>\n\n<p>Then, in process.cmd, assume that %0 contains the base name and act accordingly. For example:</p>\n\n<pre><code>echo The file is %0\ncopy %0.in %0.out\nren %0.out monkeys_are_cool.txt\n</code></pre>\n\n<p>There might be a better  way to do this in one script, but I've always been a bit hazy on how to pull of multiple commands in a single for loop in a batch file.</p>\n\n<p>EDIT: That's fantastic! I had somehow missed the page in the docs that showed that you could do multi-line blocks in a FOR loop. I am going to go have to go back and rewrite some batch files now...</p>\n"}, {'answer_id': 39652, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>There is a tool usually used in MS Servers (as far as I can remember) called <a href="http://www.ss64.com/nt/forfiles.html" rel="nofollow noreferrer">forfiles</a>:</p>\n\n<p>The link above contains help as well as a link to the microsoft download page.</p>\n'}, {'answer_id': 39656, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 7, 'selected': False, 'text': '<p>You can use this line to print the contents of your desktop:</p>\n\n<pre><code>FOR %%I in (C:\\windows\\desktop\\*.*) DO echo %%I \n</code></pre>\n\n<p>Once you have the <code>%%I</code> variable it\'s easy to perform a command on it (just replace the word echo with your program)</p>\n\n<p>In addition, substitution of FOR variable references has been enhanced\nYou can now use the following optional syntax:</p>\n\n<pre><code>%~I         - expands %I removing any surrounding quotes (")\n%~fI        - expands %I to a fully qualified path name\n%~dI        - expands %I to a drive letter only\n%~pI        - expands %I to a path only (directory with \\)\n%~nI        - expands %I to a file name only\n%~xI        - expands %I to a file extension only\n%~sI        - expanded path contains short names only\n%~aI        - expands %I to file attributes of file\n%~tI        - expands %I to date/time of file\n%~zI        - expands %I to size of file\n%~$PATH:I   - searches the directories listed in the PATH\n               environment variable and expands %I to the\n               fully qualified name of the first one found.\n               If the environment variable name is not\n               defined or the file is not found by the\n               search, then this modifier expands to the\n               empty string\n</code></pre>\n\n<p><a href="https://ss64.com/nt/syntax-args.html" rel="noreferrer">https://ss64.com/nt/syntax-args.html</a></p>\n\n<p>In the above examples <code>%I</code> and PATH can be replaced by other valid\nvalues.  The <code>%~</code> syntax is terminated by a valid FOR variable name.\nPicking upper case variable names like <code>%I</code> makes it more readable and\navoids confusion with the modifiers, which are not case sensitive.</p>\n\n<p>You can get the full documentation by typing <code>FOR /?</code></p>\n'}, {'answer_id': 39664, 'author': 'Jim Buck', 'author_id': 2666, 'author_profile': 'https://Stackoverflow.com/users/2666', 'pm_score': 9, 'selected': True, 'text': '<p>Assuming you have two programs that process the two files, process_in.exe and process_out.exe:</p>\n\n<pre><code>for %%f in (*.in) do (\n    echo %%~nf\n    process_in "%%~nf.in"\n    process_out "%%~nf.out"\n)\n</code></pre>\n\n<p>%%~nf is a substitution modifier, that expands %f to a file name only.\nSee other modifiers in <a href="https://technet.microsoft.com/en-us/library/bb490909.aspx" rel="noreferrer">https://technet.microsoft.com/en-us/library/bb490909.aspx</a> (midway down the page) or just in the next answer.</p>\n'}, {'answer_id': 39750, 'author': 'Martin', 'author_id': 770, 'author_profile': 'https://Stackoverflow.com/users/770', 'pm_score': 2, 'selected': False, 'text': '<p>Expanding on <a href="https://stackoverflow.com/users/4142/nathan-fritz">Nathans</a> post. The following will do the job lot in one batch file.</p>\n\n<pre><code>@echo off\n\nif %1.==Sub. goto %2\n\nfor %%f in (*.in) do call %0 Sub action %%~nf\ngoto end\n\n:action\necho The file is %3\ncopy %3.in %3.out\nren %3.out monkeys_are_cool.txt\n\n:end\n</code></pre>\n'}, {'answer_id': 35572977, 'author': 'Sandro Rosa', 'author_id': 3971553, 'author_profile': 'https://Stackoverflow.com/users/3971553', 'pm_score': 2, 'selected': False, 'text': '<p>The code below filters filenames starting with given substring. It could be changed to fit different needs by working on subfname substring extraction and IF statement:</p>\n\n<pre><code>echo off\nrem filter all files not starting with the prefix \'dat\'\nsetlocal enabledelayedexpansion\nFOR /R your-folder-fullpath %%F IN (*.*) DO (\nset fname=%%~nF\nset subfname=!fname:~0,3!\nIF NOT "!subfname!" == "dat" echo "%%F"\n)\npause\n</code></pre>\n'}, {'answer_id': 46755785, 'author': 'Z. Mickaels', 'author_id': 8777470, 'author_profile': 'https://Stackoverflow.com/users/8777470', 'pm_score': 0, 'selected': False, 'text': '<p>Echoing f.in and f.out will seperate the concept of what to loop and what not to loop when used in a for /f loop.</p>\n\n<pre><code>::Get the files seperated\necho f.in&gt;files_to_pass_through.txt\necho f.out&gt;&gt;files_to_pass_through.txt\n\nfor /F %%a in (files_to_pass_through.txt) do (\nfor /R %%b in (*.*) do (\nif "%%a" NEQ "%%b" (\necho %%b&gt;&gt;dont_pass_through_these.txt\n)\n)\n)\n::I\'m assuming the base name is the whole string "f".\n::If I\'m right then all the files begin with "f".\n::So all you have to do is display "f". right?\n::But that would be too easy.\n::Let\'s do this the right way.\nfor /f %%C in (dont_pass_through_these.txt)\n::displays the filename and not the extention\necho %~nC\n)\n</code></pre>\n\n<p>Although you didn\'t ask, a good way to pass commands into f.in and f.out would be to...</p>\n\n<pre><code>for /F %%D "tokens=*" in (dont_pass_through_these.txt) do (\nfor /F %%E in (%%D) do (\nstart /wait %%E\n)\n)\n</code></pre>\n\n<p>A link to all the Windows XP commands:<a href="https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr=true" rel="nofollow noreferrer">link</a></p>\n\n<p>I apologize if I did not answer this correctly. The question was very hard for me to read.</p>\n'}]
['https://Stackoverflow.com/questions/39615', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3974/']