How to loop through files matching wildcard in batch file

batch-filecommand linewindows-xp

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:

  • Display the base name 'f'
  • Perform an action on 'f.in'
  • Perform another action on 'f.out'

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

Best Answer

Assuming you have two programs that process the two files, process_in.exe and process_out.exe:

for %%f in (*.in) do (
    echo %%~nf
    process_in "%%~nf.in"
    process_out "%%~nf.out"
)

%%~nf is a substitution modifier, that expands %f to a file name only. See other modifiers in https://technet.microsoft.com/en-us/library/bb490909.aspx (midway down the page) or just in the next answer.