如何将带空格的目录路径传递到 .cmd 脚本中?
How do I pass a directory path with spaces into a .cmd script?
我正在尝试将两个参数传递到我的 .cmd 脚本中,以便为 arg1 创建一个文件夹并为 arg2 定义一个源目录。源目录是 "Shared Documents"。问题是我不能将 space 或通配符传递给参数,除非它声明路径不存在。我也试过用引号传递它,结果相同。
@set dirYr=%1
@set dir1=%2
@set connectionroot=https://somewhere.com/sites/foo_bar/foobar/FooBar::Source path
@set sourcepath=https://somewhere.com/sites/foo_bar/foobar/FooBar\%dir1%::Destination path
@set destinationpath=\foo\bar\%dirYr%\%dir1%
::Check commandline argument to make sure that %2 is present.
IF NOT DEFINED dir1 (echo USAGE: foo_bar.cmd ^<YYYY^>^<Directory^|Filename^>GOTO badexit)
任何关于如何让脚本接受带有 space 的参数的指导将不胜感激。我知道参数需要引号,我只是不知道在插入变量之前删除引号的语法
假设您正在执行这样的批处理文件:
myscript.bat "arg 1" "arg 2"
使用这些命令行参数和定义变量的最佳实践如下所示。
@echo off
set "dirYr=%~1"
set "dir1=%~2"
set "connectionroot=https://somewhere.com/sites/foo_bar/foobar/FooBar"
set "sourcepath=https://somewhere.com/sites/foo_bar/foobar/FooBar\%dir1%"
set "destinationpath=\foo\bar\%dirYr%\%dir1%"
::Check commandline argument to make sure that %2 is present.
IF NOT DEFINED dir1 (echo USAGE: foo_bar.cmd ^<YYYY^>^<Directory^|Filename^>GOTO badexit)
现在请记住,如果您使用带空格的变量,则在使用它们时也需要引用这些变量。最佳做法是始终使用引号,无论是否需要它们。
我正在尝试将两个参数传递到我的 .cmd 脚本中,以便为 arg1 创建一个文件夹并为 arg2 定义一个源目录。源目录是 "Shared Documents"。问题是我不能将 space 或通配符传递给参数,除非它声明路径不存在。我也试过用引号传递它,结果相同。
@set dirYr=%1
@set dir1=%2
@set connectionroot=https://somewhere.com/sites/foo_bar/foobar/FooBar::Source path
@set sourcepath=https://somewhere.com/sites/foo_bar/foobar/FooBar\%dir1%::Destination path
@set destinationpath=\foo\bar\%dirYr%\%dir1%
::Check commandline argument to make sure that %2 is present.
IF NOT DEFINED dir1 (echo USAGE: foo_bar.cmd ^<YYYY^>^<Directory^|Filename^>GOTO badexit)
任何关于如何让脚本接受带有 space 的参数的指导将不胜感激。我知道参数需要引号,我只是不知道在插入变量之前删除引号的语法
假设您正在执行这样的批处理文件:
myscript.bat "arg 1" "arg 2"
使用这些命令行参数和定义变量的最佳实践如下所示。
@echo off
set "dirYr=%~1"
set "dir1=%~2"
set "connectionroot=https://somewhere.com/sites/foo_bar/foobar/FooBar"
set "sourcepath=https://somewhere.com/sites/foo_bar/foobar/FooBar\%dir1%"
set "destinationpath=\foo\bar\%dirYr%\%dir1%"
::Check commandline argument to make sure that %2 is present.
IF NOT DEFINED dir1 (echo USAGE: foo_bar.cmd ^<YYYY^>^<Directory^|Filename^>GOTO badexit)
现在请记住,如果您使用带空格的变量,则在使用它们时也需要引用这些变量。最佳做法是始终使用引号,无论是否需要它们。