VBScript current directory + sub directory?

梦想与她 提交于 2019-12-10 23:34:32

问题


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

回答1:


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



回答2:


You can get the current directory with :

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


来源:https://stackoverflow.com/questions/3972814/vbscript-current-directory-sub-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!