简体   繁体   中英

Windows 7 vs Windows 10 - Handling folder names with spaces in batch files

I'm trying to create a batch script which must run on both Windows 7 and Windows 10 . However, there seems to be a difference in the way each handles folder names with spaces and I have to escape them differently.

In particular, on Windows 7 I'm successfully using the following command:

start cmd.exe /k "%OpenOCD_bin_Path%\openocd.exe -f %OpenOCD_bin_Path%\..\scripts\interface\ftdi\SuperDongle.cfg -f %OpenOCD_bin_Path%\..\scripts\target\nrf52.cfg"

The OpenOCD_bin_Path includes a folder which has spaces in it; the full path it references is:

C:\Users\Rafael\AppData\Roaming\GNU MCU Eclipse\OpenOCD\0.10.0-7-20180123-1217\bin

However, on Windows 10 the same line fails and displays the following error:

'C:\\Users\\Rafael\\AppData\\Roaming\\GNU' is not recognized as an internal or external command, operable program or batch file.

If I change the command to:

start cmd.exe /k ""%OpenOCD_bin_Path%"\openocd.exe -f "%OpenOCD_bin_Path%"\..\scripts\interface\ftdi\SuperDongle.cfg -f "%OpenOCD_bin_Path%"\..\scripts\target\nrf52.cfg"

it succeeds.

Conversely, if I run that command on Windows 7 , I get this error:

'""C:\\Users\\Rafael\\AppData\\Roaming\\GNU' is not recognized as an internal or external command, operable program or batch file.

Notice the extra "" in front on this version.

TLDR; Is there a way I can make this work on both systems without using a switch to detect which OS is runnning?

cmd.exe is silly and removes the starting and ending quotes from the command but there is a hack to defeat it:

start cmd.exe /k if 1==1 "%OpenOCD_bin_Path%\openocd.exe" -f "%OpenOCD_bin_Path%\..\scripts\interface\ftdi\SuperDongle.cfg" -f "%OpenOCD_bin_Path%\..\scripts\target\nrf52.cfg"

Calling cmd.exe from start is pointless anyway, just use start :

start "" "%OpenOCD_bin_Path%\openocd.exe" -f "%OpenOCD_bin_Path%\..\scripts\interface\ftdi\SuperDongle.cfg" -f "%OpenOCD_bin_Path%\..\scripts\target\nrf52.cfg"

It is important that %OpenOCD_bin_Path% is the same on all systems in terms of quotes. You normally don't quote paths set in the environment because it is hard to remove the quotes again when needed.

You can strip the quotes but ideally you should avoid dealing with string manipulation in batch files because of escape issues.

Here is a example that tries to handle both cases:

@echo off
goto run_tests

:stripquotes
FOR /F "tokens=1,*" %%A IN ('echo.%*') DO set %%~A=%%~B
goto :EOF

:test
call :stripquotes mynewvar %*
echo DEBUG: mynewvar is now %mynewvar%
start "" "%mynewvar%\app.exe" "par am 1" param2 "par am 3"
goto :EOF


:run_tests
set OpenOCD_bin_Path=c:\foo bar\baz
call :test %OpenOCD_bin_Path%

set OpenOCD_bin_Path="c:\foo bar\baz"
call :test %OpenOCD_bin_Path%

Using either of the ideas I provided in my comment should bypass the spaces.

Using CD /D :

@Echo Off
Set "OpenOCD_bin_Path=%AppData%\GNU MCU Eclipse\OpenOCD\0.10.0-7-20180123-1217\bin"
CD /D "%OpenOCD_bin_Path%\..\scripts" 2>Nul || Exit / B
Start "" Cmd /K "..\bin\openocd.exe -f interface\ftdi\SuperDongle.cfg -f target\nrf52.cfg"

Or you could try it without explicitly starting cmd.exe :

@Echo Off
Set "OpenOCD_bin_Path=%AppData%\GNU MCU Eclipse\OpenOCD\0.10.0-7-20180123-1217\bin"
CD /D "%OpenOCD_bin_Path%\..\scripts" 2>Nul || Exit / B
Start ..\bin\openocd.exe -f interface\ftdi\SuperDongle.cfg -f target\nrf52.cfg

Using Start /D :

@Echo Off
Set "OpenOCD_bin_Path=%AppData%\GNU MCU Eclipse\OpenOCD\0.10.0-7-20180123-1217\bin"
Start "" /D "%OpenOCD_bin_Path%" Cmd /K "openocd.exe -f ..\scripts\interface\ftdi\SuperDongle.cfg -f ..\scripts\target\nrf52.cfg"

Or once again without explicitly starting cmd.exe :

@Echo Off
Set "OpenOCD_bin_Path=%AppData%\GNU MCU Eclipse\OpenOCD\0.10.0-7-20180123-1217\bin"
Start "" /D "%OpenOCD_bin_Path%" openocd.exe -f ..\scripts\interface\ftdi\SuperDongle.cfg -f ..\scripts\target\nrf52.cfg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM