This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
So I helped someone do this earlier but I was curious as to why it messed up when I made it into a one liner.
It’s probably something dumb I’m doing so I figured I’d ask.
Can someone explain to me why this messes up the first entry?
Get-ChildItem C:/temp/test -Filter *0* | ?{$_.attributes -eq "Directory"} | Rename-Item -NewName { [regex]$pattern = "0"; $pattern.replace($_.name, "2", 1)} -Verbose
It makes my structure like this:
C:\temp\221
C:\temp\202
C:\temp\203
C:\temp\204
If I run
Get-ChildItem C:/temp/test -Filter *0* | ?{$_.attributes -eq "Directory"} | Rename-Item -NewName { [regex]$pattern = "0"; $pattern.replace($_.name, "2", 1)} -WhatIf
It shows properly but when I run the command the first folder always has two 2s'(becomes 221). I feel dumb but maybe it is because I am tired.
my structure is:
C:/temp/test/001
C:/temp/test/002
C:/temp/test/003
C:/temp/test/004
Solution:
It needs to have the first portion in braces to prevent it from re-reading. Thanks u/Lee_Dailey and to u/l0wet for showing me the “-Directory” feature of GCI (I’m still a scrub it appears!)
(Get-ChildItem C:/temp/test -Filter *0* -Directory) | Rename-Item -NewName { [regex]$pattern = "0"; $pattern.replace($_.name, "2", 1)}
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...