staples
staples

Reputation: 424

Batch if statement doesn't work

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

Upvotes: 3

Views: 1666

Answers (3)

dbenham
dbenham

Reputation: 130919

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%\" ( ...

Upvotes: 2

foxidrive
foxidrive

Reputation: 41287

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%\" (

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234875

Use double quotation characters and drop all extraneous whitespace:

if "%moveType%"=="directory" (

Upvotes: 2

Related Questions