About
This script has developed as a result of one of my work tasks. That task being naming and storing equipment related documents for our division onto a shared drive.
On this shared files for units are stored into a file that is labeled with the equiopments unit number. The issue arrises in that it is a shared drive and storing convention varies between not only how our equiopment manager stores files, but other divisions as well.
There are a few common folder structures for equipment which I use in my script to search for the equipment folders.
\Excavators\EX510
\Excavators\MB\EX510
In comes my script to automate naming and copying of files to the remote server. The script allows me to preview the PDF, rename, make a copy to remote server and archive the orignal on the local machine.
The script works by searching the shared drive for an equipment folder using the previously outlined structres and copying in the file to the target location. If unable to find the equipoment folder searching for any of the defined folder structures it defaults to storing the file in a to sort folder for later sorting.
My script is as follows
function ClosePDF {
$acrobat = New-Object -ComObject "AcroExch.App"
$avDoc = $acrobat.GetActiveDoc()
$avDoc.Close(0)
}
$pfdFiles=Get-ChildItem -Path ".\01_Unsorted" -Filter "*.pdf"
if( $pfdFiles.Count -eq 0) {
echo "No files in folder"
return
}
for ($i=0;$i -lt $pfdFiles.Count; $i++) {
$pdfOldName=$pfdFiles[$i]
Invoke-Item $pdfOldName.FullName
Write-Host "Working File: " $pdfOldName.Name
$input= Read-Host 'Rename'
ClosePDF
if($input -eq "" -or $input -eq "n" -or $input -eq "next") {
continue
}
if($input -eq 'end' -or $input -eq 'exit') {
break
}
$unitNumber= $input.ToString().Substring(0,$input.ToString().IndexOf(" "))
$destinationRemote="P:\Operations\Projects Maintenance\_MBToSort\$unitNumber"
$findPath=Resolve-Path -Path "P:\Operations\Projects Maintenance\*\MB\$unitNumber*"
if($findPath -ne $null) {
$destinationRemote=$findPath
}
$findPath=Resolve-Path -Path "P:\Operations\Projects Maintenance\*\$unitNumber*"
if($findPath -ne $null) {
$destinationRemote=$findPath
}
if(!(Test-Path $destinationRemote)) {
md $destinationRemote
}
$newName="$input.pdf"
Copy "$pdfOldName" "$destinationRemote\$newName"
Move "$pdfOldName" ".\02_Sorted\$newName"
Write-Host "-----------------------------------------------------------------"
}
Read-Host 'Done'