1

I already searched through google but cant find an answer.

i want a powershell script to add a zero on every 3rd position on every foldername.

Structure now looks like this:

10_vdvdsfadsgd
11_dsnpdnfp
12_spancfspo
20_ndsknfp
21_mpmsdpfdo

and i want it to be like this:

100_vdvdsfadsgd
110_dsnpdnfp
120_spancfspo
200_ndsknfp
210_mpmsdpfdo
sasros
  • 13
  • 2

2 Answers2

3

You can rename the folders with Rename-Item, and then insert 0 into the name at a specific index with String.Insert():

Get-ChildItem path\to\root\folder -Directory |Rename-Item -NewName { $_.Name.Insert(2, "0") }

Using Get-ChildItem's -Filter parameter if you want to target only folders with the given name format:

Get-ChildItem path\to\root\folder -Directory -Filter "??_*" |Rename-Item -NewName { $_.Name.Insert(2, "0") }
mklement0
  • 382,024
  • 64
  • 607
  • 775
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks alot!!!! Can you explain what -Filter "??_*" does? Is ??_* simply a Placeholder or is it a regex – sasros Nov 27 '21 at 18:43
  • 1
    @sasros See [this answer](https://stackoverflow.com/a/17739503/15339544) to understand the `?` wildcard. – Santiago Squarzon Nov 27 '21 at 19:00
  • It's a wildcard filter, `??` means any 2 characters, `_` is a literal `_` and `*` means any number of characters. It's equivalent to the regex `^.._.*$` – Mathias R. Jessen Nov 27 '21 at 20:01
  • Nicely done, although due to the legacy quirks of `-Filter` as explained in the answer that @Santiago links to, `??_*"` will _not_ work as expected - it will match _more_ than expected. – mklement0 Nov 27 '21 at 23:20
  • @mklement0 nobody is passing `??_*"` to the filter parameter, the double-quote is part of the string literal. – Mathias R. Jessen Nov 27 '21 at 23:58
  • That was a _typo_. And, as such, entirely incidental to my point, which stands. – mklement0 Nov 28 '21 at 00:35
  • @mklement0 I'm afraid I still don't understand. Can you provide an example of a file name matched by `??_*` that isn't at least 3 characters long and doesn't have a `_` at index 2? – Mathias R. Jessen Nov 28 '21 at 01:17
  • `[1].txt` (in _Windows PowerShell_). I guess I should have said: will _typically_, but _not always_ work as expected. The larger point is: Beware the legacy quirks of `-Filter` wildcards, especially in Windows PowerShell. – mklement0 Nov 28 '21 at 02:50
  • The edge cases with your particular pattern turned out to be more exotic than I thought. It is a _trailing_ run of consecutive `?` that is much more likely to yield false positives: e.g., `Get-ChildItem -Filter ??.txt` matches both `12.txt` and `1.txt` – mklement0 Nov 28 '21 at 07:51
  • @mklement0 That's because of the `.`. With the pattern I've provided, this is simply not the case, eg. `New-Item 1_.txt -Force|Out-Null;gci -Filter ??_*` yields nothing – Mathias R. Jessen Nov 28 '21 at 14:03
  • Yes (it's either before a `.` or at the end of the pattern). However, `??_*` matches files named `[1].txt` and `[c] anyoubelievethis`, for instance (sic - don't ask me why). – mklement0 Nov 28 '21 at 14:07
0

If you change the path in the script to where the folders are, the script below changes the name to add a zero before the underscore.

$folderspath = 'C:\Test'
$folders = gci -Path $folderspath

ForEach($folder in $folders) {
    $CurrentFolderName = $folder.name
    $CurrentFolderName = $CurrentFolderName.tostring()
    $NewFolderName = $CurrentFolderName.Replace("_","0_")
    $FolderPath = $folder.FullName
    
    Rename-Item -Path $FolderPath -NewName $NewFolderName
    
}
NeoTheNerd
  • 566
  • 3
  • 11