1.

Solve : Conditional statements in DOS batch files?

Answer»

Hello,

Microsoft Windows XP [Version 5.1.2600].

How does one write an IF statement with several conditions? Also, can anyone explain the use and syntax of the parenthesis the IF statement? I have read the help files on the IF statement and still do not understand enough to write more complicated conditional logic than the one line examples.

Tim_C

what is it your trying to do???

some examples would help.

For 3 conditions you'll need 2 if statements, for 4 conditions you'll still want 2 statements. For 5 conditions you require 3 statements and so on...

I want to run a program but only if all 9 files exists. The program will run without error as long as one of more of the files is available. So I want to write a conditional that looks something like the following.

If EXIST 2007_033.h20v08.hdf
AND EXIST 2007_033.h20v09.hdf
AND EXIST 2007_033.h20v10.hdf
AND EXIST 2007_033.h21v08.hdf
AND EXIST 2007_033.h21v09.hdf
AND EXIST 2007_033.h21v10.hdf
AND EXIST 2007_033.h22v08.hdf
AND EXIST 2007_033.h22v09.hdf
AND EXIST 2007_033.h22v10.hdf

then

dir /b /s 2007_033.*.hdf > tmp.log
mrtmosaic tmp.log

else echo incomplete data >> 2007_033.txt
Quote

I want to run a program but only if all 9 files exists. The program will run without error as long as one of more of the files is available.

Seems contradictory. This little snippet will test for all files being available. It's a simple change to have one or more available.

Code: [Select]@echo off
set count=0
for %%V in (2007_033.h20v08.hdf 2007_033.h20v09.hdf 2007_033.h20v10.hdf 2007_033.h21v08.hdf 2007_033.h21v09.hdf 2007_033.h21v10.hdf 2007_033.h22v08.hdf 2007_033.h22v09.hdf 2007_033.h22v10.hdf) do (
if exist %%v call set /a count=%%count%%+1
)
if count neq 9 goto :eof
.
. put your code here
.
.
.

Quote
For 3 conditions you'll need 2 if statements, for 4 conditions you'll still want 2 statements. For 5 conditions you require 3 statements and so on...

Perhaps someone could explain this statement.


Quote from: Sidewinder on June 22, 2008, 02:36:05 PM

Quote
For 3 conditions you'll need 2 if statements, for 4 conditions you'll still want 2 statements. For 5 conditions you require 3 statements and so on...

Perhaps someone could explain this statement.




I'd be surprised.
Sidewinder,

Thanks for the For-loop. I hope to use it this WEEK sometime.

Perhaps I should explain my purpose even more since my explanation above seemed contradictory or at least confusing.

The nine hdf files are MODIS satellite images of different but adjacent portions of East Africa from the same 8-day period. The mrtmosaic program will mosaic these adjacent images into one image. mrtmosaic, however, is designed for researchers having different areas of interest. So mrtmosaic will mosaic as many or as few images as are supplied to it, whether there are 9, 2, or 50 images. You get the picture. Due to atmospheric conditions, not all images are available for each time period. Since my team can only use a mosaiked image if it covers the ENTIRE area of interest, and I have thousands of time periods to process, I only want to mosaic those time periods for which all 9 images are available and skip the time periods with incomplete imagery.

Thank you again.

By the way. Your answer incorporated a For-loop which is another programming design I wanted to use in other applications. So your one answer has HELPED me twice.



Quote from: Dias de verano on June 22, 2008, 03:35:59 PM
Quote from: Sidewinder on June 22, 2008, 02:36:05 PM

Quote
For 3 conditions you'll need 2 if statements, for 4 conditions you'll still want 2 statements. For 5 conditions you require 3 statements and so on...

Perhaps someone could explain this statement.




I'd be surprised.


Surprised??? whys that??

OK the sentence and grammar are very poor. (had been a long day) I was trying to explain that if you have 2 conditions (yes/no for example) you'd need 1 if statement. If you had 3 conditions, you'll need 2 if statements and so on.

I was under the impression that this was what the OP was after.

I misunderstood.


Discussion

No Comment Found