Computer Hope

Microsoft => Microsoft DOS => Topic started by: xeifrank on July 30, 2009, 05:39:47 PM

Title: For loop to read multiple files
Post by: xeifrank on July 30, 2009, 05:39:47 PM
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.
Title: Re: For loop to read multiple files
Post by: Salmon Trout on July 31, 2009, 12:22:47 AM
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

Title: Re: For loop to read multiple files
Post by: Salmon Trout on July 31, 2009, 12:57:32 AM
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