Rename file with multiple CSV info

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  "*$($*" } | 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

So… what specifically do you need help with?

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

$newfilename = "$($_.ColumnA)-$($_.ColumnB).mp3"

Like that?

Yes but how do incorporate that into the pipeline?

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  "*$($*" } | 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  "*$($*" } | dir -Include *.wav, *.mp3 | copy-item -Destination $SourceDir -Force -PassThru | Rename-Item ... }

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

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

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)" }


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.

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

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

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

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