nec*_*ss_ 6 windows batch cmd.exe
我编写了这个小的备份脚本,它接受用户输入文件的源/目标。它可以很好地处理空格,C:\Program Files\
但似乎无法处理C:\Program Files (x86)\
. 这是我用来测试和解决我的问题的代码片段。
@echo off
set sourcedef=%USERPROFILE%\Desktop\Source
set /P sourceinp= Location of the source [Default=%sourcedef%] :
if "%sourceinp%" == "" ( set source=%sourcedef% )
if not "%sourceinp%" == "" ( set source=%sourceinp% )
echo Source folder set as : %source%
pause
Run Code Online (Sandbox Code Playgroud)
如果我尝试给它任何Program Files (x86)
目录,cmd.exe 就会关闭商店。有小费吗?
编辑: 感谢用户“不是我”解决了,非常感谢!
这是代码现在的样子:
@echo off
set source=%USERPROFILE%\Desktop\Source
set /p "source= Location of the source [Default=%source%] : " string ( str ^)
echo Source folder set as : %source%
pause
Run Code Online (Sandbox Code Playgroud)
\n
@echo off \n\nset "_source_default=%USERPROFILE%\\Desktop\\Source"\n\n^< nul <con: set /p "_source_input= Location of the source [Default=%_source_default%] : " \n\nif "%_source_input%" == "" (set "_source=%_source_default%") else set "_source=%_source_input%"\n \necho\\Source folder set as %_source%\n%__APPDIR__%Timeout.exe /t -1 & goto=:EOF
Run Code Online (Sandbox Code Playgroud)\n^< nul
在行的开头使用 , 这是@jeb在dostips.com中报告的最近发现的转义机械技巧,它将应用于用户输入之前的转义并扩展到用户输入:^< nul <con: can literal escaping your line and your input \n \n^< nul <con: set /p "_source_input= Location of the source [Default=%_source_default%] : "
Run Code Online (Sandbox Code Playgroud)\n关于输入这个/这些字符(
和/或)
及其转义(如果需要)
rem :: Using only (\nset /p "_input= Location : " string ( \nrem :: works ⁄⁄ output = string ( ⁄⁄ there\'s no need to escape\n\nrem :: Using only )\nset /p "_input= Location : " string )\nrem :: works ⁄⁄ output = string ) ⁄⁄ there\'s no need to escape\n\nrem :: Using both ( and )\nset /p "_input= Location : " string ( str ^) \nrem :: works ⁄⁄ output = string ( str ) ⁄⁄ there\'s no need in (, only in ^)
Run Code Online (Sandbox Code Playgroud)\nF:\\2020-SU\\1572099>input_testv2.cmd\nLocation of the source [Default=C:\\Users\\ecker\\Desktop\\Source] : string (((((((\nSource folder set as : string (((((((\n\nF:\\2020-SU\\1572099>input_testv2.cmd\nLocation of the source [Default=C:\\Users\\ecker\\Desktop\\Source] : ))))))))))) string\nSource folder set as : ))))))))))) string\n\nF:\\2020-SU\\1572099>input_testv2.cmd\nLocation of the source [Default=C:\\Users\\ecker\\Desktop\\Source] : ( ( ( string ^) ^) ^)\nSource folder set as : ( ( ( string ) ) )\n\n
Run Code Online (Sandbox Code Playgroud)\n)
,如果您(
之前有一个形成对,那么是的,您将需要使用每一对^)
,不需要额外的赔率来逃脱: )))
F:\\2020-SU\\1572099>input_testv2.cmd\nLocation of the source [Default=C:\\Users\\ecker\\Desktop\\Source] : ( ( ( string ^) ^) ^) ))))\nSource folder set as : ( ( ( string ) ) ) ))))\n
Run Code Online (Sandbox Code Playgroud)\n一些进一步阅读:
\n[\xe2\x88\x9a]设置 /?
\n[\xe2\x88\x9a]参考:^< nul
阶段解析规则1.5和2 CR与重定向中的Bug/Mystery
[\xe2\x88\x9a]了解批处理文件中的 start、2>nul、cmd 和其他符号
\n[\xe2\x88\x9a]使用圆括号/方括号对 Windows 批处理文件中的表达式进行分组
\n 归档时间: |
|
查看次数: |
565 次 |
最近记录: |