批处理脚本的文件路径中的空格

时间:2022-01-05 02:19:35

enter image description hereI wrote batch script for my project,as the path contains space,its not working. Could you please help me

在这里输入图像描述我为我的项目写了批处理脚本,因为路径包含空格,它不起作用。请你帮助我好吗

@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:\Program Files (x86)\Common Files\Zoom\Support\CptControl.exe"
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

2 个解决方案

#1


1  

Place quotes out of the value but protecting the asignment

将报价置于价值之外但保护对齐

set "file=C:\Program Files (x86)\Common Files\Zoom\Support\CptControl.exe"

There is no need for for /f to read the file size, use a simple for. Also note the quotes not stored in %file% are now included in the command

没有必要为/ f读取文件大小,使用简单的。另请注意,未存储在%file%中的引号现在包含在命令中

FOR %%A IN ("%file%") DO set "size=%%~zA"

Just a session capture

只是会话捕获

批处理脚本的文件路径中的空格

The test code

测试代码

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "file=C:\Program Files (x86)\Windows NT\Accessories\WordPad.exe"

    echo File to process ------------------------------------------------------------
    echo "%file%"
    echo(

    echo Data from dir command ------------------------------------------------------
    dir "%file%" | findstr /r /c:"^[^ ]"
    echo(

    echo Data from for command ------------------------------------------------------
    for %%a in ("%file%") do (
        echo %%~fa : %%~za
        set "size=%%~za"
    )
    echo Reported size: %size%
    echo(

#2


0  

First, you can set the path as unquoted and then just quote it later:

首先,您可以将路径设置为不带引号,然后稍后引用它:

set filepath=C:\Program Files (x86)\Common Files\Zoom\Support

and use it in below in between %

并在%之间使用它

"%filepath%\CptControl.exe"

or file name with spaces

或带空格的文件名

"%filepath%\Cpt Control.exe"

#1


1  

Place quotes out of the value but protecting the asignment

将报价置于价值之外但保护对齐

set "file=C:\Program Files (x86)\Common Files\Zoom\Support\CptControl.exe"

There is no need for for /f to read the file size, use a simple for. Also note the quotes not stored in %file% are now included in the command

没有必要为/ f读取文件大小,使用简单的。另请注意,未存储在%file%中的引号现在包含在命令中

FOR %%A IN ("%file%") DO set "size=%%~zA"

Just a session capture

只是会话捕获

批处理脚本的文件路径中的空格

The test code

测试代码

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "file=C:\Program Files (x86)\Windows NT\Accessories\WordPad.exe"

    echo File to process ------------------------------------------------------------
    echo "%file%"
    echo(

    echo Data from dir command ------------------------------------------------------
    dir "%file%" | findstr /r /c:"^[^ ]"
    echo(

    echo Data from for command ------------------------------------------------------
    for %%a in ("%file%") do (
        echo %%~fa : %%~za
        set "size=%%~za"
    )
    echo Reported size: %size%
    echo(

#2


0  

First, you can set the path as unquoted and then just quote it later:

首先,您可以将路径设置为不带引号,然后稍后引用它:

set filepath=C:\Program Files (x86)\Common Files\Zoom\Support

and use it in below in between %

并在%之间使用它

"%filepath%\CptControl.exe"

or file name with spaces

或带空格的文件名

"%filepath%\Cpt Control.exe"