Batch add header to ASCII text files, variable length

I need to add a simple single line header to a variable-length ascii (.asc) file package (every few million lines) and save with the same name. I have a large number of these files. Is this best done using a Windows batch file? If so, can anyone explain how?

Thanks in advance, I know that it should be simple and look for an answer, but nothing was found that seems completely correct ...

+3
source share
3 answers

As you can only add something to the file, you cannot prefix the text with a simple operation.
But you can:

  • copy your header first into a new file ( copy header.template header.tmp)
  • (type original.txt >> header.tmp)
  • (del original.txt)
  • (ren header.tmp original.txt)

EDIT:

for %%F in (*.txt) DO (
  echo Working on %%F
  copy header.template newFile.tmp
  type "%%~F" >> newFile.tmp
  del "%%~F"
  copy newFile.tmp "%%~F"
)
+1

.

:

move source.asc source.asc.tmp
echo "header" > source.asc
type source.asc.tmp >> source.asc
del source.asc.tmp

, ( ):

for %%F in (*.asc) DO (
    move "%%F" tmp.txt
    echo header > "%%F"
    type tmp.txt >> "%%F"
    del tmp.txt
)
+1

Although jeb's answer decided to solve this question, I think the method below should work faster with more files, because it uses fewer commands and the "mass rename" operation:

for %%F in (*.txt) do (
   echo Working on %%F
   copy header.template + "%%F" "%%~nF.tmp"
)
move /Y *.tmp *.txt
+1
source

All Articles