VMware Cloud Community
vmCalgary
Enthusiast
Enthusiast
Jump to solution

Rename multiple folders with string

Our environment has folders leftover from a former vCloud environment that contains unnecessary information contained in brackets.Example: foldernameioldername (asdafasdkfajsdkfjkajwoe ) .

I want to rename the folder to foldername . This would be easy if I had just one folder, but I have several with subfolders that contain the unnecessary info in brackets. How can this be achieved? This will be done from two separate main folders in different datacenters (vm and template folders), vCloud (AB-01) and vCloud (BC-02), vs the entire vcenter or a specific datacenter.

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$dcNames = 'DC1', 'DC2'

$folderName = '* (*)'


Get-Folder -Location $dcNames -Name "$($folderName)*" -Type VM |

   ForEach-Object -Process {

   $newName = $_.Name -replace '(\w+) .*', '$1'

   Set-Folder -Folder $_ -Name $newName -Confirm:$false

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$dcNames = 'DC1', 'DC2'

$folderName = '* (*)'


Get-Folder -Location $dcNames -Name "$($folderName)*" -Type VM |

   ForEach-Object -Process {

   $newName = $_.Name -replace '(\w+) .*', '$1'

   Set-Folder -Folder $_ -Name $newName -Confirm:$false

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmCalgary
Enthusiast
Enthusiast
Jump to solution

Luc,

I am concerned that this code may get folders outside of the subfolders listed in folders vCloud (CG-NL) and vCloud (CG-ZZ) if I go with DC.

2019-01-15_08-06-08.png

0 Kudos
vmCalgary
Enthusiast
Enthusiast
Jump to solution

Try something like this

$dcNames = 'DC1', 'DC2'

$folderName = '* (*)'

Get-Folder -Location $dcNames -Name "$($folderName)*" -Type VM |

   ForEach-Object -Process {

   $newName = $_.Name -replace '(\w+) .*', '$1'

   Set-Folder -Folder $_ -Name $newName -Confirm:$false

}

Looks like the following may work:

$dcNames = 'DC1', 'DC2'

$folderName = 'O* (*)'

Get-Folder -Location $dcNames -Name "$($folderName)*" -Type VM |

   ForEach-Object -Process {

   $newName = $_.Name -replace '(\w+) .*', '$1'

   Set-Folder -Folder $_ -Name $newName -Confirm:$false

}

pastedImage_2.png

0 Kudos
vmCalgary
Enthusiast
Enthusiast
Jump to solution

Perfection!!! Thank you LucD

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, you can indeed change the folderName mask to whatever you need to select.
With the Location parameter you restrict the Get-Folder cmdlet to those specific datacenters.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vmCalgary
Enthusiast
Enthusiast
Jump to solution

LucD

I'm struggling with finding those folders with 37 characters between a pair of parenthesis so I can assign to $folderName.

eg: CloudpathES_5.0.3314 (dc9109d8-808a-4fa4-8a5f-5f6f0321ba29)

Information before the pair of brackets is a unique name per folder, as is the information within parenthesis, but the character count between parenthesis is consistently 37 characters.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

In the masking a '?' represent a single character.

You could try with

$folderName = "*($('?' * 37))"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vmCalgary
Enthusiast
Enthusiast
Jump to solution

@LucD

That didn't quite work.

I played around a bit and got a list of folders with the following:

Get-Folder '*(*)' | Where-Object {$_.name.length -gt 40} |Select Name,@{Name="NameLength";Expression={$_.name.length}}

----                                                                      ----------

O3190_VDC_CGCL_DEV (4bdc8fea-4652-4278-ae3f-5058cd5b12d2)                         57

O4063_VDC_CL_DEV (f25ac38b-d879-4ccc-92ea-27b08bdb720f)                           55

O3190_VDC_CGCL_LAB (ca2eedbc-f191-4dd5-96ee-9c346fd2f5f7)                         57

O4063_VDC_CL_LAB (5fb5612f-0a2f-4e53-8f4e-4a2903943950)                           55

IPerf server for ASR5.5k tests (eb692d8d-946a-40da-96f3-87400b49b586)             69

WiFi Guest Portal Dev (717ddd5d-568b-4f0b-a683-9a7d5fc487af)                      60

WiFi DEV Location Based Service (ece8f18b-905d-4eb3-a913-1e6935029c31)            70

Wi-Fi Captive Portal Dev (aa58c19f-24d0-406c-8ef2-922435f3ba90)                   63

I started at -gt 37 and then worked my way up until I hopefully got all folders. I'll run a quick '*(*)' check when done. Thanks for your help. I would have expected this to be easier (former sed/awk history in my career).

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The masking that is available on the value you pass to Name is quite limited (* for any number of characters, ? for 1 character).

$folderName = '*abc?'

Get-Folder -Name $folderName

On the other hand with the -match operator one can use RegEx expressions to match a name (and that should more up your street).

Get-Folder | where {$_.Name -match "\w* \(.*\)"}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmCalgary
Enthusiast
Enthusiast
Jump to solution

I ended up exporting info to csv, doing the filename part with an =LEFT  formula. Turns out there was 105 folders.

$csv=Import-Csv .\Book1.csv

foreach($folder in $csv){

Get-Folder $($folder.OldName) |

Set-Folder -name $($folder.NewName) -confirm:$true }

I played it safe with $true and did a manual confirmation.

scripts> $csv

OldName                                                                   NewName

-------                                                                   -------

arahmani-RHEL6 (bb1afd1b-3fd0-47e9-9b0e-f995fe40f8d8)                     arahmani-RHEL6

Arris Headend DMZ (2cd4a98f-ba17-4d45-9f98-bf14d59e96f8)                  Arris Headend DMZ

Bootp (2158febf-f48f-450e-b9d4-4f5f5e7c88ec)                              Bootp

BootP Test (448a7b65-36a0-4359-82c7-bc09eb725d84)                         BootP Test

bootpd_too (c839a3b7-04d6-4d6a-905e-9a3ea0316843)                         bootpd_too

Calgary PreProd (445bdf9e-a636-4557-8356-d7420b9e2454)                    Calgary PreProd

0 Kudos