2

I'm working with a command-line program that does image-processing, and I need to run the same command on an entire folder of images. I have heard that I can run loops in the command prompt, but I have seen all sorts of different examples online and can't figure out the syntax. The images in the folder are labled "single0.pgm, single1.pgm, single2.pgm,..." all the way to single39.pgm. The command I need to run is:

DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i single0.pgm -o single0.ppm

And I need to do that for every picture. In C it is just a simple for loop like

for (int j = 0; j<40; j++) {
    DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i singlej.pgm -o singlej.ppm
}

How do I do that in the command prompt?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
singmotor
  • 3,930
  • 12
  • 45
  • 79
  • 1
    Possible duplicate of *[Iterate all files in a directory using a 'for' loop](http://stackoverflow.com/questions/138497/iterate-all-files-in-a-directory-using-a-for-loop)*. – Peter Mortensen Oct 23 '16 at 11:05

3 Answers3

7

I figured it out! For anyone curious, the loop is:

for %a in (0 1 2 3 4 5 6 7 8 9 10 11 1
2 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39) do DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i single%a..pgm -o si
ngle%a.ppm
singmotor
  • 3,930
  • 12
  • 45
  • 79
2
for /l %a in (0,1,39) do DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i single%a..pgm -o single%a.ppm

is less prone to typos. This command runs happily directly from the prompt, but if it's a line within a batch file, you'd need to double the % for each instance of the metavariable %a (i.e. %a becomes %%a within a batch file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Magoo
  • 77,302
  • 8
  • 62
  • 84
1

Here is a good short explanation of for loops, with examples. http://www.robvanderwoude.com/for.php Take a note of the 2 important key points:

  1. The code you write in command prompt will not work when you put it in batch files. The syntax of loops is different in those 2 cases due to access to variables as %%A instead of %A
  2. Specifically for your problem it is easier do do dir *.pgm and than run a for loop over all the files. Thus your program will work for any amount of files and not just hard coded 40. This can be done as explained here: Iterate all files in a directory using a 'for' loop
Community
  • 1
  • 1
DanielHsH
  • 4,287
  • 3
  • 30
  • 36