2
votes

I am trying to get the path of a file that is within a sub-directory of the current directory in VBScript. The following does not seem to work?

currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
FileToCopy = currentDirectory & "\test\user.js"

Here is the entire code:

Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "unproxy\user.js")

''# get AppdataPath
Set WshShell = CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")

AppdataPath = WshSysEnv("APPDATA") 

FoxProfilePath = AppdataPath & "\Mozilla\Firefox\Profiles\"

'"# is firefox and user.js present?
if oFSO.FolderExists(FoxProfilePath) AND oFSO.FileExists(FileToCopy) Then

''# copy user.js in all profilefolders to get around those random profile names =)
    For Each ProfileFolder In oFSO.GetFolder(FoxProfilePath).Subfolders
        oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True
    Next
End If

'"# clean up
Set oFSO = Nothing
Set WshShell = Nothing
Set WshSysEnv = Nothing
2
Can you elaborate on what you mean by "does not seem to work"? Do you get an error or an incorrect result? And what is the file name of your VBScript script? - Helen
I don't get an error, nothing seems to happen - the file does not copy because I am guessing it's not able to locate the user.js file. - Romulus
The file name is copyFile.vbs - Romulus
Post the entire code you're using. - Helen
Have posted entire code. - Romulus

2 Answers

3
votes

I recommend using FileSystemObject when dealing with file paths:

Set oFSO = CreateObject("Scripting.FileSystemObject")
strFolder = oFSO.GetParentFolderName(WScript.ScriptFullName)
FileToCopy = oFSO.BuildPath(strFolder, "test\user.js")

Edit: The problem is in this line of your script:

oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\" & FileToCopy, True

Since FileToCopy contains a full file name, when you concatenate it with ProfileFolder you get an invalid file name, like this:

C:\Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\mlreq6kv.default\D:\unproxy\user.js

Change this line to the one below, and your script should work fine. (Note: the trailing path separator at the end of ProfileFolder is required to indicate that the profile folder, e.g. mlreq6kv.default, is indeed a folder and not a file.)

oFSO.GetFile(FileToCopy).Copy ProfileFolder & "\", True
1
votes

You can get the current directory with :

Set WSHShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory