我希望那里的一些大脑可以为我解决这个令人沮丧的难题。 脚本(从网上各种位拼凑而成)在双击运行时效果很好,要分割的文件是硬编码的。 将文件拖放到脚本上时,出现“找不到文件”的错误。 请帮忙!
我已经尝试了所提供的答案,但随后脚本运行没有失败,但也没有输出三个文件,因为它们是&text;文本文件'价值是硬编码的。
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
文本文件如下所示:
%_N_160_SP01_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
%_N_160_SP02_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
%_N_160_SP99_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
答案 0 :(得分:1)
看起来您的代码应该从第一个参数中提取文件名:
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
进入这个:
If WScript.Arguments.Count <> 0 Then
textFile = WScript.Arguments(0)
Else
textFile = "multi2.txt"
End If
并且代码应该按预期工作。