Rename file with multiple CSV info

This topic contains 12 replies, has 2 voices, and was last updated by Profile photo of H Man H Man 2 years, 3 months ago.

  • Author
    Posts
  • #18481
    Profile photo of H Man
    H Man
    Participant

    I have a csv file with 2 columns. Column A is Phone and Column B is LeadID.

    I am identifying files on an FTP server, due to the amount of files I am also using a 3rd party indexing tool called everything( this is the 'es' in my for each loop)

    What i would like to do is use column A to identify the files , copy them to a new location and rename the file column a + b.(phone# + leadID).wav or (phone# + leadID).mp3

     
    
    $SourceDir = 'c:\Source'
    $importFile = 'c:\somefile.csv'
    
    import-csv  $ImportFile  | foreach { es  "*$($_.phone)*" } | dir -Include *.wav, *.mp3 | copy-item -Destination $SourceDir -Force
    
    Get-ChildItem -Path $SourceDir |
        foreach { 
        $ErrorActionPreference = 'silentlycontinue'
            if ($_ -match "(\d{10})" ) {
                Rename-Item -Path $_.FullName -newName "$($matches[0])$($_.Extension)" -Force
            }
        }
    
    

    any help would be greatly appreciated

  • #18483
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So... what specifically do you need help with?

  • #18484
    Profile photo of H Man
    H Man
    Participant

    I would like to create a new file name with both fieldls from the csv file column A + column B . so for example column A has phone #'s and column b has lead id's

    the phone # is already in the file name with additional string info. I would like to rename the file for example 5615552212-lead124.mp3 (the new name would be generated by the data in the 2 columns plus the existing ext.)

  • #18486
    Profile photo of Don Jones
    Don Jones
    Keymaster
    $newfilename = "$($_.ColumnA)-$($_.ColumnB).mp3"
    

    Like that?

  • #18487
    Profile photo of H Man
    H Man
    Participant

    Yes but how do incorporate that into the pipeline?

  • #18488
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Sorry. I didn't understand that you were asking how to do all of that in a one-liner – you weren't specific.

    I think you're probably making things more difficult on yourself by trying to put all of this into a one-liner, instead of a structured ForEach loop. When you start needing to carry out multiple operations, a one-liner isn't really the perfect approach. But you can do it.

    foreach-object {
      es  "*$($_.phone)*" } | dir -Include *.wav, *.mp3 | copy-item -Destination $SourceDir -Force
      rename-item ...
    }
    

    The ForEach-Object script block can contain multiple commands. Separate them either with a carriage return, as I've done in this example, or with a semicolon. Also notice that Copy-Item has a -PassThru switch. When you use it, Copy-Item will output the copied item. That could potentially be piped to Rename-Item.

    foreach { es  "*$($_.phone)*" } | dir -Include *.wav, *.mp3 | copy-item -Destination $SourceDir -Force -PassThru | Rename-Item ... }
    

    Does either of those help accomplish what you are trying to do?

  • #18489
    Profile photo of H Man
    H Man
    Participant

    Thanks Don I am going to try it out. I'll let you know

  • #18497
    Profile photo of H Man
    H Man
    Participant

    Its working but , its not including the file ext

    import-csv  $ImportFile | foreach { es  "*$($_.phone1)*" |  dir -Include *.wav, *.mp3 | Where $filter  |  copy-item -Destination $FileDir -Force -PassThru | Rename-Item -NewName "$($_.phone1)-$($_.leadid)$($_.Extension)" }
    
    [/pre[]
    
    
    
  • #18498
    Profile photo of Don Jones
    Don Jones
    Keymaster

    That's because $_ represents the output of Import-CSV, which I presume does not include a $_.Extension column. See, that's why doing these in a one-liner is so much more complicated. $_ can't represent two different things.

    If this HAS to be a one-liner, as opposed to a script ForEach block, it's going to end up being more complex. You'll have to save the original $_ to another variable at some stage, and then pipe Copy-Item to a second ForEach-Object, so that you can access two pipeline objects at once.

  • #18499
    Profile photo of H Man
    H Man
    Participant

    Hey Don it does NOT have to be a one liner. Can u please show me another example. Thanks so much

  • #18500
    Profile photo of Don Jones
    Don Jones
    Keymaster
    $entries = Import-CSV whatever
    foreach ($entry in $entries) {
    
    }
    

    Inside the ForEach loop, $entry represents on CSV file entry. So, $entry.phone1 is the "Phone1" column.

    $entries = Import-CSV whatever
    foreach ($entry in $entries) {
      $copied = es "*$($entry.phone1)*" | dir -Include *.wav, *.mp3 | Where $filter | copy-item -Destination $FileDir -Force -PassThru
      foreach ($copy in $copied) {
        $copy | Rename-Item -NewName "$($entry.phone1)-$($entry.leadid)$($copy.Extension)"
      }
    }
    

    See how the nested loops let you access two sets of input, $entry and $copy? I mean, I can't test this, so I don't know that it is a perfect solution, but it's an example of a more structured approach rather than a one-liner. Hope it helps.

  • #18501
    Profile photo of H Man
    H Man
    Participant

    It really did help thank you Don! I really appreciate it

  • #18503
    Profile photo of H Man
    H Man
    Participant

    worked great! A perfect solution!! thanks so much Don

You must be logged in to reply to this topic.