Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 statement was not written by me at all and if you can think of some other way to parse the %TargetName% variable for -Internal, -Public or -External I am all for it.

I do understand this that is kind of a strange question, so please let me know if you need any further details in order to answer.

share|improve this question
1  
Any good reason you're not doing this in PowerShell? –  Tanner Jun 3 '13 at 15:45
    
Not really.. Just not very good with powershell and these scripts were written before I took over the task of managing this project at work. –  Richie086 Jun 3 '13 at 16:11
    
and that being said, is there a really easy way to do that sort of thing in powershell? if there is, I might be interested trying to turn these into powershell scripts if someone can point me towards a solution that works in PS. –  Richie086 Jun 3 '13 at 16:13
    
I think you'd be a lot happier if you did. You could actually use parameters and switches rather than parsing a string. technet.microsoft.com/en-us/magazine/jj554301.aspx Not to mention it would be much more readable. –  Tanner Jun 3 '13 at 16:39
    
@Richie086: Besides what dbenham said below about the extra hyphens, try placing echos before all 3 ifs and see what values are printed out for x%TargetName:-Internal-=%, x%TargetName:-External-=%, x%TargetName:-Public-=% and x%TargetName%. –  Karan Jun 4 '13 at 22:46

2 Answers 2

You say you are looking for -Internal, but your search and replace is looking for -Internal-. This is true for all your search and replace operations. I suspect it may work if you simply remove the unwanted trailing - from the searches.

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
share|improve this answer
    
+1, obviously.... –  Endoro Jun 4 '13 at 4:22

Seeing as you say PowerShell would be acceptable, I would encourage going that route.

I am not 100% sure what you are doing here, but here is an example script that I believe would do part of what you are asking. You can then decide if you would like to migrate more towards Powershell.

Check if the targetName contains "Internal":

If ($TargetName -Contains "Internal")
  {
    FunctionReplaceNav($TargetName)
  }

So that is a little more stright forward and easy to read. Then, again as more of an outline then the final code:

You could do additional processing on the replace in a function (Or inline you wanted in the original If Statement) like:

$CopyVariableName = Copy-Item Object.Ext copyToDirectory\Folder
if (!($CopyVariable))
   {
    Write-Host "Something Went wrong copying: $TargetName"
   }
else 
   {
    #Nothing here, moves along
    }

Of course, we could also make the error catching much more robust, for example by using a Try, Catch, Finally and catching any system execptions like Access denied, target machine unreachable, etc..

share|improve this answer
    
Ill give this a try tomorrow. Any idea how to do positional parameters in powershell? Like %1 %2 %3. –  Richie086 Jun 6 '13 at 3:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.