Changing the current directory of a FileSystemObject - vbscript

When using a FileSystemObject you can reference the directory the script was run from by using the path ".". Is it possible to change what the FileSystemObject thinks is the current directory so you can use the "." path syntax for other directories?
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFolder(".") ' Returns the current directory
Set f2 = fso.GetFolder(".\backup") ' Returns the "backup" directory under the current directory
As a simplified example, is there a method to call on fso so that the fso.GetFolder(".") call returns the backup directory instead?

You can change the current working folder for your script using the WshShell.CurrentDirectory property:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = ".\backup"
And here's a Hey, Scripting Guy! article on the subject: How Can I Change the Working Folder of a Script?

Not in general.
But why not find the current folder and store it:
Set fso = CreateObject("Scripting.FileSystemObject")
currentPath = fso.GetAbsolutePathName(".")

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(".")

Trying to copy a file from the current directory to F:\ drive in vbscript

Here's what I have so far:
If E = 1 Then
obj.CopyFile "(CD.CurrentDirectory)" "E:\"
End If
(My WScript variable is CD)
If you don't specify a path the FileSystemObject will look in the current directory.
Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
fso.CopyFile "text.txt", "C:\Users\best buy\Downloads\stackoverflow\"
It worth noting that you can give the new file a different name.
fso.CopyFile "text.txt", "C:\Users\best buy\Downloads\stackoverflow\testing123.txt"

How do I make a vbs script to move a certain file to the desktop if I click on it?

There is a folder in which I have a file, and a shortcut to that file.
I need a VBscript in the same folder which will move the shortcut to the desktop.
If there is any more information you require to help me, I will give it to you.
I tested it and it works for me...
Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim FileName
SourceLocation = "C:\REST-OF-PATH"
DestinationLocation = "%userprofile%\Desktop\"
FileName = "FILENAME"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
ObjFso.MoveFile SourceLocation & "" & FileName, DestinationLocation & ""
Just copy all this...
Then replace REST-OF-PATHwith the rest of the path
And Replace FILENAME with the name of the file

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.

windows shell - how to detect the current location where script file is placed?

I am trying the windows shell file which will be inserted in the folder where it will analyze folders content.
Now i would like to know how can i detect which is the current path ? i.e. location where the vbs file is placed using FileSystemObject?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo objFS.GetParentFolderName(WScript.ScriptFullName)
You can get that from WScript.ScriptFullName. Just remove the filename from the end (the bit after the last backslash). I normally use JScript for scripts, but IIRC VBScript has an InStrRev function that will help you find the last backslash. Or: Create a File object for the WScript.ScriptFullName path and then use its ParentFolder property. Something like (untested):
Dim objFSO
Dim objFile
Dim objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(WScript.ScriptFullName)
Set objFolder = objFile.ParentFolder
To get the full path only without the extension I use Replace(WScript.ScriptFullName, WScript.ScriptName, "") to just result in a filepath

Resources