Vbscript 在vbs中将Windows安装目录用作变量时,Windows找不到指定的路径

Vbscript 在vbs中将Windows安装目录用作变量时,Windows找不到指定的路径,vbscript,Vbscript,使用%windir%位置作为变量时,Windows找不到指定的路径。还有,有人知道64位计算机是否有system 3s文件夹吗?我知道这个变量可以工作,因为我在messagebox中使用过它。这是我的代码,如果可以请帮忙 set shell = WScript.CreateObject("WScript.Shell") windowsdir = shell.ExpandEnvironmentStrings("%windir%") MsgBox(windowsdir) Dim objShel

使用%windir%位置作为变量时,Windows找不到指定的路径。还有,有人知道64位计算机是否有system 3s文件夹吗?我知道这个变量可以工作,因为我在messagebox中使用过它。这是我的代码,如果可以请帮忙

set shell = WScript.CreateObject("WScript.Shell")

windowsdir = shell.ExpandEnvironmentStrings("%windir%")

MsgBox(windowsdir)

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""windowsdir & \System32\dvdplay.exe""")
Set objShell = Nothing
dim filesys, newfolder, newfolderpath
Dim oShell : Set oShell = CreateObject("WScript.Shell")
Dim sWinDir : sWinDir = oShell.ExpandEnvironmentStrings("%windir%")
sCmd = """" & sWinDir & "\System32\test"""
WScript.Echo 3, sCmd, "'concatenation' solution"

newfolderpath = """"& sCmd& ""
set filesys=CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(newfolderpath) Then
Set newfolder = filesys.CreateFolder(newfolderpath)
End If
很抱歉,我正在尝试使用相同的方法创建文件夹,现在我收到一个“错误的文件名或编号”错误。如果可以的话,请帮忙

set shell = WScript.CreateObject("WScript.Shell")

windowsdir = shell.ExpandEnvironmentStrings("%windir%")

MsgBox(windowsdir)

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""windowsdir & \System32\dvdplay.exe""")
Set objShell = Nothing
dim filesys, newfolder, newfolderpath
Dim oShell : Set oShell = CreateObject("WScript.Shell")
Dim sWinDir : sWinDir = oShell.ExpandEnvironmentStrings("%windir%")
sCmd = """" & sWinDir & "\System32\test"""
WScript.Echo 3, sCmd, "'concatenation' solution"

newfolderpath = """"& sCmd& ""
set filesys=CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(newfolderpath) Then
Set newfolder = filesys.CreateFolder(newfolderpath)
End If

使用.Run或.Exec时,应使用临时变量存储和显示命令(至少在“它工作”之前):

输出:

cscript 19367777.vbs
0 C:\WINDOWS
1 "windowsdir & \System32\dvdplay.exe" no variable interpolation in VBScript
2 "C:\WINDOWS\System32\dvdplay.exe" 'no concatenation' solution
3 "C:\WINDOWS\System32\dvdplay.exe" 'concatenation' solution

由于VBScript不会将变量内容拼接成字符串文字,因此您必须将这些部分连接起来(在我看来,这是第二个最好的解决方案)或将.expandEnvironmentString()应用于包含
%windir%

的命令。在32位窗口上,您只有System32

在64位windows上,您有:

  • System32(包含64位库)
  • SysWOW64(具有在“Windows on Windows 64”仿真层中运行的32位库)
我知道,这让人困惑

在这两种体系结构上,
%WINDIR%
应该是相同的。如果您只在64位系统上运行脚本,那么只需将System32更改为SysWOW64就可以了

在32和64位上运行脚本时,您可能需要注意的是:“哪个脚本主机与.vbs文件关联?”通常,如果双击64位上的vbs,解释器将是:
C:\Windows\System32\WScript.exe
(即64位主机),这意味着,例如
%PROGRAMFILES%
扩展为
C:\PROGRAMFILES
。在
c:\Windows\SysWOW64\WScript.exe
(32位)中展开相同的变量,您将得到
c:\Program Files(x86)


很长一段时间以来,我一直使用框架代码,允许脚本在32位主机中“自行重新运行”,只是为了实现交叉兼容性。

我注意到,有时在更新Visual Studio或SQL Server时,例如,
路径有点太满,或者
%systemroot%
条目被“下推”因为有新的条目

如果您的系统找不到
%windir%
,请查看您的系统
路径
,并确保前几个条目如下所示:

%SystemRoot%;%SystemRoot%\system32;   ...etc
然后确保系统变量
windir
设置为
C:\Windows


您可能需要重新启动。这对我起到了作用。

谢谢你的帮助,工作得很好,解释得也很好。
Run
Exec
方法都会扩展环境变量,所以类似于
oShell.Run“%windir%\System32\dvdplay.exe”之类的方法会很好地工作。谢谢。我同意这令人困惑。我将在不同的计算机上使用VBScript进行实验,以查看结果。请不要重新定义原始问题,而是发布一个新问题(可能参考前一个问题)。您当前的问题是:虽然shell需要引号来正确解析包含空格的filespec,但使用file/folderspec作为参数的FSO方法知道,带有空格的spec仍然只是一个spec。因此,不要对此类spec使用引号。