Note: $arrOldNames contains only a list of old folder names like so: old_name_1 $arrFullNames contains the full path and folder name like so: ‘\\mynas\ar_books\old_name_1’
For example, if ‘old_name_1’ is contained in $arrFullNames, then store that value from $arrFullNames into a new array called $arrOldNames2.
I can use the following to check if the $oldName exists in $arrFullNames…
foreach ($oldName in $arrOldNames) {
if ($arrFullNames -like "*$oldName*") {
Write-Host "$oldName exists"
}
}
However, I can’t figure out how to retrieve the object in $arrFullNames to store it into a new array.
I think I just found something that will help me achieve my final goal with renaming the folders…
I just need to test it out more to perform the actual renaming…
foreach ($oldName in 0..$arrOldNames.GetUpperBound(0)) {
if ($arrFullNames -like "*$oldName*") {
Write-Output "Full OLD name is $($arrFullNames[$line])"
#Write-Output "OLD name is $($arrOldNames[$line])"
Write-Output "NEW name is $($arrNewNames[$line])"
#Write-Host "$oldName exists"
}
}
I’ll be back…
DISREGARD… that GetUpperBound(0) isn’t returning the correct NewName value I wanted… The Write-Output "Full OLD name is $($arrFullNames[$line])" is returning the first value in that array, not the one it’s being matched to.
This is why I think I need the new array so that the new array will line up with the NewName array…
If there’s a way to search and rename without a new array, that would be optimal…
That code just pulls the data from the text files in, loops through each $arrfullnames object, and if it is in $arroldnames, it puts it in the output… I’m just capturing all that in the $arr var, so assuming its more than one, it will be an array of strings. In any case, the hits should be in $arr. You can name it whatever you want of course, my brain couldn’t come up w/ a name haha. If I misunderstood though sorry I may just need further clarification on the ask.
Both ways should give you same output. Mine could be simplified a little, but that just reflects the way my mind works. I didn’t think of looping through the array that already contained the data I needed to export, so I had to come up with a way for my comparison to return the matched value. Using -like in an if statement won’t do it, but that’s what Where-Object is made for.
I was playing around with your code, but $arr is coming up empty.
I tried the following:
Changed -contains to -like; still empty.
Reversed arrays for both tests with -contains and -like; still empty.
Used wildcards (*) for both tests with -contains and -like; $arrFullNames -like "*$_*" returned the 5 values from the $arrOldNames instead of the matching values from the $arrFullNames array.
Just curious, but is the current PS Object ($_) the array for the one being piped to the ForEach-Object or the IF statement?
Ultimately I’m trying to return the matching value from $arrFullNames array based on the search value used from the $arrOldNames array.
Something along the lines of:
if value in $arrOldName is found in $arrFullNames, then return the value found in $arrFullNames.
I’m using a wildcards because if the $arrOldNames value is: ‘old name 1’, the value in $arrFullNames will be slightly different and includes the full path to the folder; example:
‘\\mynas\ar_books\old name 1 - LG 3.1’
I’m going to play around with @masterofthehat 's code in the time being…