VBScript当前目录+子目录?

时间:2010-10-19 21:13:03

标签: windows scripting file vbscript working-directory

我正在尝试获取VBScript中当前目录的子目录中的文件路径。以下似乎不起作用?

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

以下是整个代码:

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 个答案:

答案 0 :(得分:3)

我建议在处理文件路径时使用FileSystemObject

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

编辑:问题出在您的脚本的这一行:

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

由于FileToCopy包含完整的文件名,因此当您将其与ProfileFolder连接时,会收到无效的文件名,如下所示:

  

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

将此行更改为下面的行,您的脚本应该可以正常工作。 (注意:ProfileFolder末尾的尾随路径分隔符需要指示配置文件夹,例如 mlreq6kv.default ,确实是文件夹而不是文件。)

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

答案 1 :(得分:1)

您可以使用以下命令获取当前目录:

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