How do I use spaces with my objShell.CurrentDirectory code? - windows

How can I fix this code so it works correctly? I think it has something to do with the spaces in "Start Menu". The problem comes when I run the file in another drive. For example my P: Drive
Set objShell = WScript.CreateObject("WScript.Shell")
File = "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
objShell.CurrentDirectory = File

The environment variables can't be just notated in the path string. You have to use the Shell object method below (ExpandEnv...) to obtain the Environment variable.
This should do what I assume you're trying to do to-date.
Set objShell = WScript.CreateObject("WScript.Shell"): appdata = objShell.ExpandEnvironmentStrings("%appdata%")
File = appdata & "\Microsoft\Windows\Start Menu\Programs\Startup"
objShell.CurrentDirectory = File
Wscript.Echo objShell.CurrentDirectory

Related

Why doesn't WScript.Shell.ExpandEnvironmentStrings work with %CD%?

In the command line you can output the current directory using echo %CD% like this this:
The Windows Scripting Host provides the ExpandEnvironmentalStrings method which can be used like this:
Dim objWshShell : Set objWshShell = CreateObject("Wscript.Shell")
MsgBox objWshShell.ExpandEnvironmentStrings("%WINDIR%")
However, it doesn't work with %CD%. It just returns the same value, %CD%:
Dim objWshShell : Set objWshShell = CreateObject("Wscript.Shell")
MsgBox objWshShell.ExpandEnvironmentStrings("%CD%")
Why doesn't this work? I know there are other ways to get the current directory; this is just a curiousity.
The variable %CD% is a CMD-builtin automatic variable, not an environment variable like %PATH% or %USERNAME%. It can only be used within CMD, e.g.
cmd /c echo %CD%
Same goes for the variables %TIME%, %DATE%, and %ERRORLEVEL%.
If you want the current working directory in VBScript you need to use the CurrentDirectory property of the WshShell object
Set sh = CreateObject("WScript.Shell")
WScript.Echo sh.CurrentDirectory
or expand the path of the directory .:
Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.GetAbsolutePathName(".")

Which environment variable to use for filepath?

I want to assign a variable to a filepath located in the 'Users' folder in the C: Drive. This filepath is different for each user, but I can't figure out exactly which environment variable to use, and how to use it with VBScript.
Example Code:
Set wshShell = CreateObject("WScript.Shell")
Set wshSystemEnv = wshShell.Environment("USER")
'The folder where to save the file:
strFolder = "C:\Users\" & wshSystemEnv & "\AppData\Roaming\Microsoft\AddIns"
So how do I use the wshSystemEnv variable in the filepath, and is it even the right variable?
The error that pops up for the strFolder line is
Wrong number of arguments or invalid property assignment
Use the %APPDATA% environment variable:
Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
appData = wshShell.ExpandEnvironmentStrings("%APPDATA%")
strFolder = fso.BuildPath(appData, "Microsoft\AddIns")
Always build your paths using the BuildPath method.
The error you're getting from your code is because wshSystemEnv is an object and cannot be concatenated with strings like that.
The environment variable %APPDATA% traditionally points to the application data folder in the user's (roaming) profile. Since Windows Vista Microsoft split that folder into three subfolders only one of which remains part of roaming profiles (AppData\Roaming). The other 2 remain local mainly for synchronization performance reasons.
The reason why %APPDATA% points to the Roaming subfolder instead of %USERPROFILE%\AppData is most likely that having it point to the parent folder would have required a lot of application vendors to release updates with modified paths due to the additional level of hierarchy (%APPDATA%\application\foo to %APPDATA%\Roaming\application\foo).
The appropriate environment variable is "USERNAME".
Set wshShell = CreateObject("WScript.Shell")
wshSystemEnv = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
strFolder = "C:\Users\" & wshSystemEnv & "\AppData\Roaming\Microsoft\AddIns"
or you can also use:
wshSystemEnv = wshShell.Environment("PROCESS").Item("USERNAME")
Just Google VBScript environment variable and you'll get lots of examples and references.

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

