Moritz Friedrich
Moritz Friedrich

Reputation: 1481

Batch echo command to file doesn't escape

I am writing a script to automate backup setup on new servers. I am not the batch guru, so it is kept rather simple (See here in complete for reference http://pastebin.com/D1zEP8dj). Everything works to the bit where I try to write another batch script to a file:

        (
        echo del "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before\*.*" /S /Q
        echo.
        echo move /Y "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Full\*.*" "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
        echo move /Y C:\Scripts\Backup.log "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
        echo move /Y C:\Scripts\Backupdiff.log "%BACKUP_SERVER%Snapshot-%%COMPUTERNAME%%\Week_before"
        echo.
        echo.
        echo C:\Scripts\snapshot.exe C: "%BACKUP_SERVER%Snapshot-$computername\Full\$disk-Partition-$type.sna" --LogFile:C:\Scripts\Backup.log -L50000 -GX --usevss --AllWriters  
        echo @echo off
        echo    if %%errorlevel%% NEQ 0 (

        echo        echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%  ^| C:\Scripts\blat\blat.exe -server %BLAT_MAILSERVER%  -to [email protected] -f backup@%BLAT_DOMAIN% -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backup.log
        echo    )
        ) >full-backup.bat

    if %DO_DIFF_BACKUP%==y (
        (
            echo C:\Scripts\snapshot.exe C: %BACKUP_SERVER%\Snapshot-$computername\Diff\$disk-Partition-$type-$weekday.sna -h%BACKUP_SERVER%\Snapshot-$computername\Full\c-Partition-ful.hsh --LogFile:C:\Scripts\Backupdiff.log -L50000 -GX --usevss --AllWriters  
            echo @echo off
            echo    if %%%errorlevel%%% NEQ 0 (
            echo        echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%  ^| C:\Scripts\blat\blat.exe -server %BLAT_MAILSERVER%  -to [email protected] -f backup@%BLAT_DOMAIN% -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backupdiff.log
            echo    )
        ) >diff-backup.bat
    )

So when executing the script, it stops on the last line of this excerpt. The file full-backup.bat doesn't exist at all, but won't throw an error at all, while the file diff-backup.bat is written with the following content:

 C:\Scripts\snapshot.exe C: c:\test\Snapshot-$computername\Diff\$disk-Partition-$type-$weekday.sna -hc:\test\Snapshot-$computername\Full\c-Partition-ful.hsh -- LogFile:C:\Scripts\Backupdiff.log -L50000 -GX --usevss --AllWriters  
 @echo off
    if %0% NEQ 0 (

The internet says, to escape percents double them, to escape pipes ^|-them, etc. But it does not seem to change anything if I quote things, use escape signs at all - batch won't listen to me :(. Any help would be greatly appreciated.

Upvotes: 1

Views: 535

Answers (1)

jeb
jeb

Reputation: 82307

When you double the percent signs where you want to echo literally % and escaping all other special characters with ^ it should work.

As you can't know if your variables contains also special characters, it would be the best to use delayed expansion here instead of percent expansion. This works as all content is safe when expanded by delayed expansion

setlocal EnabledDelayedExpansion
(
    echo del "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before\*.*" /S /Q
    echo.
    echo move /Y "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Full\*.*" "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
    echo move /Y C:\Scripts\Backup.log "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
    echo move /Y C:\Scripts\Backupdiff.log "!BACKUP_SERVER!Snapshot-%%COMPUTERNAME%%\Week_before"
    echo.
    echo.
    echo C:\Scripts\snapshot.exe C: "!BACKUP_SERVER!Snapshot-$computername\Full\$disk-Partition-$type.sna" --LogFile:C:\Scripts\Backup.log -L50000 -GX --usevss --AllWriters  
    echo @echo off
    echo    if %%errorlevel%% NEQ 0 (

    echo        echo Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%  ^| C:\Scripts\blat\blat.exe -server !BLAT_MAILSERVER!  -to [email protected] -f backup@!BLAT_DOMAIN! -s "Error while Backup at %%COMPUTERNAME%%.%%USERDOMAIN%%" -attach C:\Scripts\Backup.log
    echo    ^)
)

Upvotes: 1

Related Questions