(已解决)如何将 Program Files (x86) 作为用户输入处理?

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)

It *_* Me 3

\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
\n
    \n
  • 您可以^< nul在行的开头使用 , 这是@jebdostips.com中报告的最近发现的转义机械技巧,它将应用于用户输入之前的转义并扩展到用户输入:
  • \n
\n

在此输入图像描述

\n
\n
    \n
  • 将此发现应用/调整到您的代码/输入将是:
  • \n
\n
^< 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
\n

关于输入这个/这些字符(和/或)及其转义(如果需要)

\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)\n
    \n
  • 测试和输出:
  • \n
\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\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
    \n
  • Obs:对于每个),如果您(之前有一个形成,那么是的,您将需要使用每一对^),不需要额外的赔率来逃脱: )))
  • \n
\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\n


归档时间:

查看次数:

565 次

最近记录:

5 年,1 月 前