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?
{ (gc 'temp_new_lines'),(gc $_) | sc $_ }