在 Windows .cmd 文件或批处理文件中设置路径中包含空格的路径变量

发布于 2024-08-13 11:15:06 字数 388 浏览 3 评论 0原文

我对剧本写作很陌生,无法让这个作品发挥作用。如果我将文件移动到其中没有空格的路径,我就可以,但如果可以的话,我希望它能够使用该空格。

我想将一堆 Office 更新提取到包含 .cmd 文件的文件夹中。为了使批处理文件在任何计算机上都可用,我设置了一个路径变量,只需在一个地方更改该变量即可在另一台计算机上运行它。问题是路径中有空格。如果我在定义中的路径周围加上引号,cmd.exe 会在附加文件名和开关之前将它们放在路径周围,并且批处理会失败并显示“命令行语法错误”。如果没有引号,它将失败并显示“不被识别为内部或外部命令、可操作程序或批处理文件”。

为了进行测试,我正在使用帮助开关,直到或者如果我可以让它工作。我可以使用 8.3 文件/文件夹名称(例如,我的文档为 MyDocu~1)来完成此操作,但可以用不同的方式完成吗?

I'm new to script writing and can't get this one to work. I could if I moved the files to a path without a space in it, but I'd like it to work with the space if it could.

I want to extract a bunch of Office updates to a folder with a .cmd file. To make the batch file usable on any computer, I set a path variable which I only have to change in one place to run it on another machine. The problem is that the path has a space in it. If I put quotes around the path in the definition, cmd.exe puts them around the path before it appends the filename and switches and the batch fails with "Command line syntax error." Without quotes, it fails with, "is not recognized as an internal or external command, operable program, or batch file."

For testing, I'm using the help switch until or if I can get it working. I can do it using an 8.3 file/folder name (e.g. My Documents as MyDocu~1), but can it be done a different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

潦草背影 2024-08-20 11:15:06

尝试这样的事情:

SET MY_PATH=C:\Folder with a space

"%MY_PATH%\MyProgram.exe" /switch1 /switch2

Try something like this:

SET MY_PATH=C:\Folder with a space

"%MY_PATH%\MyProgram.exe" /switch1 /switch2
冷默言语 2024-08-20 11:15:06

使用:

set "VAR_NAME=<String With Spaces>"

我在更新路径时

set "PATH=%UTIL_DIR%;%PATH%"

I use

set "VAR_NAME=<String With Spaces>"

when updating path:

set "PATH=%UTIL_DIR%;%PATH%"
夜夜流光相皎洁 2024-08-20 11:15:06

这里有两个选择。首先,您可以存储不带引号的路径,然后稍后引用它:

set MyPath=C:\Program Files\Foo
"%MyPath%\foo with spaces.exe" something

您可以使用的另一个选项是一个子例程,该子例程用于不带引号的字符串(但在这种情况下,它实际上不是一个好主意,因为您要添加引号,删除它们并再次重新添加它们,但没有任何好处):

set MyPath="C:\Program Files\Foo"
call :foo %MyPath%
goto :eof

:foo
"%~1\foo.exe"
goto :eof

%~1 删除参数周围的引号。当传递文件夹名称时,这会很方便,但正如前面所说,在这种特殊情况下,这不是最好的主意:-)

There are two options here. First, you can store the path unquoted and just quote it later:

set MyPath=C:\Program Files\Foo
"%MyPath%\foo with spaces.exe" something

Another option you could use is a subroutine which alles for un-quoting strings (but in this case it's actually not a very good idea since you're adding quotes, stripping them away and re-adding them again without benefit):

set MyPath="C:\Program Files\Foo"
call :foo %MyPath%
goto :eof

:foo
"%~1\foo.exe"
goto :eof

The %~1 removes quotation marks around the argument. This comes in handy when passing folder names around quoted but, as said before, in this particular case it's not the best idea :-)

猫九 2024-08-20 11:15:06

试试这个;

  1. 创建一个变量如下

    SET“SolutionDir=C:\测试项目\自动化测试\bin\调试”**
    
  2. 然后用变量替换路径。确保为开始和结束添加引号

    vstest.console.exe“%SolutionDir%\Automation.Specs.dll”
    

Try this;

  1. create a variable as below

    SET "SolutionDir=C:\Test projects\Automation tests\bin\Debug"**
    
  2. Then replace the path with variable. Make sure to add quotes for starts and end

    vstest.console.exe "%SolutionDir%\Automation.Specs.dll"
    
尐籹人 2024-08-20 11:15:06

创建 .bat 文件时,我总是将路径放在双引号中。 (我刚刚添加了暂停,因此它不会关闭屏幕。)

例如:

"C:\Program Files\PageTech\PCLReader64_131\PCLReader64.exe"
PAUSE

I always place the path in double quotes when I am creating a .bat file. (I just added the PAUSE so it wont close the screen.)

For example:

"C:\Program Files\PageTech\PCLReader64_131\PCLReader64.exe"
PAUSE
ま柒月 2024-08-20 11:15:06

执行此操作的正确方法如下所示:

