Monday, 3 June 2013

IF statements in batch files

IF statements in batch files I am currently using Cruise Control to schedule 'builds' for various internal websites. I use batch files to kick off each of these 'builds' (build meaning a specific webhelp output). Let's say for example purposes, we have builds labeled Internal, External and Public. Inside of the master batch file, you would find the following code: if x%TargetName:-Internal-=%==x%TargetName% goto ReplaceNav if x%TargetName:-External-=%==x%TargetName% goto SkipSearch if x%TargetName:-Public-=%==x%TargetName% goto SkipSearch :: do not uncomment or the skin will not get copied to the output So, let me explain what is going on here.. The variable %TargetName% is already set to a value before this part of the script runs. The variable %TargetName% will contain the text -Internal, -External or -Public and will have other characters at the beginning or end of each target name, but the thing I want to be able to have a conditional statement for is if the script sees -Internal in the %TargetName% variable, the script will then skip to a code block labeled :ReplaceNav. Here is what :ReplaceNav looks like. :ReplaceNav echo. echo ----------------------------------------------------------------- echo Replacing Navigation.htm and Search.htm files echo TargetName is %TargetName% echo CurrUser is %CurrUser% echo ----------------------------------------------------------------- echo. pushd ..\..\some\dir\MasterPages copy /y "Navigation1.COPY" "..\..\..\Output\%CurrUser%\%TargetName%\Skin\Navigation.htm" 2>&1 IF %ERRORLEVEL% NEQ 0 ( echo. echo Failed to replace Navigation.htm file with Exit Code: %ERRORLEVEL% echo. exit /b %ERRORLEVEL% ) copy /y "Search.COPY" "..\..\..\Output\%CurrUser%\%TargetName%\Skin\Search.htm" 2>&1 IF %ERRORLEVEL% NEQ 0 ( echo. echo Failed to replace Search.htm file with Exit Code: %ERRORLEVEL% echo. exit /b %ERRORLEVEL% ) popd ReplaceNav essentially replaces two files in the webhelp output with a two custom pages that contain a search engine that is only available to internal users. If the variable %TargetName% contains -External or -Public, the batch file SHOULD skip to the :SkipSearch section of the batch file. :SkipSearch echo. echo ----------------------------------------------------------------- echo Skipped Search and Navigation Replacement echo ----------------------------------------------------------------- echo. My issue is as follows (and I am pretty sure it has to do with not structuring the IF statement properly): How do I structure this code block if x%TargetName:-Internal-=%==x%TargetName% goto ReplaceNav if x%TargetName:-External-=%==x%TargetName% goto SkipSearch if x%TargetName:-Public-=%==x%TargetName% goto SkipSearch :: do not uncomment or the skin will not get copied to the output to properly step through the %TargetName% variable to see if it has the value of -Internal, -External or -Public and then skip to the correct code block in my script? Currently, it seems to evaluate the if x%TargetName:-Internal-=%==x%TargetName% goto ReplaceNav statement, but no matter what it is replacing the navigation pages on builds with -External and -Public as well :( I should also mention that this if st

No comments:

Post a Comment