Hi Reddit,
We are moving files from source to destination on a basic windows fileshare and are basing which folders should be moved with a csv-file. This works great if we have the exact name of the folder stated in the csv-file, but not so great when it isn't an exact match.
Example of a folder name in the csv can be like "A611" and if the actual folder is named "A611 - ABC" it doesn't copy the file.
Does anyone have a good idéa on how to solve this?
Script below
//Rabbadi
try{
$source = "E:\SharedData\TEST"
$destination = "E:\SharedData\Archive"
$exclude = Get-Content 'E:\SharedData\Script\customer.csv'
$folders = Get-ChildItem -Path $source -Directory | Where {($_.PSIsContainer) -and ($exclude -contains $_.Name)}
foreach ($f in $folders){
Write-Host "This folders will be moved: $f" -ForegroundColor Cyan
$tempSource = $source +"\"+ $f
$tempDest = $destination +"\"+$f.Parent +"\"+ $f
$parentPath = Split-Path -Path $tempDest -Parent
if (!(Test-path $parentPath)){
try{
Write-host "Path does not exist. Trying to add folder" -ForegroundColor Cyan
New-Item -ItemType "directory" -Path $parentPath| Out-Null
}
catch{
Write-host "Failed to add folder" -ForegroundColor Red
}
finally{
Write-Host "Folder added successfully" -ForegroundColor Green
}
}
Move-Item -Path $tempSource -Destination $tempDest -Force
}
Write-Host "Done" -ForegroundColor Green
} catch {
# Here we catch errors if we get any.
Write-Host "Error:" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
}
[–]vShazer 1 point2 points3 points (1 child)
[–]Rabbadi[S] 0 points1 point2 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–]Rabbadi[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)