-1

Im new to the windows cmd (not powershell), how is an easy way to print out only the first 2-3 lines of each file (also some big files among it) in a folder? I found this so far, but it displays only the first two lines of one file, I cant get it working for all files in the folder.

for %x in (*.csv) do (more %x 2)

KevinNivek
  • 21
  • 7
  • Regardless of the fact that you've not made much of an effort at coding the task yourself, I do not believe that `more filename.csv 2` actually prints just the first `2` lines of `filename.csv` anyhow. Please check out the help and usage information for the `more.com` utility, by opening a Command Prompt window, typing `more /?` and pressing the `[ENTER]` key. Then feel free to use the seach facility at the top of the page to locate other examples whereby the intent is to read the first _i_ lines of a file, and adapt those as necessary. – Compo Oct 14 '21 at 15:47
  • well for one file it does print the first lines, but it does not work for the whole loop so i asked the question. however, I did not think of reading and then displaying since i thought there must be an more easy way (and stackoverflows proposals when asking the question where not really helping) – KevinNivek Oct 18 '21 at 10:24
  • will try this: https://stackoverflow.com/questions/130116/windows-batch-commands-to-read-first-line-from-text-file – KevinNivek Oct 18 '21 at 10:24

2 Answers2

0

I finally added this solution https://stackoverflow.com/a/130298/12473440 as head.bat in the PATH via set PATH=%PATH%;C:\myfolder, and it works perfectly!

KevinNivek
  • 21
  • 7
0

a for /f loop still processes the whole file. Actually, just reading the first <n> lines only is a lot faster with really big files (at a factor of about 10 with a 50MB text file). With small files, the difference is neglectable.

@echo off
setlocal EnableDelayedExpansion
set "file=s:\AutoIt\DMK\dmk.log"
set firstn=2
<"%file%" (for /l %%n in (1,1,%firstn%) do (
  set "line="
  set /p line=
  echo(!line!
))
Stephan
  • 53,940
  • 10
  • 58
  • 91