VBScript 当前目录 +子目录?

发布于 2024-09-28 01:44:38 字数 1109 浏览 8 评论 0原文

我试图获取 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-10-05 01:44:38

我建议在处理文件时使用 FileSystemObject paths:

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

Edit: 问题出在脚本的这一行:

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

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

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

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

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

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
非要怀念 2024-10-05 01:44:38

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

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

You can get the current directory with :

Set WSHShell = WScript.CreateObject("WScript.Shell")
WScript.Echo WshShell.CurrentDirectory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文