可能的 Windows 8 cmd 更改?

Mat*_*ker 5 powershell command-line windows-8

出于某种原因,我决定在我的开发机器上试用 Windows 8。到目前为止,一切都很好,直到我尝试启动具有一些自定义功能的 Powershell,包括PATHvcvars32.bat.

最初,脚本是从这里提取的/sf/ask/9670111/进行了一些更改以允许它在 x64 Powershell 实例中运行,所以它最终看起来像这样:

function Get-Batchfile($file) 
{
    $theCmd = "`"$file`" & set" 
    cmd /c $theCmd | Foreach-Object {
        $thePath, $theValue = $_.split('=')
        Set-Item -path env:$thePath -value $theValue
    }
}

function VsVars32($version = "10.0")
{
    $theKey = "HKLM:SOFTWARE\Wow6432Node\Microsoft\VisualStudio\" + $version
    $theVsKey = get-ItemProperty $theKey
    $theVsInstallPath = [System.IO.Path]::GetDirectoryName($theVsKey.InstallDir)
    $theVsToolsDir = [System.IO.Path]::GetDirectoryName($theVsInstallPath)
    $theVsToolsDir = [System.IO.Path]::Combine($theVsToolsDir, "Tools")
    $theBatchFile = [System.IO.Path]::Combine($theVsToolsDir, "vsvars32.bat")
    write-host $theBatchFile
    Get-Batchfile $theBatchFile
    [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell"
}

VsVars32
Run Code Online (Sandbox Code Playgroud)

它在 Windows 7 下工作得很好。现在,在 Windows 8 下,我得到以下回复:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

经过进一步检查,似乎cmd /c工作方式发生了一些变化。由于只是单独运行该cmd行,结果如下:

PS> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

有没有人遇到过类似的问题,希望有解决方法?

编辑:

正如下面的回答中提到的,可能存在引用问题。我什至在发布之前就尝试过这个,并且因为我的麻烦得到了这个:

PS> cmd /c ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat""
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:28
+ cmd /c ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvar ...
+                            ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

引用的几种不同排列:

双引号包裹在单引号中:

PS> echo '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"'
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"

PS> cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"'
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

转义双引号:

PS> echo "`"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat`""
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"

PS> cmd /c "`"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat`""
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

再一次:完全相同的脚本在 Windows 7 下工作。

agi*_*ish 1

如果您使用的是 Windows Power Shell,并且路径中包含空格,则必须使用“”(双双引号)。

""C:\Program Files""
Run Code Online (Sandbox Code Playgroud)

示例:cmd /c ""start cd 'C:\Program Files'""打开一个新的命令提示符,当前目录为C:\Program Files. 如果它没有用双引号括起来,则会由于包含空格“”而引发错误。

编辑:它也适用于双单引号。

编辑:您可以选择使用 ` 字符(反引号/反勾)转义空格。

cmd /c start cd C:\Program` Files
Run Code Online (Sandbox Code Playgroud)