Author Posts

November 19, 2013 at 11:21 am

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.
}


	
			
	

November 19, 2013 at 11:34 am

Are you positive that the case of the letters is the only difference? The FileSystem provider that's handling those paths in Test-Path and New-Item is not case sensitive; it shouldn't be behaving the way you're describing. For example:

$Path = Join-Path -Path $env:temp -ChildPath 'SomeNewFolderName'

Test-Path $Path  # False

$null = New-Item -Path $Path -ItemType Directory

Test-Path $Path  # True

$Path = $Path.ToLower()
$Path

Test-Path $Path  # True

New-Item -Path $Path -ItemType Directory  # Throws an error because the item already exists

November 19, 2013 at 11:42 am

Completely Certain. I create the PDF from an excel script every time. I have worked around it by setting up tables to look up the exact same name in most cases, but when I have a vendor that is not on that list and it does not auto populate it, If I do not type the name exactly including case, it will make new folders. 90% of the time, its not a problem, which is great, but the other 10% of the time is nearly impossible to notice until I am trying to track it down.

November 19, 2013 at 12:21 pm

Fair enough. I can't explain why it's giving you this problem, but if we throw out the assumption that the file system is not supposed to be case sensitive, you could try this:

$textInfo = [System.Globalization.CultureInfo]::CurrentCulture.TextInfo

Get-ChildItem -path "C:\Users\treaves\Documents" -Filter *.pdf | foreach {
    $currentFile = $_
    $Community,$vendor,$work,$po = $CurrentFile.basename -split "-"  | foreach { $textInfo.ToTitleCase($_.Trim()) }
    
    # ...
}