Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

1
Renaming first occurrence using piped value
Post Flair (click to view more posts with a particular flair)
Post Body

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)}

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
26,663
Link Karma
8,214
Comment Karma
18,430
Profile updated: 1 day ago
Posts updated: 8 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago