Why oShell.Run not work

后端 未结 2 1067
萌比男神i
萌比男神i 2021-01-07 06:38

This code not work

set oShell = WScript.CreateObject (\"WScript.shell\")
oShell.Run \"%appdata%\\Test.bat\",0,False

But this code work

2条回答
  •  清酒与你
    2021-01-07 07:27

    @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?

提交回复
热议问题