Read ONLY x lines from a txt file with a windows batch file

前端 未结 2 1938
小蘑菇
小蘑菇 2021-01-24 05:20

How can I read only X lines from a a.txt file?

The file contains all the names of a directory, I would like to read only x lines. X can be a number that can varies from

2条回答
  •  长情又很酷
    2021-01-24 06:21

    you can use vbscript. Here's an example

    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objArgs = WScript.Arguments
    strNum = objArgs(0)
    strFile=objArgs(1)
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfLine
        If CInt(objFile.Line) > CInt(strNum) Then
          Exit Do  
        End If 
        strLine=objFile.ReadLine
        WScript.Echo strLine    
    Loop
    

    save as myscript.vbs and

    c:\test> cscript //nologo myscript.vbs 99 file
    

    Or if have the luxury to install tools, you can download sed or gawk for windows . Then on the command line

    sed.exe "99q" file
    gawk.exe "NR>2{exit}1" file
    

提交回复
热议问题