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

Page 1 of 2 12 LastLast

  1. Posts : 318
    Windows 10 Home Single Language 21H1
       #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. Posts : 16,910
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #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. Posts : 318
    Windows 10 Home Single Language 21H1
    Thread Starter
       #3

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


  4. Posts : 16,910
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #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:
    :: This script reads and echoes each line in the designated SourceFile
    :: This script copes with spaces and special characters being in the current folder path, in the SourceFile name and in the text of the SourceFile
    :: Each line of the output continues to be protected against spaces and special characters and must be unless and until the text is proven to contain no spaces or special characters
    
    :: This Echo Off command just allows the processing to be hidden and would often be used in real scripts.
    :: The symbol at the start of the line stops the command itself being shown
    :: It is currently 'remarked out' by preceding it with :: a pair of colons so the processing can be examined
    :: @Echo Off
    :: Simplifying the default prompt $p$g for ease of viewing the processing
    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=File & Read.txt"
    For /f  "usebackq tokens=*" %%X in ("%SourceFile%") do Call :Processing "%%X"
    GoTo EndSimpleFileReadingDemo
    
    :Processing 
    :: Process each passed line
    :: Protect each stage of processing and displaying variables with " so that special characters, such as &, do not cause it to fail
    set ThisParameter=%*
    ::Now strip out the leading & trailing " that were added in passing the lines to protect them so leaving each original line unaltered
    set "ThisParameter=%ThisParameter:~1%"
    set "ThisParameter=%ThisParameter:~0,-1%"
    :: Any further processing of the read line takes place here.  The echo is just used as an example.
    :: ThisParameter must continue to be protected by " unless and until the text is proven to contain no spaces or special characters
    echo "%ThisParameter%"
    GoTo :eof
    
    :EndSimpleFileReadingDemo
    Pause during testing

    For is such a sophisticated & powerful command that it takes a lot of studying and a lot of getting used to.
    Show the built in help with this command For /?
    For - SS64
    For - MSLearn



    Denis

    1st Sep 2023 - I updated the batch file to include the protection of spaces and special characters that we discussed later on in this thread. I felt shamed into doing so because, after all this time, I found a non-fatal error in the batch file.
    It's not my record though. Last year I found a bug in a script that I had been using since the 1990s. The bug was revealed by a particular combination of inputs that had simply never occurred before.
    Last edited by Try3; 01 Sep 2023 at 10:45.
      My Computer


  5. Posts : 16,910
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #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:
    :: This script reads and echoes each line in the designated SourceFile and it displays a count of the line number for each line
    :: This script copes with spaces and special characters being in the current folder path, in the SourceFile name and in the text of the SourceFile
    :: Each line of the output continues to be protected against spaces and special characters and must be unless and until the text is proven to contain no spaces or special characters
    
    :: This Echo Off command just allows the processing to be hidden and would often be used in real scripts.
    :: The symbol at the start of the line stops the command itself being shown
    :: It is currently 'remarked out' by preceding it with :: a pair of colons so the processing can be examined
    :: @Echo Off
    :: Simplifying the default prompt $p$g for ease of viewing the processing
    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"
    
    :: Initialisation
    ::Set up a counter for use in the For looping
    Set /a LineCounter=0
    
    Set "SourceFile=File & Read.txt"
    For /f  "usebackq tokens=*" %%X in ("%SourceFile%") do Call :Processing "%%X"
    GoTo EndFileReadingCountingDemo
    
    :Processing 
    :: Increment the LineCounter
    Set /a LineCounter=%LineCounter%+1
    :: Process each passed line
    :: Protect each stage of processing and displaying variables with " so that special characters, such as &, do not cause it to fail
    set ThisParameter=%*
    ::Now strip out the leading & trailing " that were added in passing the lines to protect them so leaving each original line unaltered
    set "ThisParameter=%ThisParameter:~1%"
    set "ThisParameter=%ThisParameter:~0,-1%"
    :: Any further processing of the read line takes place here.  The echo is just used as an example.
    echo Line number %LineCounter% is - "%ThisParameter%"
    GoTo :eof
    
    :EndFileReadingCountingDemo
    Pause during testing

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

    Denis

    Also updated on 1 Sep 2023
    Last edited by Try3; 01 Sep 2023 at 10:49.
      My Computer


  6. Posts : 7,606
    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. Posts : 318
    Windows 10 Home Single Language 21H1
    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. Posts : 18,044
    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. Posts : 7,606
    Windows 10 Home 20H2
       #9

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


  10. Posts : 16,910
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #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


 

  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 04:18.
Find Us




Windows 10 Forums