windows VBScript 当前目录+子目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3972814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:26:20  来源:igfitidea点击:

VBScript current directory + sub directory?

windowsscriptingfilevbscriptworking-directory

提问by Romulus

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?

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

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

回答by Helen

I recommend using FileSystemObjectwhen dealing with file paths:

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

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 FileToCopycontains a full file name, when you concatenate it with ProfileFolderyou get an invalid file name, like this:

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

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

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 ProfileFolderis required to indicate that the profile folder, e.g. mlreq6kv.default, is indeed a folder and not a file.)

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

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

回答by RealHowTo

You can get the current directory with :

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

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