0

This works to add the text my_new_text as the last line in all .txt files:

FOR /F "tokens=*" %%G IN ('dir /b *.txt') DO echo my_new_text>> "%%G"

Is there a simple edit that can be made to this command to make it add my_new_text as the first line in the file?

If I can do that then I can add the lines I need (in reverse order).

I tried things like putting << instead of >> but that's about the limit of my abilities!

I'm not sure if echo even has this functionality?

It would be simpler in PowerShell I guess but I'd want to run that command in this same batch file.

Trying with PowerShell using an existing file with the lines:

I did try a Powershell command that uses an already created txt file containing the lines but that didn't work.

I ran this first as a set of standard echo commands to create the file:

echo my_1st_line>> "temp_lines.abc"
echo my_2nd_line>> "temp_lines.abc"
echo my_3rd_line>> "temp_lines.abc"

(Those are called ".abc" files just so they aren't called txt files, for this next bit!)

Then I used this PowerShell command to try to add the contents of temp_lines.abc to the start of all txt files:

Powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "gci *.txt | ForEach-Object {(Add-Content $_) -Value (Get-Content 'temp_lines.abc') | Set-Content $_.FullName}"

That doesn't work - PowerShell doesn't add anything to the start of txt files.

It would be a lot better if this can be done just using echo but I guess it's another one where PowerShell is needed?

2
  • 2
    Given the data fits in memory, { (gc 'temp_new_lines'),(gc $_) | sc $_ } Commented Apr 28, 2022 at 4:50
  • Cheers, that works. If you put it as the answer I will mark it as correct.
    – bat_cmd
    Commented Apr 28, 2022 at 8:23

1 Answer 1

1

Is there a simple edit that can be made to this command to make it add my_new_text as the first line in the file?

There's no way to insert data in a file in any programming or script languages because disk or tape data at the end of the file don't automatically move for you. You have to copy the data to a new file or load it into memory and save it as another file. Here's a solution in cmd

FOR /F "tokens=*" %%G IN ('dir /b *.txt') DO (
    >  "%%G_tmp" echo my_new_text
    >> "%%G_tmp" type "%%G"
    del "%%G"
    ren "%%G_tmp" "%%G"
)

If you want to insert these 3 lines in temp_lines.abc

my_1st_line
my_2nd_line
my_3rd_line

to all the *.txt files then do similarly

FOR /F "tokens=*" %%G IN ('dir /b *.txt') DO (
    >  "%%G_tmp" type temp_lines.abc
    >> "%%G_tmp" type "%%G"
    del "%%G"
    ren "%%G_tmp" "%%G"
)

Add-Content in PowerShell obviously won't work because it appends content to a specified item or file if you've read its man page. And there's no PowerShell that can prepend data to a file due to the reason I said. The solution is also copy data to memory or to another file

ls *.txt |% { (gc temp_lines.abc, $_) | sc $_}
ls *.txt |% { $c = gc $_; cp temp_lines.abc $_; ac $_ $c }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .