3

I can run dos2unix on one file in PowerShell:

dos2unix ./assets/style.css

How to do this for all CSS files under ./assets/ and its subdirectories?

Annie
  • 3,090
  • 9
  • 36
  • 74

1 Answers1

7
'.\assets' | Get-ChildItem -Recurse -File -Filter '*.css' | ForEach-Object {
    dos2unix $_.FullName
}

Explanation

Get-ChildItem is like dir or ls (in powershell the latter 2 are aliases for that cmdlet). -File means return only files. -Recurse means recurse child directories. -Filter allows us to get only the file pattern desired.

Then we pipe that into ForEach-Object to execute a script block for each file returned and in there, we just execute the dos2unix command.

FullName is the property of the file object that contains the full path to the file.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • Curious use of piping WRT `'.\assets'`. Why not just `Get-ChildItem Assets -Recure -Filter *.css | Foreach {dos2unix $_.FullName}`? Note that you don't need to quote `*.css`. Also, use of -File will only work on PowerShell v3 and later. – Keith Hill Oct 31 '14 at 15:38
  • I prefer that style. So I tend to do things like `$path | Join-Path -Child Path 'whatever'` when possible. I just like the pipeline! Good point about `-File`. – briantist Oct 31 '14 at 15:44
  • Can you tell me how to run it quietly, so it doesn't print output? – Annie Oct 31 '14 at 17:30
  • Try piping it to `Out-Null`. I'm on mobile now so I can't post a proper example. – briantist Oct 31 '14 at 17:34