Batch file or command line for removing header lines from text files

I am trying to remove the first 3 lines from 10,000 text files. Using:

for %f in (*.txt) do more +3 “%f” >> output.txt

I am able to remove the headers and concatenate all files in the folder to one new file. However, I would like to keep the files separate, just without the header lines. Is this possible?

3 Answers

You cannot overwrite source files. And you cannot autogenerate new name because of possible overlapping. But you can save the file without header into temporary folder and then move it over source. It will be something like:

@echo off
for %%f in (*.txt) do ( more +3 "%%f" > "%TEMP%\%%f" move /y "%TEMP%\%%f" "%%f" > nul
)
echo Done.

Or the same as one command in command line:

@for %f in (*.txt) do @(more +3 "%f" > "%TEMP%\%f" && move /y "%TEMP%\%f" "%f" > nul)

If your environment makes it possible then consider using Powershell so you don't need to use temp files and folders.

Foreach ($file in Get-ChildItem -Path ".\" -Filter *.txt ) { $content = Get-Content $file.FullName | Select-Object -Skip 3; Set-Content -Path $file.FullName -Value $content;
}

Just create separate files in another directory.

md sub
for %f in (*.txt) do more +3 “%f” > sub\%f
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like