locked
Loop through a folder and all subfolders in Powershell RRS feed

  • Question

  • Hey Scripting Guys,

    I found this Powershell command online:

    ls -r  *.txt  | % {(gc $_ | % { $_.TrimEnd() }) | sc $_ }

    to remove trailing spaces from the end of each line of each text file in a folder.  How can I make it loop through the folder and all of its subfolders?

    Thanks.

    Tuesday, February 4, 2020 8:44 PM

All replies

  • Sorry but this forum does not rewrite code you have found online.  It is for questions about code you are writing or have written.

    Please read the following carefully:


    \_(ツ)_/

    Tuesday, February 4, 2020 9:38 PM
  •   How can I make it loop through the folder and all of its subfolders?

    It already does. Run the first command (ls -r *.txt) and see what it outputs. 

    For readability I would recommend that you use the full cmdlet name and not the alias. 

    get-childitem  -recurse  *.txt  | % {(get-content  $_ | % { $_.TrimEnd() }) | set-content  $_ }  

    Wednesday, February 5, 2020 12:48 AM