這是來自Filter,一個將stdin過濾爲stdout的程序。這部分是通過在文件的每一行上運行命令行指定的腳本。 Dir /b
給你一個文件列表。
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
RawScript = Arg(1)
'Remove^from quoting command line and replace : with vbcrlf so get line number if error
Script = Replace(RawScript, "^", "")
Script = Replace(Script, "'", chr(34))
Script = Replace(Script, ":", vbcrlf)
'Building the script with predefined statements and the user's code
Script = "Dim gU" & vbcrlf & "Dim gdU" & vbcrlf & "Set gdU = CreateObject(" & chr(34) & "Scripting.Dictionary" & chr(34) & ")" & vbcrlf & "Function UF(L, LC)" & vbcrlf & "Set greU = New RegExp" & vbcrlf & "On Error Resume Next" & vbcrlf & Script & vbcrlf & "End Function" & vbcrlf
'Testing the script for syntax errors
On Error Resume Next
set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)
With ScriptControl1
.Language = "VBScript"
.UseSafeSubset = False
.AllowUI = True
.AddCode Script
End With
With ScriptControl1.Error
If .number <> 0 then
Outp.WriteBlankLines(1)
Outp.WriteLine "User function syntax error"
Outp.WriteLine "=========================="
Outp.WriteBlankLines(1)
Outp.Write NumberScript(Script)
Outp.WriteBlankLines(2)
Outp.WriteLine "Error " & .number & " " & .description
Outp.WriteLine "Line " & .line & " " & "Col " & .column
Exit Sub
End If
End With
ExecuteGlobal(Script)
'Remove the first line as the parameters are the first line
'Line=Inp.readline
Do Until Inp.AtEndOfStream
Line=Inp.readline
LineCount = Inp.Line
temp = UF(Line, LineCount)
If err.number <> 0 then
outp.writeline ""
outp.writeline ""
outp.writeline "User function runtime error"
outp.writeline "==========================="
Outp.WriteBlankLines(1)
Outp.Write NumberScript(Script)
Outp.WriteBlankLines(2)
Outp.WriteLine "Error " & err.number & " " & err.description
Outp.WriteLine "Source " & err.source
Outp.WriteLine "Line number and column not available for runtime errors"
wscript.quit
End If
outp.writeline temp
Loop
一般使用
filter <inputfile >outputfile
filter <inputfile | other_command
other_command | filter >outputfile
other_command | filter | other_command
Vbs
filter vbs "text of a vbs script"
filter vb "text of a vbs script"
使用冒號將單獨的語句和線條。如果您需要單引號,請使用單引號代替雙引號(39)。用^字符轉義括號和&符號。如果你需要使用字母(136)。
該函數被稱爲UF(用於UserFunction)。它有兩個參數,包含當前行的L和包含行數的LC。將腳本的結果設置爲UF。看例子。
有三個全局對象可用。未聲明的全局變量gU維護狀態。如果您需要多個變量,請將其用作數組。用於保存和訪問先前行的Dictionary對象gdU。並準備使用RegExp對象。
例
這vbs腳本插入行號,可設定輸入到函數UF哪個過濾器打印。
filter vbs "uf=LC ^& ' ' ^& L"<"%systemroot%\win.ini"
這是它的外觀在內存
Dim gU
Set gdU = CreateObject("Scripting.Dictionary")
Set greU = New RegExp
Function UF(L, LC)
---from command line---
uf=LC & " " & L
---end from command line---
End Function
如果有語法錯誤過濾器會顯示調試信息。
用戶函數的語法錯誤
1 Dim gU
2 Dim gdU
3 Set greU = CreateObject("Scripting.Dictionary")
4 Function UF(L, LC)
5 On Error Resume Next
6 uf=LC dim & " " & L
7 End Function
Error 1025 Expected end of statement
Line 6 Col 6
用戶功能的運行時錯誤
1 Dim gU
2 Dim gdU
3 Set greU = CreateObject("Scripting.Dictionary")
4 Function UF(L, LC)
5 On Error Resume Next
6 uf=LC/0 & " " & L
7 End Function
Error 11 Division by zero
Source Microsoft VBScript runtime error
Line number and column not available for runtime errors
其它實例
逆向每一行
filter vbs "uf=StrReverse^(L^)"<"%systemroot%\win.ini"
暫時將其添加到document_open。然後使用cmd批處理腳本或其他方式,逐個打開所有單詞文檔。另外,在document_open中添加一個文檔關閉調用。 AFAIK,當你打開單詞,沒有任何文件作爲參數時,document_open宏將不會被調用。因此,您可以在完成後刪除它。您可能想要定義更好的退出條件。 – anishsane 2014-12-11 05:37:18
或者,以更好的方式,您可以使用(在單獨的vbs文件中)createobject函數來創建msword文檔對象。然後逐個打開這些文件,應用這些格式更改並在保存文件後關閉這些文件。 – anishsane 2014-12-11 05:38:45