问题
This code not work
set oShell = WScript.CreateObject ("WScript.shell")
oShell.Run "%appdata%\Test.bat",0,False
But this code work
set oShell = WScript.CreateObject ("WScript.shell")
oShell.Run "C:\Users\User\AppData\Roaming\Test.bat",0,False
Why oShell.Run not find the file ?
回答1:
@ansgar-wiechers is spot on about the ExpandEnvironmentStrings() as some of the other answers have suggested using it, but the documentation is clear;
From MSDN - Run Method (Windows Script Host)
The Run method returns an integer. The Run method starts a program running in a new Windows process. You can have your script wait for the program to finish execution before continuing. This allows you to run scripts and programs synchronously. Environment variables within the argument strCommand are automatically expanded. If a file type has been properly registered to a particular program, calling run on a file of that type executes the program. For example, if Word is installed on your computer system, calling Run on a *.doc file starts Word and loads the document. The following table lists the available settings for intWindowStyle.
If you are having problems using environment variables in your code, it's likely they have been remapped probably by a login script or policy. You can test this by typing the following at a command prompt;
echo %appdata%
If this returns nothing or not what you expect the %appdata%
environment variable has been remapped.
To show you how easy it is to remap the value from a command prompt
>set appdata
APPDATA=C:\Users\Example.Profile\AppData\Roaming
>set appdata=c:\
>echo %appdata%
C:\
Obviously you can reverse this again to correct the issue;
>set appdata=C:\Users\Example.Profile\AppData\Roaming
>echo %appdata%
C:\Users\Example.Profile\AppData\Roaming
Disclaimer: These are just examples of changing the environment variables via the command prompt, this does not change global environment variables and the changes only affect the current instance of the command prompt. To do this you have to modify the registry via Registry Editor, Group Policy etc or use the System Properties screen in Control Panel.
The AppData
location is configured in the registry as part of the Users Shell Folders that make up the user profile.
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
The default value for the AppData
key in the registry is;
%USERPROFILE%\AppData\Roaming
Either way the VBScript is not at fault.
Useful Links
- What are PATH and other environment variables, and how can I set or use them?
回答2:
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "WinDir is " & WshShell.ExpandEnvironmentStrings("%WinDir%\system32")
And running it
C:\Users\User>cscript "C:\Users\User\Desktop\New Text Document.vbs"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
WinDir is C:\Windows\system32
If it doesn't work there ask at www.superuser.com why your system is misconfigured.
来源:https://stackoverflow.com/questions/38426486/why-oshell-run-not-work