-
Escape strings contendo
cmd
venenoso caracteres em variáveis usando aspas duplas quando definindo variáveis :-
set "_nl=& echo."
-
set "_file=path with spaces\name.ext"
- (existem algumas raras circunstâncias apenas onde é necessário outro esquema de escape)
-
- e, em seguida, use variáveis de maneira adequada :
- sem escape:
echo WARNING !!!%_nl%Scheduler File is ^=%_size% bytes
- com escape:
FOR /F "usebackq delims=" %%A IN ('"%_file%"') DO set "_size=%%~zA"
- sem escape:
Observe que os nomes de variáveis definidos no próximo script prefixados com uma linha _
low (sublinhado) para facilitar a depuração, consulte SET _&PAUSE
debugging (temporary) output.
Script comentado :
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
REM The below command will look for the size of file on the server
REM and inform the user if scheduler is down.
set "_nl=& echo." escape ampersand by double quotes
set "_file=D:\bat\odds and ends\a b\testfile.txt" escape spaces using double quotes
rem set "_file=C:\SUPPORT\APAC SIT\NewtextDoc.txt"
set "_maxbytesize=0" keep using double quotes even if unnecessary
if not exist "%_file%" (
set /A "_size=_maxbytesize-1"
echo "%_file%" does not exist
) else (
FOR /F "usebackq delims=" %%A IN ('"%_file%"') DO set "_size=%%~zA"
)
echo debugging output should show variables _file, _maxbytesize, _nl, _size
SET _&PAUSE
if %_size% LEQ %_maxbytesize% (
echo WARNING !!!%_nl%Scheduler File is ^=%_size% bytes
echo Please do not process invoices, contact Webcenter Support
) else (
echo Scheduler File OK%_nl%"%_file%" filesize is %_size% bytes
)
PAUSE
Saída :
==> rename "D:\bat\odds and ends\a b\testfile.txt" testfilea.txt
==> set _
Environment variable _ not defined
==> D:\bat\SU30895.bat
"D:\bat\odds and ends\a b\testfile.txt" does not exist
debugging output should show variables _file, _maxbytesize, _nl, _size
_file=D:\bat\odds and ends\a b\testfile.txt
_maxbytesize=0
_nl=& echo.
_size=-1
Press any key to continue . . .
WARNING !!!
Scheduler File is =-1 bytes
Please do not process invoices, contact Webcenter Support
Press any key to continue . . .
==> rename "D:\bat\odds and ends\a b\testfilea.txt" testfile.txt
==> D:\bat\SU30895.bat
debugging output should show variables _file, _maxbytesize, _nl, _size
_file=D:\bat\odds and ends\a b\testfile.txt
_maxbytesize=0
_nl=& echo.
_size=13
Press any key to continue . . .
Scheduler File OK
"D:\bat\odds and ends\a b\testfile.txt" filesize is 13 bytes
Press any key to continue . . .
==>