Cynosure.X International LLC

:

Add Comment | Related Links | TrackBack
Related Content

Multi-Line IF Statements in Batch File

Most folks don't realize it, but you can actually place multiple statements into if-else clauses. In fact, most people don't even realize that the batch file allows you to have an else clause. The following code demonstrates how you can have multi-line IF statements, so that you don't have to do a lot of goto's.

  if exist %sourceDir% (
md %targetDir%
xcopy /E %sourceDir% %targetDir%
copy %sourceDir%\*.lnk %targetDir%
) else (
echo %SourceDir% not found!
)

The '(' and ')' must be on the same line as the if and else statements as shown above. Even though they are not the open and close bracket, you can think of them as such. With multi-line if-else statements, you batch file can become far more structured than ever.

Chieh Cheng
Fri, 17 Oct 2008 21:35:20 +0000

I hit a snag today. The set variable function does not work inside a multi-statement if-else scope. For example, the following code does not work:

  if "%tempDir%" == "" (
set output=*** Variable tempDir not set!
echo %output%
echo %output% >> %logFile%
set success=false
)

The batch processor was never able to use the %output% that was set within that scope.

Chieh Cheng
Tue, 21 Oct 2008 22:29:32 +0000

The work around for this problem is to call a sub-routine in the if statement. The following is an example:

  if "%tempDir%" == "" (
call :subroutine
)
goto :end

:subroutine
set output=*** Variable tempDir not set!
echo %output%
echo %output% >> %logFile%
set success=false
goto :eof

:end

Chieh Cheng
Thu, 30 Oct 2008 20:42:59 +0000

Have you tried to at the beginning of your code set

SetLocal EnableDelayedExpansion

and before exiting from the script code

EndLocal

johan k
Tue, 09 Jun 2009 11:03:23 +0000

Just tried it, johan. That didn't work. :-( The following is the prototype code:

  SetLocal EnableDelayedExpansion

if "" == "" (
set output=*** just some text
echo %output%
)

EndLocal

Instead of printing "*** just some text", the output was "ECHO is off." . . . meaning %output% wasn't assigned a value.

Chieh Cheng
Tue, 09 Jun 2009 20:23:28 +0000

Hey Cheih,
I'm batch noob so I may be off base here, but I had this problem the other week and this syntax sorted it out:

echo.%output%

It unsures that the echo output command doesn't get confused with the echo on/off command. Seems like it's reading your output variable and defaulting ECHO to off because it's not a valid parameter (on or off). You've probably got over this problem by now, but this may help someone else.

luke mackay
Mon, 03 Aug 2009 01:18:57 +0000

Try:

echo !output!

Replacing the % with ! will solve your problem with setting a variable in a loop.

TheBlackOne
Wed, 26 Aug 2009 15:06:46 +0000

Thanks, TheBlackOne! That worked. Delayed expansion has to be enabled for this trick to work. The following is an example batch program that demonstrates this trick.

SetLocal EnableDelayedExpansion

if "" == "" (
set output=*** just some text
echo !output!
)

EndLocal

Chieh Cheng
Thu, 10 Sep 2009 10:11:37 +0000

I want to add to the previous comment that you can use the new variable within the IF clause outside the IF clause scope. The following example expands on the previous one and shows how it can be used outside the scope.

SetLocal EnableDelayedExpansion

if "" == "" (
set output=*** hello dude
echo !output!
)

echo !output!
echo %output%

EndLocal

Chieh Cheng
Fri, 25 Sep 2009 17:25:34 +0000

You are great!
Thank you very much.

Ivan Ferrer
Fri, 03 Jun 2011 15:44:48 +0000

Found another interesting side-effect. If "!output!" is assigned within the if-clause, then "%output%" will be assigned after exiting the if-clause scope. Use following example:

  SetLocal EnableDelayedExpansion

if "" == "" (
set output=*** just some text
echo %output%
echo !output!
echo %output%
)

echo %output%
echo !output!
echo %output%

EndLocal

The output is as follows:

  ECHO is off.
*** just some text
ECHO is off.
*** just some text
*** just some text
*** just some text

Notice that "%output%" cannot be reference in the if-clause scope, but it can be referenced in the global scope.

Chieh Cheng
Fri, 03 Jun 2011 16:27:40 +0000

Thanks for teaching me the call subroutine command. If statements are quite fickle and cmd batch scripts, sometime collapsing the script for no reason. Simplifying the if statement by making it call a subrountine really helped.

Fadi R
Sat, 09 Apr 2016 03:31:30 +0400

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

TrackBack

TrackBack only accepted from WebSite-X Suite web sites. Do not submit TrackBacks from other sites.

Send Ping | TrackBack URL | Spam Control

Title: SetVariableToOutput.bat (GPL)
Weblog: Cynosure.X International
Excerpt: You can use this batch file in a multi-line IF statements with delayed expansion enabled. The following example demonstrates how to use it in a multi-line IF statement. SetLocal EnableDelayedExpansion if "%success%" == "true" ( call SetVariableToOutput.bat type "%drive%\Purify.cfg" set pur . . .
Tracked: Tue, 22 Sep 2009 05:13:07 +0000

Add Comment

Spam Control | * indicates required field
Your Name: *
E-mail:
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 9 + 5?
Click button only once, please!

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

145 Users Online

Hacking Digital Cameras
Fun for Photographers


Get Our

Memecoins!



Amazon Associate

Copyright © 1996 - 2024. All Rights Reserved.