3 ms·
Got a directory full of files with underscores in the names? Want spaces instead? Easy, type this: dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -ma
by tacos 11y ago
Got a directory full of files with underscores in the names? Want spaces instead? Easy, type this:
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "_"} | foreach { $New=$_.name.Replace("_"," ") Rename-Item -path $_.Fullname -newname $New -passthru }
EDIT: I purposely posted the first Google result. You obviously Googled as well as your code got it backward. But really, why isn't it this? It's worked since the 1980s. You'll dig it since it uses extra apostrophes.
rename '_' ' ' *
- UK-AL 11y agoDir | rni –NewName { $_.name –replace " ","_" } That command does some extra stuff, which would make the bash equivalent also complicated.
- latkin 11y agoYep. Technically the request included recursive rename, and to avoid renaming directories, but gosh that's not hard: dir -rec -file | rni –newname { $_.name –replace "_"," " } Edit: Oops, request did not include recursive, the original sample solution did. FWIW top Google results for "unix recursively rename files" give below solutions (slightly different scenarios but you get the idea). Not particularly shorter, or less cryptic: find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' \; shopt -s globstar nullglob rename _dbg.txt .txt **/*dbg*
- tacos 11y agoY'all are still missing the point. We live in a world where people use CoffeeScript to avoid typing a semicolon. When you Google a PowerShell problem the Google results come back looking insane. You can play code golf here all you want but even this version looks nuts to outsiders. It's a cultural problem. You don't solve it by shouting subtly-differing versions of your culture, louder.
- UK-AL 11y agoHis 'rename' wasn't recursive. <Quote>Y'all are still missing the point. We live in a world where people use CoffeeScript to avoid typing a semicolon. When you Google a PowerShell problem the Google results come back looking insane. You can play code golf here all you want but even this version looks nuts to outsiders It's a cultural problem. You don't solve it by shouting subtly-differing versions of your culture, louder. ".</Quote> You're not getting the point. Powershell is just short, and sometimes shorter if you actually learn it. "When you Google a PowerShell problem the Google results come back looking insane" - This is a problem with the age of powershell. Bash has been around for ever, so there is lots of good quality content on the web.
- tacos 11y agoThat wasn't what I Googled. And Microsoft is announcing bash and Linux shell subsystems today for Windows so y'all are really missing the point. Maybe dev goes one way and ops goes another. But probably not. I'm not betting on Powershell.
- UK-AL 11y ago"You obviously Googled as well as your code got it backward. " - No, just a simple mistake from writing it on the fly. This isn't even probably the smallest, somebody smarter than me could do better. There's probably a PowerShell module that has your equivalent.