Batch file 使用批处理文件传递中间有空格的单词

Batch file 使用批处理文件传递中间有空格的单词,batch-file,Batch File,我刚刚编写了一个快速批处理文件,可以将某个目录输入上传到phone config中,而无需用户手动完成 @echo off set /p title= Contact name? set /p number= Contact phone number? start "" http://admin:password@172.31.2.214/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%title%)(e164=%number%)(h3

我刚刚编写了一个快速批处理文件,可以将某个目录输入上传到phone config中,而无需用户手动完成

@echo off
set /p title= Contact name? 
set /p number= Contact phone number? 
start "" http://admin:password@172.31.2.214/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%title%)(e164=%number%)(h323=%title%)
批处理文件获取变量,然后我将它们添加到start命令中。这样的脚本工作,它登录到电话界面,然后上传目录输入

如果我输入John作为标题,输入123456作为数字,我会得到一个带有此链接的新浏览器窗口

批处理文件工作,用户可以使用此批处理输入目录条目。但问题是,大多数标题都不止一个词。问题是,如果在第一个变量下输入John Smith,链接如下所示: 管理员:password@172.31.2.214/!mod%20cmd%20FLASHDIR0%20添加项目%20104%20(cn=John)


第二部分(标题变量中的空格后)被删除。如何强制批处理文件发送带有%20而不是空格字符的URL?

URL编码空间中的
+
123 456
变为
123+456
()。因此,您需要的是替换给定字符串中的空格,并使用
+
。这就是您可以执行的方法:

@echo off
set /p title= Contact name?
set /p number= Contact phone number?
set urltitle=%title: =+%
set urlnumber=%number: =+%
start "" http://admin:password@172.31.2.214/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%urltitle%)(e164=%urlnumber%)(h323=%title%)
如果您想将空格编码为
%20
,以下是您的代码:

@echo off
setlocal EnableDelayedExpansion
set /p title= Contact name?
set /p number= Contact phone number?
set urltitle=!title: =%%20!
set urlnumber=!number: =%%20!
start "" http://admin:password@172.31.2.214/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%urltitle%)(e164=%urlnumber%)(h323=%title%)

您可以使用
%variable:old=new%
替换变量中的另一个字符串。这样,要用其他内容替换空格,可以使用
%variable:=somethingelse%

但是,问题是您试图替换为
%20
,其中包含%符号。这将被解释为变量名。如果您确实想使用
%20
,而不是
+
,则必须使用转义百分号,如下所述:


在你链接的问题中,投票最多的答案是:“理论上,我认为你应该在
之前有
%20
,在
example.com/foo%20bar?foo+bar
之后有
+
”。此问题中使用的参数都是URL的一部分。没有
。更新了我的答案。感谢帮助!我更正了我这边的批处理文件,现在它正常工作了。很高兴我能提供帮助。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。
@echo off
setlocal EnableDelayedExpansion
set /p title= Contact name?
set /p number= Contact phone number?
set urltitle=!title: =%20!
set urlnumber=!number: =%20!
setlocal DisableDelayedExpansion
start "" http://admin:password@172.31.2.214/!mod%%20cmd%%20FLASHDIR0%%20add-item%%20104%%20(cn=%urltitle%)(e164=%urlnumber%)(h323=%urltitle%)