(Solved) How to deal with Program Files (x86) as user input?

Solution 1:

@echo off 

set "_source_default=%USERPROFILE%\Desktop\Source"

^< nul <con: set /p "_source_input= Location of the source [Default=%_source_default%] : " 

if "%_source_input%" == "" (set "_source=%_source_default%") else set "_source=%_source_input%"
   
echo\Source folder set as %_source%
%__APPDIR__%Timeout.exe /t -1 & goto=:EOF

  • You can use ^< nul, at the beginning of your line, this is a recently discovered escaping mechanical trick reported by @jeb in dostips.com, that will apply to escape before user input and extended to user input:

enter image description here


  • Applying/adapting this discovery to your code/input would be:
^< nul <con: can literal escaping your line and your input 
    
^< nul <con: set /p "_source_input= Location of the source [Default=%_source_default%] : " 

About input this/these characters ( and/or ) and their escaping (if necessary)

rem :: Using only (
set /p "_input= Location : " string (  
rem ::     works ⁄⁄ output = string (  ⁄⁄  there's no need to escape

rem :: Using only )
set /p "_input= Location : " string )
rem ::     works ⁄⁄ output = string )  ⁄⁄  there's no need to escape

rem :: Using both ( and )
set /p "_input= Location : " string ( str ^)  
rem ::     works ⁄⁄ output = string ( str )  ⁄⁄  there's no need in (, only in ^)
  • Test and outputs:
F:\2020-SU\1572099>input_testv2.cmd
Location of the source [Default=C:\Users\ecker\Desktop\Source] : string (((((((
Source folder set as :  string (((((((

F:\2020-SU\1572099>input_testv2.cmd
Location of the source [Default=C:\Users\ecker\Desktop\Source] : ))))))))))) string
Source folder set as :  ))))))))))) string

F:\2020-SU\1572099>input_testv2.cmd
Location of the source [Default=C:\Users\ecker\Desktop\Source] : ( ( ( string ^) ^) ^)
Source folder set as :  ( ( ( string ) ) )

  • Obs: For each ), if you have one ( previous, forming pairs, then yes, you will need to use each pair ^), additional odds ))) are not necessary to escape:
F:\2020-SU\1572099>input_testv2.cmd
Location of the source [Default=C:\Users\ecker\Desktop\Source] : ( ( ( string ^) ^) ^) ))))
Source folder set as :  ( ( ( string ) ) ) ))))

  • Some further reading:

    [√] set /?

    [√] Refer: ^< nul Bug/Mystery in the phase parsing rules 1.5 and 2 CR vs redirect

    [√] Understanding start, 2>nul, cmd, and other symbols in a batch file

    [√] Use parentheses/brackets to group expressions in a Windows batch file

Solution 2:

If I input and Program Files (x86) directory, cmd errors

This is because ( and ) are special characters in cmd scripts and they need to be escaped using ^.

Without escaping:

F:\test>test
Location of the source [Default=C:\Users\David\Desktop\Source] : C:\Program Files (x86)
) was unexpected at this time.

F:\test>

With escaping:

F:\test>test
Location of the source [Default=C:\Users\David\Desktop\Source] : C:\Program Files ^(x86^)
 Source folder set as : C:\Program Files (x86)
Press any key to continue . . .

Further Reading

  • An A-Z Index of the Windows CMD command line | SS64.com
  • Windows CMD Commands (categorized) - Windows CMD - SS64.com
  • Quotes, Escape Characters, Delimiters - Windows CMD - SS64.com
  • Parenthesis/Brackets - Windows CMD - SS64.com