Windows 7与Windows 10 - 处理批处理文件中包含空格的文件夹名称

时间:2018-04-13 21:54:29

标签: windows batch-file windows-7 escaping windows-10

我正在尝试创建必须同时在Windows 7Windows 10上运行的批处理脚本。但是,每个处理文件夹名称与空格的方式似乎有所不同,我必须以不同的方式对它们进行转义。

特别是在Windows 7我成功使用以下命令:

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"

OpenOCD_bin_Path包含一个包含空格的文件夹;它引用的完整路径是:

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

但是,在Windows 10上,同一行失败并显示以下错误:

  

' C:\用户\圣拉斐尔\应用程序数据\漫游\ GNU'不被识别为内部或外部命令,可操作程序或批处理文件。

如果我将命令更改为:

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"

成功。

相反,如果我在Windows 7上运行该命令,我会收到此错误:

  

'"" C:\用户\圣拉斐尔\应用程序数据\漫游\ GNU'不被识别为内部或外部命令,可操作程序或批处理文件。

请注意此版本前面的额外""

TLDR;有没有办法在不使用开关来检测哪个操作系统运行的情况下在两个系统上都能正常工作?

2 个答案:

答案 0 :(得分:0)

cmd.exe是愚蠢的,并从命令中删除了起始和结束引号,但有一个黑客可以击败它:

// theroom information template for target element
var template="";
template += "<div id=\"theroom-info\">";
template += "  <span id=\"theroom-tag\"><\/span>";
template += "  <span id=\"theroom-id\"><\/span>";
template += "  <span id=\"theroom-class\"><\/span>";
template += "<\/div>";
template += "";
template += "<style>";
template += "  #theroom-info {";
template += "    position: fixed;";
template += "    bottom: 0;";
template += "    width: 100%;";
template += "    left: 0;";
template += "    font-family: \"Courier\";";
template += "    background-color: #ffffff;";
template += "    padding: 10px;";
template += "    color: #333333;";
template += "    text-align: center;";
template += "    box-shadow: 0px 4px 20px rgba(0,0,0,0.3);";
template += "  }";
template += "";
template += "  #theroom-tag {";
template += "    color: #C2185B;";
template += "  }";
template += "";
template += "  #theroom-id {";
template += "    color: #5D4037;";
template += "  }";
template += "";
template += "  #theroom-class {";
template += "    color: #607D8B;";
template += "  }";
template += "<\/style>";

var options = {
  template: template,
  showInfo: true
};

// initialize
theRoom.start(options);

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" 调用cmd.exe无论如何都是毫无意义的,只需使用start

start

在引号方面,start "" "%OpenOCD_bin_Path%\openocd.exe" -f "%OpenOCD_bin_Path%\..\scripts\interface\ftdi\SuperDongle.cfg" -f "%OpenOCD_bin_Path%\..\scripts\target\nrf52.cfg" 在所有系统上都是相同的。您通常不会引用环境中设置的路径,因为在需要时很难再删除引号。

您可以删除引号,但理想情况下,由于转义问题,您应该避免在批处理文件中处理字符串操作。

以下是尝试处理这两种情况的示例:

%OpenOCD_bin_Path%

答案 1 :(得分:0)

使用我在评论中提供的任何一个想法都应绕过空格。

使用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"

或者您可以在没有明确启动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

使用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"

或者再次没有明确启动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