此代码无效
set oShell = WScript.CreateObject ("WScript.shell") oShell.Run "%appdata%\Test.bat",0,False但是此代码有效
set oShell = WScript.CreateObject ("WScript.shell") oShell.Run "C:\Users\User\AppData\Roaming\Test.bat",0,False为什么要使用oShell.Run找不到文件?
Why oShell.Run not find the file ?
推荐答案@ ansgar-wiechers 在 ExpandEnvironmentStrings() ,因为其他一些答案也建议使用它,但是文档很清楚;
@ansgar-wiechers is spot on about the ExpandEnvironmentStrings() as some of the other answers have suggested using it, but the documentation is clear;
来自 MSDN-运行M ethod(Windows脚本宿主) Run方法返回一个整数。 Run方法启动在新的Windows进程中运行的程序。您可以让脚本等待程序完成执行,然后再继续。这使您可以同步运行脚本和程序。 strCommand参数中的环境变量会自动展开。如果文件类型已正确注册到特定程序,则对该类型文件的调用run将执行该程序。例如,如果您的计算机系统上安装了Word,则在* .doc文件上调用运行将启动Word并加载文档。下表列出了intWindowStyle的可用设置。
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%如果此操作不返回结果,则不会产生%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.
AppData 位置在注册表中配置为构成用户配置文件的用户外壳文件夹的一部分。
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注册表中 AppData 项的默认值为:
The default value for the AppData key in the registry is;
%USERPROFILE%\AppData\Roaming无论哪种方式,VBScript都不会出错。
Either way the VBScript is not at fault.
- 什么是PATH和其他环境变量,以及如何设置或使用它们?
- What are PATH and other environment variables, and how can I set or use them?
更多推荐
为什么oShell.Run不起作用
发布评论