0

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 3

2

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) 
2

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; } 
1

Just create separate files in another directory.

md sub for %f in (*.txt) do more +3 “%f” > sub\%f 
1
  • Create subfolder permission needed... sub subfolder must not exist... and some amount of free space needed too (but seems it exists because it exists for output.txt file). Commented Oct 11, 2018 at 11:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.