1

I have a structure with multiple subfolders and I would like to rename the file and (sub)folders by introducing a code with a structure as follows:

1 (folder) 1.1 (subfolder) 1.1.1 (file)

or alternatively attribute a specific name from a CSV. I have tried with

 $csv="path\rename.csv" #path to csv file

$folder= Get-ChildItem "path"-Recurse -File #target folder containing files

cd ($folder); import-csv ($csv)| foreach {rename-item -path $_.path -newname $_.filename}

but the process is iterative then it stops with errors because in the meanwhile the up-directory has changed name...

Thanks for any help.

UserEC
  • 11
  • 1
  • Please edit your question to clarify. Provide sample existing folder/file names and sample `.csv` data. Are you saying a sample filepath is `1\1.1\1.1.1`??? If you want to rename both subfolders and the files within them, and your `.csv` references the current path, your code will need to either rename the files first and then the folder that contains them, or rename the folder and then reference the files by their new path. You cannot re-name the subfolder and file in a single command. – Keith Miller May 17 '22 at 14:03
  • `$folder= Get-ChildItem "path"-Recurse -File` is going to create an array of FileInfo objects, not a single directory, so `cd ($folder)` is sure to throw an error. The parenthesses are unnecessary as well... – Keith Miller May 17 '22 at 14:08
  • 1
    when dealing with changes in a dir tree ... start from the _bottom_ of the tree. [*grin*] also ... don't use the pipeline since that can make changes **_and then read the change as something new that needs to be processed_**. – Lee_Dailey May 18 '22 at 08:12

0 Answers0