0

I have a batch file with more command to skip the first few lines of the file and print the rest. I am using more +6 file_name. I see that it opens some percentage of the file and asks the user to enter the prompt so that it can load the next portion. I tried redirecting the output of the more command to a file using the > operation to another file and still have the same problem.

https://ss64.com/nt/more.html

When MORE is used without any redirection symbols it will display the percent complete e.g.

MORE /E myfile.txt
--More (17%) --

Thanks, Pavan.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
  • 1
    Yes. If the file is more than 64K lines, `MORE` will prompt you to continue. – Squashman Aug 15 '18 at 14:59
  • So is there a way to circumvent this problem @Squashman. Or do I have to resort to reading the file using a for-loop and skipping the first 6 lines :( – Pavan Dittakavi Aug 15 '18 at 15:04
  • 3
    You will need to use a `FOR /F` to read the file. Has been discussed here on SO and on DosTips.com. Nobody has found a way around that afaik. – Squashman Aug 15 '18 at 15:29
  • Even if you piped some prompt reply in (like `echo/| more +6 infile.txt > outfile.txt`) you'd get the prompt text in the output file; `for /F` however limits the lengths of each line to 8K characters, so depending on your data you might need to switch to another language like PowerShell... – aschipfl Aug 15 '18 at 19:05

2 Answers2

0

The more command prompts for user input even when you redirect its output to a file, as soon as 64K lines have been encountered. In addition, more expands TABs to SPACEs.

A work-around is to use a for /F loop to skip the first 6 lines:

> "outfile.txt" (
    for /F "usebackq skip=6 delims=" %%L in ("infile.txt") do @(
        echo(%%L
    )
)

This however limits the line length to 8K characters/bytes. Furthermore, this skips empty lines.

To keep empty lines, you could do this:

> "outfile.txt" (
    for /F "skip=6 delims=" %%L in ('findstr /N "^" "infile.txt"') do @(
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        echo(!LINE:*:=!
        endlocal
    )
)

This precedes every line by a line number and a colon using findstr intermittently, so no line appears empty to for /F; the prefix is removed when writing out the lines then. The line length is still limited though.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • `for /F "skip=6 tokens=1,*delims=:" %%L in ('findstr /N "^" "infile.txt"') do @echo(%%M`? – Stephan Aug 16 '18 at 06:02
  • 1
    Just be aware that the method used in the comment above would remove any leading colons, **`:`** from your lines because `Delims` is greedy, _it counts multiple successive delimiters as one_. – Compo Aug 16 '18 at 06:36
  • That's why I prefer `find /n /v ""` over `findstr /n "^"` (colons are pretty common, but a line starting with `]` should be very very rare.) – Stephan Aug 16 '18 at 06:47
0

You may use this simpler method:

< myfile.txt (

   rem Skip the first 6 lines
   for /L %%i in (1,1,6) do set /P "="

   rem Show the rest
   findstr "^"

)

This method may fail if the skipped lines are longer than 1023 characters, that is the limit of set /P command.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Besides the worse line length limitation, this requires that the last line in the text file is terminated by a line-break, as otherwise, `findstr` may hang (depending on Windows version; on Windows 7 it hangs)... – aschipfl Aug 16 '18 at 08:31