dlanod
dlanod

Reputation: 9020

Changing the current directory of a FileSystemObject

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?

Upvotes: 12

Views: 33629

Answers (2)

Helen
Helen

Reputation: 98072

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?

Upvotes: 19

Mitch Wheat
Mitch Wheat

Reputation: 300827

Not in general.

But why not find the current folder and store it:

Set fso = CreateObject("Scripting.FileSystemObject")
currentPath = fso.GetAbsolutePathName(".")

Upvotes: 4

Related Questions