3

In my batch file I have an if statement which is misbehaving. What am I missing which is causing this?

if "%moveType%"=="file" (
echo   Arcval File Move 1>>"%logfile%" 2>&1

if not exist %arcval_folder% (
    mkdir %arcval_folder%
    echo Directory: %arcval_folder% created. 1>>"%logfile%" 2>&1
    ECHO. 1>>"%logfile%" 2>&1
)

echo xcopy "%src_dir%" "%tgt_dir%" 1>>"%logfile%" 2>&1
echo F|xcopy "%src_dir%" "%tgt_dir%" 1>>"%logfile%" 2>&1

if ERRORLEVEL 1 (
    echo Failure: File not found, Arcval File not promoted. 1>>"%logfile%" 2>&1
    echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalFailure-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Failure! File not found, Arcval File not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
    "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalFailure-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Failure! File not found, Arcval File not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
    ECHO. 1>>"%logfile%" 2>&1

    echo Failure email sent, exiting program.

)   else (
    echo Success: Arcval File was successfully promoted. 1>>"%logfile%" 2>&1
    echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Arcval File was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
    "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Arcval File was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
    ECHO. 1>>"%logfile%" 2>&1

    echo Success email sent, exiting program.
)

goto end

)

if "%moveType%"=="directory" (
echo   Polysystems Directory Move 1>>"%logfile%" 2>&1

if not exist %tgt_dir% (
    mkdir %tgt_dir%
    echo Directory: %tgt_dir% created. 1>>"%logfile%" 2>&1
    ECHO. 1>>"%logfile%" 2>&1
) else (
    echo ERROR: Polysystems Target directory already exists, Source was not promoted. 1>>"%logfile%" 2>&1
    echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsErr-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "ERROR! Polysystems Target directory already exists, Source was not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
    ECHO. 1>>"%logfile%" 2>&1

    "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsErr-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "ERROR! Polysystems Target directory already exists, Source was not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
    ECHO. 1>>"%logfile%" 2>&1
    echo Failure email sent, exiting program. 1>>"%logfile%" 2>&1
    exit
)   

echo xcopy %src_dir% %tgt_dir% /Y /Z /C /F /E 1>>"%logfile%" 2>&1
echo F|xcopy %src_dir% %tgt_dir% /Y /Z /C /F /E 1>>"%logfile%" 2>&1

echo Success: Polysystems Directory was successfully promoted. 1>>"%logfile%" 2>&1
echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Polysystems Directory was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
ECHO. 1>>"%logfile%" 2>&1

"\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Polysystems Directory was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1
ECHO. 1>>"%logfile%" 2>&1
echo Success email sent, exiting program. 1>>"%logfile%" 2>&1

goto end
)

This is how it is behaving: when move type == file it runs through that if statement just fine, however, when the move type == directory, the script dies and does not do anything.

When the if statements are switched (directory first, file second) directory works and file doesn't

4
  • There is nothing wrong with the code you posted. The problem must be in code that you are not showing. Perhaps you have some unbalanced parentheses.
    – dbenham
    Commented Mar 26, 2014 at 15:16
  • @dbenham - I have updated my code with everything within the two if statements
    – staples
    Commented Mar 26, 2014 at 18:10
  • Is the code above within another loop?
    – foxidrive
    Commented Mar 26, 2014 at 18:51
  • @foxidrive no it is not. Above this code I parse a file to get my variables populated, and it dies after I echo the variables if it is the second if
    – staples
    Commented Mar 26, 2014 at 19:32

3 Answers 3

2

I can only come up with one scenario that could lead to the behavior you describe. I suspect that if moveType = directory, then arcval_folder must not be defined. Conversely, if moveType = file, then tgt_dir must not be defined.

The reason your code would fail in such a case is because complex IF statements are parsed all at once, including all related blocks of code that follow. The entire complex statement must have valid syntax, even if the IF statement evaluates to FALSE. If some portion of the IF statement does not parse properly, then the batch script terminates with a syntax error.

So if moveType = directory and arcval_folder is undefined, then the first IF block looks like the following after variable expansion:

if "file" == "file" (
  echo ....
  if not exist (
     etc.
  )
  etc.
  goto end
)

The inner IF NOT EXIST statement becomes invalid, and the entire script fails.

If moveType = file and tgt_dir is undefined, then the second IF block would be invalid, but the GOTO END in the first IF block bypasses the problem. You only have problems in this case if you reverse the order of the IF blocks.

The simplest solution is to enclose the IF NOT EXIST folder in quotes. Foxidrive is also correct that you probably should include a trailing \ to make sure you are only looking for folders.

if not exist "%arcval_folder%\" ( ...

if not exist "%tgt_dir%\" ( ...
2
  • so should I have all of my %tgt_dir% and %arcval_folder% variables in quotes?
    – staples
    Commented Mar 27, 2014 at 13:13
  • @staples - The expression should be quoted in the IF NOT EXIST statement. The quotes should not be in the variable values.
    – dbenham
    Commented Mar 27, 2014 at 13:54
2

Use double quotation characters and drop all extraneous whitespace:

if "%moveType%"=="directory" (
1
  • Not a bad idea to add the quotes, but I doubt it makes a difference in this case. The spaces before and after == do not cause a problem.
    – dbenham
    Commented Mar 26, 2014 at 15:17
1

From here the %tgt_dir% and other variables are not double quoted and spaces/& characters will break it.

if not exist %tgt_dir% (

and in this test a trailing backslash is also needed otherwise it can match a filename too.

if not exist "%tgt_dir%\" (
3
  • Correct solution, but I think wrong explanation. If spaces or poison characters were the problem, then the order of the IF statements would not matter. See my answer for my theory on why the code is failing.
    – dbenham
    Commented Mar 26, 2014 at 21:21
  • @dbenham The first if statement works because they are quoted. The second set fail, because they are unquoted - assuming there are spaces/& in the terms. We're actually guessing because we don't have the terms and so can't test the code with the real life data.
    – foxidrive
    Commented Mar 27, 2014 at 6:23
  • I think you missed my point. In OP's code, neither IF NOT EXIST statement is quoted. If space/& were causing the problem, then the order of the outer IF blocks would not matter - it would always fail. But the OP reports otherwise. My undefined variable theory does yield the asymmetric failure that depends on IF order. Yes it is a guess, but it is a highly educated guess. I don't see any other possible sources of error that could yield the observed behavior.
    – dbenham
    Commented Mar 27, 2014 at 12:16

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