双击批处理文件不起作用,因为脚本内的文件路径包含空格


0

下面是我的批处理脚本。我用扩展名file.bat保存了文件,然后双击了它。它没有显示任何内容,因为文件路径包含空格(以表示set file="C:\SUPPORT\APAC SIT\NewtextDoc.txt"

如果我使用的set file="C:\SUPPORT\APACSIT\NewtextDoc.txt" 话,它可以工作。
如果我使用set file="C:\SUPPORT\APAC SIT\NewtextDoc.txt"它就行不通。

@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

我认为我不能完全理解这个主意,但是您是否试图摆脱空白?用双引号括住脚本中包含空格的路径的意思" "

PS ^(脱字符)是命令提示符所解释的另一个转义字符

在我的批处理脚本中,路径设置为file ==“ C:\ SUPPORT \ APAC SIT \ NewtextDoc.txt”
suvarna

当我通过命令窗口运行批处理脚本时,它通过修改双引号来工作。但是,如果我双击bat文件,则无法正常工作
suvarna '16

您是否尝试从set的赋值中删除双引号并按^字符转义单个空格实例,例如APAC^ SIT

Answers:


0
  • cmd 定义变量时,使用双引号转义变量中包含有毒字符的字符串
    • set "_nl=& echo."
    • set "_file=path with spaces\name.ext"
    • (只有在少数情况下才需要使用另一个转义模式)
  • 然后以适当的方式使用变量
    • 转义: echo WARNING !!!%_nl%Scheduler File is ^=%_size% bytes
    • 转义: FOR /F "usebackq delims=" %%A IN ('"%_file%"') DO set "_size=%%~zA"

请注意,在下一个脚本中定义的变量名称以_低行(下划线)为前缀,以便于调试,请参阅SET _&PAUSE调试(临时)输出。

评论脚本

@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

输出

==> rename "D:\bat\odds and ends\a b\testfile.txt" testfilea.txt

==> set _
Environment variable _ not defined

==> D:\bat\SU\1130895.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\SU\1130895.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 . . .

==>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.