Intent:
The code is intending to amend the folder structure to inverse the order.
e.g.
From: G:\To Be Organised\Test\PID\Hello Pty Ltd*.*
To: G:\To Be Organised\Test\Hello Pty Ltd\PID*.*
The majority of files copy, but some do not. Some files seem to fail because they can’t be found. I suspected it was the 255 char length, but reviewing the folders that doesn’t seem to be the case. I’m hoping for a code review to make this work? It’s a one time use program, so it doesn’t have to be fast/efficient…
Many thanks,
$dest = "G:\To Be Organised\Test"
$tempOutput = "G:\To Be Organised\_TestOutput"
get-childitem -LiteralPath $dest -Directory |
foreach{
$ProductClass= $_.BaseName
get-childitem -LiteralPath $_.FullName -Directory | #get the insured names
foreach{
$InsuredName = Split-Path -Path $_ -Leaf
$sourcePath = $_.FullName #Full directory path of the product class subdirectories
$destPath = [IO.Path]::Combine($tempOutput, $InsuredName, $ProductClass) #change the destination path to be the new frankenstein output.
$fileList = @(Get-ChildItem -Path "$($sourcePath)" -File -Recurse)
$directoryList = @(Get-ChildItem -Path "$($sourcePath)" -Directory -Recurse)
Write-Host "Moving $($sourcePath) to $($destPath)"
ForEach($directory in $directoryList){
$directories = New-Item ($directory.FullName).Replace("$($sourcePath)",$destPath) -ItemType Directory -ea SilentlyContinue
}
ForEach($file in $fileList){
try {
Move-Item -Path $file.FullName -Destination ((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)) -Force -ErrorAction Stop
}
catch{
Write-Warning "Unable to move '$($file.FullName)' to '$(((Split-Path $file.FullName).Replace("$($sourcePath)",$destPath)))': $($_)"
return
}
}
}
}
#Write-Host "Deleting folder '$($sourcePath)'"
#Remove-Item -Path "$($sourcePath)" -Recurse -Force -ErrorAction Stop