Bulk Rename-Item from CSV

This topic contains 4 replies, has 2 voices, and was last updated by  Will Anderson 2 years, 10 months ago.

  • Author
    Posts
  • #22095

    Aaron Rouse
    Participant

    Good evening everyone, hoping someone might be able to tell me where I am missing some logic here. The following script works when there is one item in the csv file. Whenever there is more than one item, it fails with "Rename-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported." The header "PointerToSource" is a CIFS file path that is accessible. Like I mentioned, it works great with a single row in the CSV file, but as soon as there are more than one row besides the header, it fails.

    This is my first post, but I've been around and certainly appreciate all help I've gained from others postings.

    $filenames = Import-Csv "H:\docs\111\import_d1.csv"            
    foreach [$file in $filenames]            
    {            
        $filepath = $filenames.pointertosource            
        $fileFirstname = $filenames.import_first_name            
        $fileLastname = $filenames.import_Last_name
       
        
        Rename-Item $filepath -NewName $filefirstname-$filelastname.tif
    

    Respectfully,

    Aaron Rouse

  • #22096

    Will Anderson
    Keymaster

    Hey there Aaron!

    In your ForEach statement, change the references to $filenames to $file.

    Sorry. Just realized that might be a little vague. Try this?

    $filenames = Import-Csv "H:\docs\111\import_d1.csv"
    foreach ($file in $filenames)
    {
    $filepath = $file.pointertosource
    $fileFirstname = $file.import_first_name
    $fileLastname = $file.import_Last_name

    Rename-Item $filepath -NewName $filefirstname-$filelastname.tif

    }

    This is actually something I do to myself all the time. 🙂

    • #22147

      Aaron Rouse
      Participant

      Thanks for the reply! I should have a test on this sometime this morning.

    • #22154

      Aaron Rouse
      Participant

      Thanks much! The specified change was all I needed, and I'm well on my way to the nearly 100k file rename operations that had to take place. I'm still a noob, and I appreciate your time!

  • #22220

    Will Anderson
    Keymaster

    No problem Aaron! I'm still pretty new myself, but I try to help where I can. 🙂

You must be logged in to reply to this topic.