1.

Solve : bat file IF statement need help?

Answer»

Hi,
I use a DOS program which CONVERT a imege format. And I tried to create a bat file which need to call for instance xxx.exe -parameter . The parameters is the imege resolution and it is a part of the image name. If the image begin whit 1_ resolution is 80X60, 2_ resolution is 120x80 and so on. Here is what I try so fat(doesn't work):

For %%F in (*.*) do goto next
:next
IF %%F == 1_*.* xxx.exe %%F 60x80
IF %%F == 2_*.* xxx.exe %%F 120x80
......

I desperately need help. Thanks to any one who can help me..




You do not say what OS you are using. This should work in Windows 2000 and later. Notice ! instead of % for variables inside loop. Also notice line starting "setlocal".

To get M chars of variable starting at position N (1st position is 0)

%variable:~N,M% [or !variable:~N,M! inside a loop, if you have enabled delayed expansion as SHOWN below]

So...

if %string% is HELLO WORLD
then %string:~1,3% is ELL

Quote


@echo off
setlocal enabledelayedexpansion
for /f "delims==" %%F in ('dir /b') do (
set filename=%%F
if "!filename:~0,2!"=="1_" set res=60x80
if "!filename:~0,2!"=="2_" set res=120x80
REM and so on all for your resolutions...

REM now call the program
xxx.exe !filename! !res!

)

Thanks that works great.
But what about if I need to check for string in any position in the file name.
Example: Find if "abc" is a part of the all file names in the current directory. OS Windows XP
can I do something like:

setlocal enabledelayedexpansion
FOR /f %%F (*.*) do (
set filename=%%F
IF filename condition "abc" command

What will be the condition if it
Thanks..You can use && and || operators

[Operation] && [do this if operation was successful]

[Operation] || [do this if command was not successful]

Do something if abc IS found...

use >nul to stop echo to screen

Quote

setlocal enabledelayedexpansion
FOR /f %%F (*.*) do (
set filename=%%F
echo !filename! | findstr "abc">nul && command


or if you have more than 1 command to execute...

This is the general way to execute more than 1 command after a TEST

Don't forget to count parentheses (( ))

Quote

setlocal enabledelayedexpansion
FOR /f %%F (*.*) do (
set filename=%%F
echo !filename! | findstr "abc">nul && (
command1
command2
ETC
)
)


Do something if abc IS NOT found...

Quote

setlocal enabledelayedexpansion
FOR /f %%F (*.*) do (
set filename=%%F
echo !filename! | findstr "abc">nul || command


or if you have more than 1 command to execute...

Quote

setlocal enabledelayedexpansion
FOR /f %%F (*.*) do (
set filename=%%F
echo !filename! | findstr "abc">nul || (
command1
command2
etc
)
)

Of course you can do this instead of having the sought string in the test itself

Quote

setlocal enabledelayedexpansion
set trigger=abc
FOR /f %%F (*.*) do (
set filename=%%F
echo !filename! | findstr "%trigger%">nul && command



Discussion

No Comment Found