I have a script running quite often throughout a working day. As of lately it moved what appears to be all of my System32 file. I know the System32 files are running through the script as they are being organized and mapped 'correctly'.
However, I am never calling the system32 directory nor am I calling anything on the C drive. Why would the script turn to the directory that isnt being called or even a different drive path for that matter?
Since then has happen I have added a -Include .pdf so that it will only move. This change is bolded and italicized below . This should work correctly even if system32 is somehow directed again, correct?
##Hashtables
$srcRoot = (Get-ChildItem -Path 'D:\Groups\Anoka\10thDistrict\eSignature and Workflow\Unsigned\*\*\Signed - Return to Court Admin' -Directory).FullName
$dstRoot = "D:\Groups\Anoka\10thDistrict\eSignature and Workflow\Signed"
# map is defined
$map = @{
'02' = '02 Anoka Court Admin Signed'
'13' = 'PICK Court Admin Signed'
'30' = 'PICK Court Admin Signed'
'33' = 'PICK Court Admin Signed'
'58' = 'PICK Court Admin Signed'
'71' = '71 Sherburne Court Admin Signed'
'82' = '82 Washington Court Admin Signed'
'86' = '86 Wright Court Admin Signed'
'CS' = 'Centralized Support Unit Signed'
}
foreach ($file in Get-ChildItem -Path $srcRoot -File ***-Include *pdf**** -Force -Recurse) {
$key = $file.BaseName.Substring(0,2)
if ($map.ContainsKey($key)) {
$fileName = $file.Name
$dstDir = Join-Path -Path $dstRoot -ChildPath $map[$key]
if (-not (Test-Path -Path $dstDir)) {
# Create destination directory below if needed for duplicates
}
if (Test-Path -Path (Join-Path -Path $dstDir -ChildPath $fileName)) {
# Error message if file already exists at destination
Write-Error -Message "File $fileName already exists at $dstDir"
# go next here
continue
}
# OK to move file
Move-Item -Path $file.FullName -Destination $dstDir
# go next here
continue
}
# this is else for -not Contains()
$dstDir = Join-Path -Path $dstRoot -ChildPath 'District Wide Signed - Error Naming Convention'
# should test if `Errors` folder exists, but should exist
if (-not (Test-Path -Path $dstDir)) {
# Create destination directory below if needed for Errors
}
if (Test-Path -Path (Join-Path -Path $dstDir -ChildPath $fileName)) {
# throw error here and if needed
# go next after error
continue
}
Move-Item -Path $file.FullName -Destination $dstDir
}