How to Create Folders and Subfolders in %AppData%

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months.
I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same time overwrite that folder and file if they already exist
Folder and subfolders to be created (Avaya) C:\Users\My Username\AppData\Roaming\Avaya\ Avaya\one-X Agent\2.5\
File from (Myfile.txt) C:\Myfile.txt to C:\Users\My Username\AppData\Roaming\Avaya\one-X Agent\2.5\
I get “Path not found” error, but If I leave the path till (Avaya) it creates Avaya Folder but not it’s subfolders C:\Users\My Username\AppData\Roaming\Avaya\
Here’s what I have and thank you in advance
Dim fso, vFolder
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
usrName = objWShell.expandEnvironmentStrings("%USERNAME%")
Set fso = CreateObject("Scripting.FileSystemObject")
Set vFolder = fso.CreateFolder("C:\Users\" & usrName & "\AppData\Roaming\Avaya\one-X Agent\2.5\")
CreateFolderDemo = vFolder.Path
The problem is that CreateFolder does not create intermediate folders. The FSO doesn't have a method that does that. It might be easier to use mkdir like this:
Option Explicit
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.Run("%COMSPEC% /c mkdir ""%APPDATA%\Avaya\one-X Agent\2.5""",0,true)
Some errors:
You declare fso, but you use objFso
You use %USERNAME% but you should consider %APPDATA% instead
You should use OPTION EXPLICIT to detect typos and undefined variables
You should make your code easier to read by dimming one variable at a time
CreateFolder doesn't create the entire tree, so you need to use FolderExists
For example:
Option Explicit
Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim appData
appData = objWShell.expandEnvironmentStrings("%APPDATA%")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(appData + "\Avaya") Then
objFso.CreateFolder appData + "\Avaya"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent\2.5") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent\2.5"
End If
Lastly, it's not clear why your solution needs to be in VBScript. It appears that your requirements are creating folders and copying files, which means, batch files would probably be a far more simpler.

Launch programs whose path contains spaces

I need to launch programs in my local system using VBScript. But I am having trouble with the syntax. This is what I am using right now -
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("iexplore")
Set objShell = Nothing
The above code successfully launches IE. I want to launch other browsers. But, if I use -
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("c:\Program Files\Mozilla Firefox\firefox.exe")
Set objShell = Nothing
it is throwing an error saying that the file or path was not found. I am not sure how the parameter inside the Run() function is taken - should I give the path to an EXE or should I give some DOS commands?!
Try:-
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("""c:\Program Files\Mozilla Firefox\firefox.exe""")
Set objShell = Nothing
Note the extra ""s in the string. Since the path to the exe contains spaces it needs to be contained with in quotes. (In this case simply using "firefox.exe" would work).
Also bear in mind that many programs exist in the c:\Program Files (x86) folder on 64 bit versions of Windows.
You van use Exec
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Exec("c:\Program Files\Mozilla Firefox\firefox.exe")
Set objShell = Nothing
It's working with
Set WSHELL = CreateObject("Wscript.Shell")
WSHELL.Exec("Application_Path")
But what should be the parameter in case we want to enter the application name only
e.g in case of Internet Explorer
WSHELL.Run("iexplore")
set shell=CreateObject("Shell.Application")
' shell.ShellExecute "application", "arguments", "path", "verb", window
shell.ShellExecute "slipery.bat",,"C:\Users\anthony\Desktop\dvx", "runas", 1
set shell=nothing
find an .exe file for the application you want to run
example iexplore.exe and firefox.exe and remove .exe
and use it in objShell.Run("firefox")
I hope this helps.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("firefox")
Set objShell = Nothing
Please try this
Copy the folder, firefox.exe is in and place in the c:\ only. The script is having a hard time climbing your file tree. I found that when I placed the *.exe file in the c:\ it eliminated the error message " file not found."

Resources