Abberix
Abberix

Reputation: 73

Batch Script - Variable Strings With Spaces

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.

Upvotes: 7

Views: 35895

Answers (3)

Loïc MICHEL
Loïc MICHEL

Reputation: 26180

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

Upvotes: 2

MC ND
MC ND

Reputation: 70961

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.

Upvotes: 13

Rafael
Rafael

Reputation: 3122

You can try put "" on SET command:

set "dir1=%ProgramFiles%\Backup\files"
set "dir2= %ProgramFiles%\Backup\settings"
set /p "somepath=Enter Path"

Upvotes: 0

Related Questions