VBScript当前目录+子目录? [英] VBScript current directory + sub directory?
问题描述
我试图获取VBScript中当前目录的子目录内的文件的路径。以下似乎并不奏效?
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName)) - ( len(WScript.ScriptName)))
FileToCopy = currentDirectory& \test\user.js
以下是整个代码:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ FileToCopy = oFSO.BuildPath(strFolder,unproxy \ user.js)
$ b $''#get AppdataPath
Set WshShell = CreateObject(WScript.Shell)
Set WshSysEnv = WshShell.Environment(PROCESS)
AppdataPath = WshSysEnv(APPDATA)
FoxProfilePath = AppdataPath& \Mozilla\Firefox\Profiles\
$ b $#是firefox和user.js目前的?
如果oFSO.FolderExists(FoxProfilePath)和oFSO.FileExists(FileToCopy)然后
''#在所有profilefolders中复制user.js以获得随机配置文件名称=)
对于每个ProfileFolder在oFSO.GetFolder(FoxProfilePath).Subfolders
oFSO。 GetFile(FileToCopy).Copy ProfileFolder&\& FileToCopy,True
Next
End If
'#清理
Set oFSO = Nothing
Set WshShell = Nothing
Set WshSysEnv = Nothing
FileSystemObject $ c当处理文件路径时:$ b>
$ b 设置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
连接时,你会得到一个无效的文件名,像这样:
\ Documents and Settings\username\Application Data\Mozilla\Firefox\Profiles\mlreq6kv.default\D:\unproxy\user.js
将这一行更改为下面的代码,您的脚本应该可以正常工作。 (注意: ProfileFolder
结尾处的尾随路径分隔符需要指明profile文件夹,例如 mlreq6kv.default 确实是一个文件夹而不是文件。)
oFSO.GetFile(FileToCopy).Copy ProfileFolder& \,True
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
解决方案 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
这篇关于VBScript当前目录+子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!