Rename File from CSV List

Welcome Forums General PowerShell Q&A Rename File from CSV List

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #110884
    Tom

    Participant
    Points: 1
    Rank: Member

    Hello, I'm trying to rename files that match values in column one of a csv adding the value in column 3 to the beginning of the file name leaving the rest of the file name intact. Here is what I have so far. I cant seem to figure out the Rename-Item. I get the error "Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no input."

    # Common Paths
    
    $PathRoot = "C:\Temp\HIPAASuite" #Where the files are to rename
    
     
    
    # Get csv file
    
    $ClientAccounts = Import-CSV -path "\\server\some\path\to\csv\file.csv"
    
     
    
    # Get each file and rename it
    
    ForEach($row in $ClientAccounts)
    
    {
    
    $CurrentClientTaxId = $row[-1].TaxId
    
    $CurrentClientName = $row[-1].ClientName
    
    #loop through files
    
    $FileExists = Test-Path -Path "$PathTotal\*$CurrentClientLB_Number*" #See if there is a file.
    
    If ($FileExists -eq $true) #The file does exist.
    
    {
    
    #ReName File
    
    Rename-Item -Path $PathRoot -NewName {$CurrentClientName + " " + $_.name}
    
    }
    
     
    
    }
    
    
  • #110905

    Participant
    Points: 28
    Rank: Member

    You don't need to use a script block there for the new name parameter – just need to create the string that you want to work with. As per your original code above with a space between currentClientName and $_.name:

    Rename-Item -Path $PathRoot -NewName "$($CurrentClientName) $($_.name)"

The topic ‘Rename File from CSV List’ is closed to new replies.