這很奇怪: 當textFile是硬編碼時,通過雙擊[輸出(分割)弧文件] 。 將文件拖放到腳本上時,沒有錯誤,但也沒有輸出文件。 當使用拖放操作時,腳本肯定會運行,我在writeTo之前向該行添加了一個簡單的消息框,以確認它確實達到了該點。 雖然在拖放時沒有輸出文件,但只有在textFile被硬編碼時才能正常工作。 有人請幫忙!當變量是參數時,VBS腳本沒有輸出,但當變量被硬編碼時輸出正確
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
謝謝!你是專家,添加父文件夾路徑是最好的選擇。 – BertB
也許它取決於Windows版本,但我在Win8.1 x64 – Wolf
@Wolf上看到當前的工作目錄是'C:\ Windows \ System32',你是對的。現在在Windows 10上測試,我也得到系統目錄。答覆已更改。由於它取決於系統配置,明天我會重新測試在Windows 7中的行爲。謝謝 –