1.

Solve : For loop to read multiple files?

Answer»

I have a WINDOWS console program that I created that I RUN using the following format.

>myprogram -f filename

where filename is a file that CONTAINS input data to my program. My filenames have the following naming convention.

1000.txt
1001.txt
1002.txt
.
.
.
9999.txt

I would like to build a batch file that runs my program with each and every input file.

ie
>myprogram -f 1000.txt
>myprogram -f 1001.txt
>myprogram -f 1002.txt
.
.
.
>myprogram -f 9999.txt

Any help would be greatly appreciated.Try this

Code: [SELECT]@echo off
for /f "delims==" %%A in ( 'dir /b /on *.txt' ) do (
start /WAIT "" "myprogram" -f %%A
)

This assumes

1. that the only .txt files in the folder are the ones that contain input data
2. You want to run your program with every single one
3. You want to run them in ascending order of filename.
4. You want the program to finish each file before going on to the next one

Code: [Select]@echo off
echo %date% %time% Starting processing
for /f "delims==" %%A in ( 'dir /b /on *.txt' ) do (
echo %date% %time% Processing file: %%A
start /WAIT "" "myprogram" -f %%A
echo %date% %time% Finished file: %%A
)
echo %date% %time% Finished batch
pause

I have put in some echo statements and a pause which might make diagnostics EASIER



Discussion

No Comment Found