1

I would like to iterate over all js files in a folder, and add the exact line to all of them. The text should be appended at the end of the file in a new line. I'd like it to be written in a windows supported scripting language like batch or powershell.

My pseudo code would look like:

foreach file in folder
    append some text to file in a new line
Secespitus
  • 710
  • 2
  • 14
  • 22
Joe
  • 443
  • 1
  • 4
  • 21
  • For Powershell have a look at [Add-Content](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-content?view=powershell-6) – Theo Jun 26 '19 at 19:45
  • What have you tried, where are you stuck? Do all these files have a line-break at their end? – aschipfl Jun 26 '19 at 21:04
  • For Batch, see [FORFILES](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles) and [this](https://stackoverflow.com/questions/5139157/append-text-with-bat) question. – BDM Jun 26 '19 at 21:12

2 Answers2

1

This can be easily done with a , using the >> redirection operator, given that every file ends with a line-break:

for %%F in ("%TargetFolder%\*.js") do (
    >> "%%~F" echo/This is the new line of text.
)

Or if you want to recurse into sub-directories also:

for /R "%TargetFolder%" %%F in ("*.js") do (
    >> "%%~F" echo/This is the new line of text.
)

You can of course first explicitly append a line-break:

for %%F in ("%TargetFolder%\*.js") do (
    >> "%%~F" (
        echo/
        echo/This is the new line of text.
    )
)

Or equivalently for the recursive approach, of course.


If you want to append a line-break only in case the file does not already end with such, you could do the following:

for %%F in ("%TargetFolder%\*.js") do (
    rem /* Count the number of lines that contain zero or more characters at the end;
    rem    this is true for every line except for the last when it is not terminated
    rem    by a line-break, because the `$` is anchored to such: */
    for /F %%D in ('findstr ".*$" "%%~F" ^| find /C /V ""') do (
        rem // Count the total number of lines in the file:
        for /F %%C in ('^< "%%~F" find /C /V ""') do (
            >> "%%~F" (
                rem // Compare the line counts and conditionally append a line-break:
                if %%D lss %%C echo/
                echo/This is the new line of text.
            )
        )
    )
)

And again equivalently for the recursive approach.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I'm feeling a little bit "greedy", but what if I want to add my line one line before the last line and not *as* the last? – Joe Jun 26 '19 at 21:42
  • 2
    Well, this is a complitely new situation, because the `>>` cannot be used any more, the file needs to be read and rewritten, which is not that trivial in batch; I feel this was worth a new question, but please make sure to include a [mcve] of your own coding attempts then... – aschipfl Jun 26 '19 at 21:45
  • You're totally right. The thing is that, apparently, there aren't so many questions in forums regarding batch commands, and I don't wish to learn the whole syntax, I just need something specific to be done. Great answer though :) – Joe Jun 26 '19 at 22:44
1

This is easily done using PowerShell.

Add-Content -Path 'C:\src\t\copyt\*.txt' -Encoding ascii -Value "`nEND"

Or, in a .bat file script.

powershell -NoLogo -NoProfile -Command ^
    Add-Content -Path 'C:\src\t\copyt\*.txt' -Encoding ascii -Value "`nEND"

If the files are not large, then to add the line before the last line:

$TempFile = New-TemporaryFile

Get-ChildItem -File -Path 'C:\src\t\copyt' -Filter '*.txt' |
    ForEach-Object {
        $f = Get-Content -Path $_.FullName
        $f[0..($f.Length -2)] + "BOTTOM LINE" + $f[-1] |
            Out-File -FilePath $TempFile -Encoding default
            Remove-Item -Path $_.FullName
            Move-Item -Path $TempFile -Destination $_.FullName
    }

Remove-Item -Path $TempFile
lit
  • 14,456
  • 10
  • 65
  • 119