为什么oShell.Run不起作用

编程入门 行业动态 更新时间:2024-10-27 00:33:37
本文介绍了为什么oShell.Run不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此代码无效

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不起作用

本文发布于:2023-11-24 11:11:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624968.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   oShell   Run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!