Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Script - Variable Strings With Spaces

Tags:

batch-file

If I do:

set dir1= %ProgramFiles%\Backup\files
set dir2= %ProgramFiles%\Backup\settings
set backup = B:\backup
start /b combine_file.exe %dir1% %dir2%

the above example just sees the first part i.e. C:\program and does not include the space yet.

If I do echo %dir1%, it will return the correct path. Where would you put the "" to resolve this problem?

Same problem happens, when you do some thing similar:

set /p somepath=Enter Path
start /b combine_file.exe %dir1%\%somepath%

You'll get an error, because of that space, yet when trying to put "" in, you often get another type of error, because of that. On test I know the example does work as if you pick a directory without spaces or manually put in "" on set /p blah= this works fine.

Pretty sure I'm just missing a simple switch or some thing like /I.

like image 667
Abberix Avatar asked Dec 19 '22 18:12

Abberix


1 Answers

set "dir1=%ProgramFiles%\Backup\files"
set "dir2=%ProgramFiles%\Backup\settings"
set "backup=B:\backup"

start /b combine_file.exe "%dir1%" "%dir2%"

set /p "somepath=Enter Path"
start /b combine_file.exe "%dir1%\%somepath%"

In general, use quotes on variable asignation to ensure the spaces are correctly handled, but don't include the quotes inside the values. So, all paths in variables doesn't contain quotes. When the value needs to be used, then, quote the variable.

like image 172
MC ND Avatar answered Jan 26 '23 16:01

MC ND