start line - end line to output file (BAT script)

Page 1 of 2 12 LastLast
  1. Feuer's Avatar
    Posts : 311
    Windows 10 Home Single Language 20H2
       #1

    start line - end line to output file (BAT script)


    input.txt
    it is a text file with 10 lines in it
    one
    two
    three
    four
    five
    six
    seven
    eight
    nine
    ten
    i want to create a text file from line 3 to 7
    it will be output.txt

    three
    four
    five
    six
    seven
    i want to know, how i can create a file that getting lines from another file with start line and end line arguments with a batch script?

    it was just an example
    i have a file with over 200 lines in it, i want to get 54 lines from it and make an output file

    more command; you can skip lines so you can set a start line, i will skip 2 lines with this command, the output file starts with 3rd line, but i do not know how i can give an end line

    more /e +2 input.txt > output.txt

    three
    four
    five
    six
    seven
    eight
    nine
    ten
      My Computer

  2. Try3's Avatar
    Posts : 8,314
    Windows 10 Home x64 Version 21H1 Build 19043.1052
       #2

    To do it in a batch file you'd use a combination of the For command, a subroutine and a counter.

    This will get you started -
    For - SS64

    Denis
      My Computer

  3. Feuer's Avatar
    Posts : 311
    Windows 10 Home Single Language 20H2
    Thread Starter
       #3

    thanks @Try3, i know the site, i am trying to understand them
      My Computer

  4. Try3's Avatar
    Posts : 8,314
    Windows 10 Home x64 Version 21H1 Build 19043.1052
       #4

    For-SimpleFileReading


    Just to get you started, here is a demonstration batch file & a text file for it to read.
    Simple example.zip
    Unzip the two files to the same folder and just run the batch file. It will echo each line in the text file.

    The batch file contents are [For-SimpleFileReading.bat]
    Code:
    prompt $g
    
    :: Change folders to the one the batch file is in just to avoid, in this demo, having to set a full path for SourceFile
    cd /d ~dp0
    
    Set SourceFile=FileToRead.txt
    For /f  "usebackq tokens=*" %%X in ("%SourceFile%") do Call :Processing "%%X"
    GoTo SimpleFileReadingDemo
    
    :Processing 
    set ThisParameter=%*
    ::Now strip out the " that were added in passing the lines and that leaves each original line in turn
    :: Protect each stage of processing and displaying variables with " so that special characters, such as &, do not cause it to fail
    set "ThisParameter=%ThisParameter:~1%"
    set "ThisParameter=%ThisParameter:~0,-1%"
    :: Any processing of the read line takes place here.  The echo is just used as an example
    echo "%ThisParameter%"
    GoTo :eof
    
    :SimpleFileReadingDemo
    Pause during testing

    For is such a sophisticated & powerful command that it takes a lot of studying and a lot of getting used to.

    Denis
      My Computer

  5. Try3's Avatar
    Posts : 8,314
    Windows 10 Home x64 Version 21H1 Build 19043.1052
       #5

    For-SimpleFileReadingWithCounter


    And here is a slightly more complicated example that includes using a line counter within the For looping
    Simple example with line counter.zip

    The batch file contents are [For-SimpleFileReadingWithCounter.bat]
    Code:
    prompt $g
    
    :: Change folders to the one the batch file is in just to avoid, in this demo, having to set a full path for SourceFile
    cd /d ~dp0
    
    ::Set up a counter for use in the For looping
    Set /a LineCounter=0
    
    Set SourceFile=FileToRead.txt
    For /f  "usebackq tokens=*" %%X in ("%SourceFile%") do Call :Processing "%%X"
    GoTo SimpleFileReadingDemo
    
    :Processing 
    :: Increment the LineCounter
    Set /a LineCounter=%LineCounter%+1
    :: Process the passed line
    set ThisParameter=%*
    ::Now strip out the " that were added in passing the lines and that leaves each original line in turn
    :: Protect each stage of processing and displaying variables with " so that special characters, such as &, do not cause it to fail
    set "ThisParameter=%ThisParameter:~1%"
    set "ThisParameter=%ThisParameter:~0,-1%"
    :: Any processing of the read line takes place here.  The echo is just used as an example
    echo Line number %LineCounter% is - "%ThisParameter%"
    GoTo :eof
    
    :SimpleFileReadingDemo
    Pause during testing

    To take specific action on particular line numbers will require use of If commands.
    If - SS64

    Denis
      My Computer

  6. Matthew Wai's Avatar
    Posts : 5,696
    Windows 10 Home 20H2
       #6

    In case the OP is interested, the following is a batch file actually used in the BSOD section.

    Batch files for use in BSOD debugging

    Feuer said:
    i am trying to understand them
    I have used the For, Skip, and token commands in the above file without really understanding them. I have created the file by trial and error.
      My Computer

  7. Feuer's Avatar
    Posts : 311
    Windows 10 Home Single Language 20H2
    Thread Starter
       #7

    @Matthew Wai @Try3 thanks for your replies, i will try them at night,

    i have also found a created bat file working with command like that,
    xxxx.bat input.txt -startline -endingline output.txt, trying to understand how it works
    i will share it too here
      My Computer

  8. Paul Black's Avatar
    Posts : 12,961
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #8

    Hello @Feuer,

    Feuer said:
    I have also found a created bat file working with command like that,
    xxxx.bat input.txt -startline -endingline output.txt, trying to understand how it works.
    I will share it too here
    Yes, if you can post the code we can have a look at it.
      My Computer

  9. Matthew Wai's Avatar
    Posts : 5,696
    Windows 10 Home 20H2
       #9

    Yes, I am too stupid to figure it out without having a look at it.
      My Computer

  10. Try3's Avatar
    Posts : 8,314
    Windows 10 Home x64 Version 21H1 Build 19043.1052
       #10

    Feuer said:
    i have also found a created bat file working with command like that,
    xxxx.bat input.txt -startline -endingline output.txt, trying to understand how it works
    ateş,

    Does this mean that extracting specified groups of lines is a fairly common requirement?
    Please may I ask what job this will form part of [just out of interest]?

    Denis
      My Computer


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 13:32.
Find Us




Windows 10 Forums