本文介绍了VBScript当前目录+子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图获取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 $ 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 AppdataPathSet 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 NextEnd If'"# clean upSet oFSO = NothingSet WshShell = NothingSet 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, TrueSince 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.jsChange 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当前目录+子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 20:28