bat逐行读取文件

时间:2010-10-01 06:54:30

标签: windows batch-file

在我的bat脚本中,是否可以访问txt文件并逐行读取。我的想法是检查行是否以标识符单词开头(在我的情况下为1或2星***)但为此我需要逐行读取文件

2 个答案:

答案 0 :(得分:2)

你可以使用vbscript

strToFind= WScript.Arguments(0)
strToFind = Replace(strToFind,"*","\*")
strFileName = WScript.Arguments(1)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFile = objFS.OpenTextFile(strFileName)
Set objRE = New RegExp
objRE.IgnoreCase = False
objRE.Pattern = "^"&strToFind&".*"
Do Until objFile.AtEndOfStream    
    strLine = objFile.ReadLine
    Set Matches = objRE.Execute(strLine)
    'WScript.Echo Matches.Count
    For Each Match in Matches   ' Iterate Matches collection.             
        WScript.Echo Match.Value        
    Next        
Loop       
objFile.Close

用法:

C:\test>cscript //nologo myscript.vbs "**" file

答案 1 :(得分:1)

以下是我发现的内容:http://www.computing.net/howtos/show/batch-file-tip-reading-writing-every-line-of-a-file/61.html

希望有所帮助..

CODE:

@echo off
for /f "delims=] tokens=1*" %%a in ('find /v /n "" ^<%1') do (
   echo.%%b
)