作成 2010.01.07
更新 2010.01.07
更新 2010.01.07
VBScript で grep もどき
使い方
フォーマット
cscript grep 検索したい文字列 ファイル名 [-v] [-i]
正規表現も使えますよ
cscript grep 検索したい文字列 ファイル名
文字列を除外
cscript grep 除外したい文字列 ファイル名 -v
大文字小文字無視
cscript grep 検索したい文字列 ファイル名 -i
コード
grep.vbs
Option Explicit
Const ForReading = 1
Dim FSO, myReg
Dim myRead, myLine
Dim strFileName, strPattern
Dim isIgnoreCase, isIgnorePattern
Dim counter
Set FSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.length < 2 Then
WScript.Echo "Usage: cscript grep.vbs regexp file [-v] [-i]"
WScript.Quit
End If
isIgnoreCase = False
isIgnorePattern = False
strPattern = ""
strFileName = ""
For counter = 0 To WScript.Arguments.length - 1
If WScript.Arguments(counter) = "-i" Then
isIgnoreCase = True
ElseIf WScript.Arguments(counter) = "-v" Then
isIgnorePattern = True
ElseIf Len(strPattern) = 0 Then
strPattern = WScript.Arguments(counter)
Else
strFileName = WScript.Arguments(counter)
End If
Next
Set myReg = new RegExp
myReg.Pattern = strPattern
myReg.Global = False
myReg.IgnoreCase = isIgnoreCase
If Not FSO.FileExists(strFileName) Then
WScript.Echo "File not found. '" & strFileName & "'"
End If
Set myRead = FSO.OpenTextFile(strFileName, ForReading)
Do While myRead.AtEndOfStream = False
myLine = myRead.ReadLine
If myReg.Test(myLine) Then
If Not isIgnorePattern Then WScript.Echo myLine
Else
If isIgnorePattern Then WScript.Echo myLine
End If
Loop
myRead.Close
タグ: VBScript