I worked though this bit of code a while back and with the help of the folks around here, this has been one of the best tools I have ever had. Simply, this is a script that look at a file name and breaks it apart to file it in to separate locations. The only complaint I have had is it is case sensitive. My files are structured as “Job Name - Vendor Name - Acct Name - PO number”.pdf. If I Use “job name” instead of “Job Name” it creates two separate folders even though the capitals are the only difference. I would really like for this script to be a little less picky about that. Code Follows and Thank you for the help.
Get-ChildItem -path "C:\Users\treaves\Documents" -Filter *.pdf | foreach {
#saving the $_ is unnecessary but makes the example more readable
$currentFile = $_
#split the values
$Community,$vendor,$work,$po = $CurrentFile.basename -split "-" | foreach {$_.Trim()}
#This is where my code begins
#This should create a vendor folder
$Vendordestination = Join-Path -Path "P:\SyncFolder\Dropbox\Vendor Files" -ChildPath $Vendor
#if the destination folder does not exist create it
if (-not (Test-Path $Vendordestination))
{
New-Item -Path $Vendordestination -ItemType "directory" -Force
}
#Hopefully this will copy the file based on the vendor
Copy-Item -Path $currentFile.fullname -Destination $Vendordestination -Force
#create the path to make working with it easy and error proof
$Cmtdestination = Join-Path -Path "P:\SyncFolder\Dropbox\Builder Folder" -ChildPath $Community
#if the destination folder does not exist create it
if (-not (Test-Path $Cmtdestination))
{
New-Item -Path $Cmtdestination -ItemType "directory" -Force
}
#add force to rewrite files in the destination folder
Move-Item -Path $currentFile.fullname -Destination $Cmtdestination -Force
#add second test path, destination and move here to move to another location.
}