@ECHO off
SET MY_PATH=M:\Dir\^
With Spaces\Sub Folder^
\Dir\Folder
:: calls M:\Dir\With Spaces\Sub Folder\Dir\Folder\hello.bat
CALL "%MY_PATH%\hello.bat"
pause

The proper way to do this is like so:

@ECHO off
SET MY_PATH=M:\Dir\^
With Spaces\Sub Folder^
\Dir\Folder
:: calls M:\Dir\With Spaces\Sub Folder\Dir\Folder\hello.bat
CALL "%MY_PATH%\hello.bat"
pause
深居我梦 2024-08-20 11:15:06

解决此问题的最简单方法是将文件夹名称放在引号中:

(cd\New Folder\...) --> (cd\"新文件夹"\...)

希望这会有所帮助。

The easiest way to fix this problem is to put the folder name in quotes:

(cd\New Folder\...) --> (cd\"New Folder"\...)

Hopes this helps.

葬﹪忆之殇 2024-08-20 11:15:06

我最近遇到了同样的问题,想象一下......

C:\ (root)
└  Folder with spaces
     └  File1.txt
     └  File2.txt

❌问题[不该做什么]

你是对的,如果你在路径周围添加“”......

SET RootFolder="C:\Folder with spaces"

如果你想附加文件名等,它就会变得不可用(不对字符串进行一些临时处理...

FOR %%F IN * DO (COPY %%F "%RootFolder%\%%F")

COPY File1.txt ""C:\Folder with spaces"\File1.txt"  <-- ❌ Invalid path
COPY File2.txt ""C:\Folder with spaces"\File2.txt"  <-- ❌ Invalid path

✅ 解决方案 [做什么]

关键是仍然使用“”,但将它们放在整个 SET 语句周围(即 BEFORE变量名称)

SET "RootFolder=C:\Folder with spaces"

当您需要重新使用变量时,这将起作用

FOR %%F IN * DO (COPY %%F "%RootFolder%\%%F")

COPY File1.txt "C:\Folder with spaces\File1.txt"
COPY File1.txt "C:\Folder with spaces\File2.txt"

I had this same problem recently, imagine...

C:\ (root)
└  Folder with spaces
     └  File1.txt
     └  File2.txt

❌ Issue [What NOT to do]

You're right, if you add " " around the path...

SET RootFolder="C:\Folder with spaces"

it then makes it unusable if you want to then append filenames, etc (without doing some interim processing on the string...

FOR %%F IN * DO (COPY %%F "%RootFolder%\%%F")

COPY File1.txt ""C:\Folder with spaces"\File1.txt"  <-- ❌ Invalid path
COPY File2.txt ""C:\Folder with spaces"\File2.txt"  <-- ❌ Invalid path

✅ Solution [What to do]

The key is to still use " ", but put them around the entire SET statement (i.e. BEFORE the variable name)

SET "RootFolder=C:\Folder with spaces"

This will them work when you need to re-use the variable

FOR %%F IN * DO (COPY %%F "%RootFolder%\%%F")

COPY File1.txt "C:\Folder with spaces\File1.txt"
COPY File1.txt "C:\Folder with spaces\File2.txt"
永不分离 2024-08-20 11:15:06

如果需要存储永久路径(cmd重启时路径不会改变)

  1. 以管理员身份运行命令提示符(右键单击cmd.exe并选择以管理员身份运行)

  2. 在 cmd 中
    setx 路径“%path%;你的新路径”
    输入

  3. 输入path并回车检查路径是否正确

If you need to store permanent path (path is not changed when cmd is restart)

  1. Run the Command Prompt as administrator (Right click on cmd.exe and select run as administrator)

  2. In cmd
    setx path "%path%;your new path"
    then enter

  3. Check whether the path is taken correctly by typing path and pressing enter

唠甜嗑 2024-08-20 11:15:06

也可以尝试添加双斜杠
就像这对我有用,

set dir="C:\\\1. Some Folder\\\Some Other Folder\\\Just Because"

@echo on
MKDIR %dir%

天啊,在发帖后他们删除了我帖子中的第二个 \,所以如果你打开我的评论,它显示三个,你应该将它们读为两个......

also just try adding double slashes
like this works for me only

set dir="C:\\\1. Some Folder\\\Some Other Folder\\\Just Because"

@echo on
MKDIR %dir%

OMG after posting they removed the second \ in my post so if you open my comment and it shows three you should read them as two......

べ繥欢鉨o。 2024-08-20 11:15:06

如果您不想/不需要在批处理脚本中声明变量,我已经解决了脚本调用中带有空格的路径的此问题,方法是在整个路径中添加双引号,并向带有空格的文件夹添加单引号:

powershell.exe "C:\FolderWithoutSpaces\'Folder With Spaces'\SubFolderWithoutSpaces\ScriptFile.ps1"

If you don't want/need to declare a variable in your batch script, I've worked around this issue for a path with spaces in a script call by adding double quotes to the whole path and single quotes to he folder with the spaces:

powershell.exe "C:\FolderWithoutSpaces\'Folder With Spaces'\SubFolderWithoutSpaces\ScriptFile.ps1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文