我如何通过带有空格的目录路径到一个.cmd脚本?

问题描述 投票:0回答:1

我想两个参数传递到我的。加利福尼亚脚本来创建ARG1一个文件夹,并定义一个源代码目录的ARG2。源文件目录是“共享文档”。问题是,我无法通过的空间或通配符进入参数没有它,说明该路径不存在。我也试图与报价,同样的结果传递给它。

@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)

如何让脚本接受包含空格的参数中的任何方向将不胜感激。我知道的是,争论需要报价,我只是不知道语法有引号删除它插入变量之前

batch-file path spaces util.cmd
1个回答
1
投票

假设你正在执行批处理文件是这样的:

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)

现在还记得,如果你正在使用这些变量用空格,你需要使用它们以及何时引用变量。最好的做法是不管始终使用需要他们的报价。

© www.soinside.com 2019 - 2024. All rights reserved.