clicar duas vezes no arquivo de lote não está funcionando, pois o caminho de arquivo dentro do script contém espaço

0

Abaixo está o meu script lote. Eu salvei o arquivo com a extensão file.bat e cliquei duas vezes nele. Não mostra nada, pois o caminho do arquivo contém espaço (em set file="C:\SUPPORT\APAC SIT\NewtextDoc.txt" )

Se eu usar set file="C:\SUPPORT\APACSIT\NewtextDoc.txt" , funciona.
Se eu usar set file="C:\SUPPORT\APAC SIT\NewtextDoc.txt" , então não funciona.

@ECHO OFF
REM  The below command will look for the size of file on the server and
     inform the user if scheduler is down.
setlocal  
set nl=^& echo.
set file="C:\SUPPORT\APAC SIT\NewtextDoc.txt"
set maxbytesize=0

FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA

if %size% EQU %maxbytesize% (echo WARNING !!! %nl%Scheduler File is ^= 

%maxbytesize% bytes%nl%Please do not process invoices, contact Webcenter 
Support) else (echo Scheduler File OK)

PAUSE
    
por suvarna 03.10.2016 / 11:25

1 resposta

0

  • 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"

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 . . .

==>
    
por 03.10.2016 / 16:05

Tags