Vbscript VBS脚本在变量硬编码时运行,但在变量来自参数拖放时不运行

Vbscript VBS脚本在变量硬编码时运行,但在变量来自参数拖放时不运行,vbscript,drag-and-drop,arguments,Vbscript,Drag And Drop,Arguments,我希望有人能帮我解决这个令人沮丧的难题。 该脚本(由网络上的各种位拼凑而成)在双击运行时工作完美,要拆分的文件是硬编码的。 将文件拖放到脚本上时,出现“未找到文件”错误 请帮忙 我尝试了提供的答案,但是脚本运行没有失败,但也没有像硬编码“textFile”值时那样输出这三个文件 if WScript.Arguments.Count <> 0 then textFile = WScript.Arguments(0) else textFile = "multi2.

我希望有人能帮我解决这个令人沮丧的难题。 该脚本(由网络上的各种位拼凑而成)在双击运行时工作完美,要拆分的文件是硬编码的。 将文件拖放到脚本上时,出现“未找到文件”错误 请帮忙

我尝试了提供的答案,但是脚本运行没有失败,但也没有像硬编码“textFile”值时那样输出这三个文件

if WScript.Arguments.Count <> 0 then

    textFile = WScript.Arguments(0)

else

    textFile = "multi2.txt"

end if


saveTo = ""
writeTo = ""
strNewLine = "%_N_"
headingPattern = "(%_N_)"

dim fileFrom, regex, fileTo
Set fso = CreateObject("Scripting.FileSystemObject")

set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp
set fileTo = nothing


with regex
    .Pattern = headingPattern
    .IgnoreCase = false
    .Global = true
end with

while fileFrom.AtEndOfStream <> true
    line = fileFrom.ReadLine
    set matches = regex.Execute(line)
    if matches.Count > 0 then

        strCheckForString = UCase("%")
        strNewLine = "%_N_"

        StrContents = Split(fso.OpenTextFile(textFile).ReadAll, vbNewLine)
        If (Left(UCase(LTrim(line)),Len(strCheckForString)) = strCheckForString) Then
            line = Right(line, len(line)-4)
            line1 = Left(line, len(line)-4)
            writeTo = saveTo & (line1 & ".arc")
            if not(fileTo is nothing) then fileTo.Close()
                set fileTo = fso.CreateTextFile(writeTo)
                fileTo.WriteLine(strNewLine & line)
        else
            fileTo.WriteLine(line)
        End If
    else
        fileTo.WriteLine(line)
    end if
wend

fileFrom.Close()

set fileFrom = nothing
set fso = nothing
set regex = nothing

看起来您的代码应该从第一个参数中提取文件名:

textFile = Right(WScript.Arguments(0), len(WScript.Arguments(0))-44)
然后仅使用文件名打开文件:

set fileFrom = fso.OpenTextFile(textFile)
OpenTextFile
正在当前工作目录下查找相对路径,除非提供了绝对路径。双击脚本运行脚本时,工作目录是启动脚本的文件夹。将文件放到脚本上时,工作目录可能完全不同

如果您的输入文件与脚本位于同一文件夹中,这将解释为什么通过双击启动时它可以工作,但在将文件放到脚本上时却不能工作。在后一种情况下,脚本将在错误的位置查找
multi2.txt

您可以通过在脚本开头添加以下行来验证这一点

WScript.Echo CreateObject("WScript.Shell").CurrentDirectory
WScript.Echo CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
我想你会有两条不同的路

您可以通过不从参数中删除路径来解决此问题。更改此项:

if WScript.Arguments.Count <> 0 then

    textFile = Right(WScript.Arguments(0), len(WScript.Arguments(0))-44)
    'textFile = (chr(34) & textFile & chr(34))

else

    textFile = "multi2.txt"

end if
如果WScript.Arguments.Count为0,则
textFile=Right(WScript.Arguments(0),len(WScript.Arguments(0))-44)
'textFile=(chr(34)&textFile&chr(34))
其他的
textFile=“multi2.txt”
如果结束
为此:

If WScript.Arguments.Count <> 0 Then
    textFile = WScript.Arguments(0)
Else
    textFile = "multi2.txt"
End If
如果WScript.Arguments.Count为0,则
textFile=WScript.Arguments(0)
其他的
textFile=“multi2.txt”
如果结束

代码应该能按预期工作。

谢谢你的回答,它完全有意义。是的,它确实提取了您猜测的文件名。但是,我以前将完整参数设置为textFile,结果是脚本没有输出,没有错误消息或失败。我在“set fileTo=fso.CreateTextFile(writeTo)”之后添加了一个简单的msgbox,以确认脚本正在运行,并且它确实显示了三次(对于三个部分),但没有输出…@user3801088:这是一个不同的问题,因此应该作为一个新问题来问。
If WScript.Arguments.Count <> 0 Then
    textFile = WScript.Arguments(0)
Else
    textFile = "multi2.txt"
End If