Why does reformatting a successful batch file cause failure?

时间:2018-06-04 17:46:57

标签: batch-file

The following batch file was written by @Compo in another post. It was required by one of my grandchildren at school, so I simply emailed the file as is. It worked perfectly for me and for her.

The batch adds a subdirectory to E:\Evernote.

The name of the subdirectory is the date in yyyy_mm_dd.

Files are copied from C: to the new subdirectory.

@Echo Off
Set "sd=C:\EverNote"
Set "dd=E:\EverNote"
Set "ds="
If Not Exist "%sd%\" Exit /B
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A_%%B_%%C"
If Not Defined ds Exit /B
RoboCopy "%sd%" "%dd%\%ds%" /E

The heat is now off, but in a few weeks the second part of the project is to submit the documented batch file for marking. My first step was to make the batch more "readable", using rules provided by the school. That means using pairs of parentheses to surround conditional statements, and plenty of "white space" and REM statements.

Problem: After adding parentheses and white space, with NO alteration to any of the statements (AFAIK), the following reformatted batch fails after a few lines.

@Echo Off
Set "sd=C:\EverNote"
Set "dd=E:\EverNote"
Set "ds="

REM The next @echo is respected
@echo "If Not Exist"

REM After the next "Pause", and hitting any key, the batch exits.
REM IMO, that means "Exit /B" is being executed. 
REM In turn, that means "%sd%\" does not exist. Why not?

Pause

If Not Exist "%sd%\" 
(    
    Exit /B
)      

For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null') Do 
(
    If Not Defined ds 
    (
        Set "ds=%%A_%%B_%%C"
    )
)

If Not Defined ds 
(
    Exit /B
)

RoboCopy "%sd%" "%dd%\%ds%" /E

Pause

I would be grateful if someone could explain how a change in formatting can cause failure.

0 个答案:

没有答案