1

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 ?

checkmate
  • 47
  • 6
  • 1
    You haven't passed a file path. See oShell.ExpandEnvironmentStrings. So `oShell.Run oshell.ExpandEnvironmentStrings("%appdata%\Test.bat"),0,False` for a one liner. –  Jul 17 '16 at 22:30
  • 1
    Check the value of `%APPDATA%`: `oShell.Run "cmd /k echo %APPDATA%", 1, True` – Ansgar Wiechers Jul 18 '16 at 08:41

2 Answers2

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

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • This code work fine set oShell = CreateObject ("WScript.shell") AppData = oshell.ExpandEnvironmentStrings("%appdata%") wscript.echo Appdata oshell.run(Appdata & "\Test.bat"),0,False but when windows start show this message: Windows Script Host C:\Users\User\AppData\Roaming – checkmate Jul 20 '16 at 02:07
  • @checkmate not sure what you are trying to achieve but what does `WScript.Echo Appdata & "\Test.bat"` display as?, should be `C:\Users\User\AppData\Roaming\Test.bat`. – user692942 Jul 20 '16 at 07:18
  • But i want access by %appdata% not C:\Users\User, the path change in others machines – checkmate Jul 21 '16 at 02:33
  • @checkmate I'm sorry I can't help, I've tried to explain how to test the `%appdata%` environment variable but you keep contradicting yourself and seem confused about what `%appdata` gives you. So I'm stepping away from this question. Good luck. – user692942 Jul 21 '16 at 06:48
0
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.

  • You don't need `ExpandEnvironmentStrings()` as `Run()` expands them automatically. The suggestion to try a different environment variable though is a good one as it's likely `%appdata%` has been re-mapped and others are fine. – user692942 Jul 18 '16 at 10:26