Revising Search Parameters

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5363

    by treaves04 at 2012-12-18 07:36:11

    I have found a bit of code that is almost a perfect fir for my situation. I have read through it and searched for other sources to help me modify it to suit my particular need but have not been successful with it. The Code I found is at: http://blogs.technet.com/b/heyscriptingguy/archive/2008/06/03/hey-scripting-guy-how-can-i-move-files-based-on-their-three-part-file-name.aspx

    The Code is as follows:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder("C:\Invoices")

    For Each objFile in objFolder.Files
    strVendorName = Left(objFile.Name, 3)
    strMonth = MonthName(Mid(objFile.Name, 4, 2))
    intYear = Mid(objFile.Name, 8, 4)

    strFolderPath = "C:\Archive\" & strVendorName & "\" & intYear & "\" & strMonth & "\"

    If Not objFSO.FolderExists(strFolderPath) Then
    Set objFolder2 = objFSO.CreateFolder(strFolderPath)
    End If

    objFSO.MoveFile objFile.Path, strFolderPath
    Next

    This article explains my situation very well, the biggest difference is the file name. Rather than a coded file name, mine are very exact. My File Names are always "Lot Community – Vendor Name – Work Description – PO Number.pdf" Yes, it has the "[space]–[space]" between each one. I have managed to make powershell move every PDF in the directory to the right target, but not to folder name based on the "Lot Community". I cannot use the counting method described in this article because my file names vary in length (ie 5 Community one, 55 Community Two, and 555 Community Three).

    If someone can help me with this I think I can use that knowledge to then copy the same files to another location based on the vendor name before moving them to the "Lot Community" folder. Thanks in advance.

    by Klaas at 2012-12-18 08:29:29

    Isn't that a VB script? Today we do things very different, we have PowerShell V3 now 😉
    If you describe your goal a little more in detail, we probably will find a simpler solution.
    For splitting up the file name there is a .split("-") method so we don't have to count characters.

    by nohandle at 2012-12-18 08:51:33

    As Klass said the code posted is VB script. I recall similar question was asked already (viewtopic.php?t=854). What part of the script posted by Richard did not suit you? We can build on that.
    To simply split string that looks like this "Lot Community - Vendor Name - Work Description - PO Number" -split "-" (you are gonna use .basename property and it does not return the extension)
    you do just this"Lot Community - Vendor Name - Work Description - PO Number" -split "-"and you get
    Lot Community
    Vendor Name
    Work Description
    PO Number
    it still has the spaces in it so you have to trim them using the .trim() method on each string. "Lot Community - Vendor Name - Work Description - PO Number" -split "-" | foreach {$_.Trim()}and you getLot Community
    Vendor Name
    Work Description
    PO Number

    now based on the position of the items save them to appropriately named variables:
    $Community,$vendor,$work,$po = "Lot Community - Vendor Name - Work Description - PO Number" -split "-" | foreach {$_.Trim()}

    now you have the data and you can use them to create the results you want.

    by treaves04 at 2012-12-18 08:59:59

    Thank for the reply. I am going to feel REALLY foolish is I have been trying to make a VB Script work in PS. My goal is very simple. I would like to create a PS script that will do two things to the PDF's I create: First, copy them based on the vendor to a vendor specific folder, and second move the file to a "Lot Community" specific folder. Yes, there will be two copies of the same file when I am done. This is because I will be allowing my vendors to login to their folder, but my community manager still needs to be able to find all his files in one place as well.

    For example, a file named "5 Old Cove Huntsville – Hazel Green Bldg Supply – Framing Materials – PO 518824.pdf" needs to end up in two places:
    1: A Vendor Folder Named "Hazel Green Bldg Supply"
    2: A Community Folder Named "5 Old Cove Huntsville"

    Thanks again, hope this makes sense.

    by nohandle at 2012-12-18 09:25:59

    try this:
    Get-ChildItem -path "c:\temp" -Filter Lot* | foreach {
    #saving the $_ is unnecessary but makes the example more readable
    $currentFile = $_
    #split the values
    $Community,$vendor,$work,$po = $CurrentFile.basename -split "-" | foreach {$_.Trim()}
    #create the path to make working with it easy and error proof
    $destination = Join-Path -Path "c:\temp" -ChildPath $Community
    #if the destination folder does not exist create it
    if (-not (Test-Path $destination))
    {
    New-Item -Path $destination -ItemType "directory" -Force
    }
    #add force to rewrite files in the destination folder
    Move-Item -Path $currentFile.fullname -Destination $destination -PassThru
    #add second test path, destination and move here to move to another location.
    }

    by treaves04 at 2012-12-18 12:05:44

    This works splendidly. I have added a little to it to handle first copying the file based on the vendor. Unless you see a problem with the copy I am using all I can say is thank you for your kindness and patience. This makes a HUGE difference to what I am doing.

    Get-ChildItem -path "c:\PSTEST" -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 "c:\PSVEndorTest" -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 -PassThru
    #create the path to make working with it easy and error proof
    $POdestination = Join-Path -Path "c:\PSPOTest" -ChildPath $Community
    #if the destination folder does not exist create it
    if (-not (Test-Path $POdestination))
    {
    New-Item -Path $POdestination -ItemType "directory" -Force
    }
    #add force to rewrite files in the destination folder
    Move-Item -Path $currentFile.fullname -Destination $POdestination -PassThru
    #add second test path, destination and move here to move to another location.
    }

    by nohandle at 2012-12-18 12:52:40

    Hi, you are welcome.:) Next time you post some code tag it as powershell (by the powershell button that is the right most on the edit buttons panel), it make the code more readable for every one.
    $POdestination = Join-Path -Path "c:\PSPOTest" -ChildPath $Community
    I don't get why you call the variable POdestination and you use the Community part of the path to set it up.
    Make sure you understand everything in the script. And please mark this thread as solved if your issue is solved.

    by treaves04 at 2012-12-18 14:03:22

    Just the way I think about it. Up to this point I have not kept the vendor directory at all because it would have been too much work. Not that you point it our, that does make a lot more sense. I will update it to prevent future confusion.

You must be logged in to reply to this topic.