This time I downloaded a bunch of mp3s that were on Google docs. Google docs is awesome, it lets you select a bunch of files, and download them all. Then it zips them up so you can download them all together. Unfortunately, when I unzipped them, they didn't have a file extension...
dir | % { mv $_.FullName ( $_.Name + ".mp3" ) }That command gets every file in the directory ( dir ), sends them to the next command ( | ), which loops over them one at a time ( %, is short hand for foreach-object ), then executes the move command ( mv ) with the file's full name as the first argument ( $_.FullName, $_ is magically set to the current loop item ), and the file's name with .mp3 tacked onto the end as the second argument ( $_.Name + ".mp3" ). The parentheses tell it to evaluate the expression and pass the result as the second argument.
Beautifully simple.
Need to figure out what properties are available on the objects you get from the dir command?
dir | get-memberNeed to figure out what the value of one of those properties will actually be?
dir | % { $_.FullName }Gotta love Powershell!
My dad pointed out to me that in cmd you can do this by: rename *.* *.mp3.
ReplyDeleteOddly enough, you can't do this in powershell.
I did find a slightly simpler powershell way to do this:
dir | rename-item -newname { $_.name + '.mp3' }
That's basicaly the same, but using rename you don't need the foreach-object command.
I can't believe the answer to my problem comes from six years ago! Thanks lol
DeleteHere's another fun example. I wanted to move all files that ended in Repository.cs to a Repositories folder in Mercurial:
ReplyDeletedir *Repository.cs | % { hg rename $_.Name ( ".\Repositories\" + $_Name ) }
And another way. As mentioned above exploring PS to learn its power whenever I think to my self "I bet PS can do this with one command". Just discovered Powershell ISE. Wow.
ReplyDeleteget-childItem "C:\Users\user1\Documents\closet*" | rename-item -newname { $_.name + ".txt" }