0

Windows cannot find the path specified when using the %windir% location as a variable. Also, does anyone know if 64 bit computers have system 3s folders? I know that the variable works because i have used it in a messagebox. Here is my code, please help if you can.

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

Sorry, i am trying to use this same method to create a folder and now i am getting a "bad file name or number" error. Please help if you can.

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
4

3 回答 3

1

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

Option Explicit
Dim oShell : Set oShell = CreateObject("WScript.Shell")
WScript.Echo 0, oShell.ExpandEnvironmentStrings("%windir%")
Dim sCmd
sCmd = """windowsdir & \System32\dvdplay.exe"""
WScript.Echo 1, sCmd, "no variable interpolation in VBScript"
sCmd = oShell.ExpandEnvironmentStrings("""%windir%\System32\dvdplay.exe""")
WScript.Echo 2, sCmd, "'no concatenation' solution"
Dim sWinDir : sWinDir = oShell.ExpandEnvironmentStrings("%windir%")
sCmd = """" & sWinDir & "\System32\dvdplay.exe"""
WScript.Echo 3, sCmd, "'concatenation' solution"

输出:

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 不会将变量内容拼接到字符串文字中,因此您必须将这些部分连接起来(在我看来,这是第二好的解决方案)或将 .ExpandEnvironmentStrings() 应用于包含%windir%.

于 2013-10-14T20:02:47.480 回答
0

在 32 位 Windows 上,您只有 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:\Program Files. c:\Windows\SysWOW64\WScript.exe在(32位)中展开相同的变量,你得到C:\Program Files (x86)

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

于 2013-10-15T09:42:22.960 回答
0

我注意到有时在更新 Visual Studio 或 SQL Server 时,例如,Path会变得有点太满,或者%systemroot%条目可能因为新条目而被“下推”。

如果您的系统找不到%windir%,请查看您的系统Path,并确保几个条目是这样的:

%SystemRoot%;%SystemRoot%\system32;   ...etc

然后确保系统变量windir设置为C:\Windows

您可能需要重新启动。这对我有用。

于 2015-05-13T20:59:52.777 回答