Renaming Files

Hi Team,

I am trying to create a one / two liner script for renaming files under a folder. I might have some conceptual errors or something else, for that I unable to perform the same. I have a text file with new file names. Can anyone suggest how should I proceed?

I tried this one;
$filenames= gc C:\rename.txt
dir C:\Cont2Ren |foreach {Ren $_.Name $filenames}
And getting the following error;
Rename-Item : Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter ‘NewName’. Specified method
is not supported.

Regards,
Roy.

You really need to show the filename list you are passing in and what the filenames on disk you want to rename for folks to assist you. Of course only post sanitized version. As for your errors.
You are using collection… Ren $_.Name $filenames
…without specifying the full file path single filename.

That is what this 'Cannot convert ‘System.Object’ means.

Your code is also not correct for what you are trying to do. You are telling PoSH to rename every file on disk to the same names coming from the text file, regardless of whatever is really there. That is not a valid request, PoSH notwithstanding.

What is the filename being read in from the text file?
How are you determining if the filename on disk, even exists, let alone what it should be renamed to relative to the filename in the text file?
If the filename in the text file is the same as on disk, what are you trying to rename the file on disk to, since they would be the same?
If you are trying to use all filenames from the text file, you have to iterate through each one individually and match them to the filenames on disk, in some way.

So,
First you must determine if the file is even on disk
Then determine what about the filename on disk matches a criteria to warrant a name change.
Match the filename criteria specifics to specifics of the filename in the text file
Rename based on the match.

Hi,

I was in a hurry so I post in brief. Also, I am trying to do as per old dos concept. Ex. Ren current_name new_name. I am working with a folder (not entire disk) under that multiple subfolders are there and under each subfolder, there are 12-18 files with sequentially named like 1,2,3… All file types are same (excel). Also, I have text files for each folder which contains the new names without extension. These names also are in sequence. Ex. First line name will go to 1.xls and so on. Yes, the text file also contains the new names in each line.

What I was planning, for each subfolder I’ll navigate there and execute the script. So I gc the file contents in a variable and using get-childitem and for each I’ll rename the files. But in reality that is not happening. Also, I am executing the script from the same location(inside subfolder), so ignoring the path parameter.

I hope you can understand my structure. Also please help me, if I want to append new name with the old name. Ex. “1 Planning.xls”, “2 meeting.xls”.
I am not very experienced in PoSH, still learning and trying to accomplish. So if you find any silly mistakes please guide accordingly.

Regards,
Roy.

Understood, but folder or full disk, still means it’s on disk. 8^}

As for the parent and subfolder thing.
This, just means, use the -recurse switch to get the parent and all subfolders
but you still must defined the FullName path to get to them

(Get-ChildItem -Path ‘D:\Temp’ -Recurse).FullName

As for your text file, you say what is in it, but don’t show a sample set of rows, for what it really looks like.
Is it a CSV (with just a .txt extension), what the delimiter, etc?

So, a quick cut and paste of the text file rows ( no more than three rows) as well as a quick cut and paste of a gci command out put of the parent folder (again only a three or so rows.), using the command provided above will help clarify things more.

I am old, and very visual kind of person. 8^}

Hi,

but folder or full disk
It's a folder resides on C: Please find the screenshot for clearing the idea. Folder Structure Text File contain Names CSV file contains names I have also tried with a CSV file with same contents. But same result again. Please note, this is demo content for making you understand. As per the screenshot, I want to rename the files with the following script.
$file= Import-Csv C:\rename.csv dir C:\Cont2Ren |foreach {Ren $_ $file}

The output error can be found on the attached link.
Error Screenshot

Please guide me as per shared screenshot. What mistake I am doing with this case? If I want to rename this files, what I have to write? Please find the expected output (done manually).
Result I am Expecting

Regards,
Roy.

OK, I know I said I was a very visual kind of guy 8^},

But the below is what I expected to see in your reply.
Not that the pictures did not help. I just try to keep things as simple as possible.

Now, the pictures show my exact concerns with your effort.

1 - The text file and the CSV are the same data and neither file has any relationship to what is on disk (in the folder(s)).
2 - You are trying to take a random list of names and use that to rename a random set of files on disk, without telling the code how to do that.
3 - How are these XLS files generated, and why are the using number vs a real name?
4 - How do you know what is in them without opening them in Excel first and if you have / had to open them first to see what is in them and who was the file owner, why not just manually name them when you did that?

You have to match something from the source to the destination for the rename or you are saying for every name in the list change the filename on disk in the order they are read, no matter what is in them or who the owner truly was.

Here is what I hoped to see from you

Sample directory listing of the file on disk
($FilesOnDisk = Get-ChildItem -Path ‘c:\Cont2Ren*.xls’ -Recurse | Sort)
Results

C:\Cont2Ren\Cont2RenSubFolder\4.xls
C:\Cont2Ren\Cont2RenSubFolder\5.xls
C:\Cont2Ren\Cont2RenSubFolder\6.xls
C:\Cont2Ren\1.xls
C:\Cont2Ren\2.xls
C:\Cont2Ren\3.xls

The layout of the rename.txt content, showing any evidence of a matching string to the files on disk
($NewFileNames = Import-Csv -Path ‘C:\Cont2Ren\rename.txt’)

#Results

FileID NewFilename


1 Horak Antonin CZPG
2 Rubinova Ladislava CZPG
3 Capova Helena

The you could do something like this.

Clear-Host;$NewFileNames | %{$FileMatches = ((Get-ChildItem -Path C:\Cont2Ren -File -Recurse) -match $.FileID).FullName
ForEach($FileMatch in $FileMatches){Rename-Item -Path $FileMatch -NewName ($
.NewFileName + ‘.xls’) -WhatIf}}

What if: Performing the operation “Rename File” on target “Item: C:\Cont2Ren\1.xls Destination: C:\Cont2Ren\Horak Antonin CZPG.xls”.
What if: Performing the operation “Rename File” on target “Item: C:\Cont2Ren\2.xls Destination: C:\Cont2Ren\Rubinova Ladislava CZPG.xls”.
What if: Performing the operation “Rename File” on target “Item: C:\Cont2Ren\3.xls Destination: C:\Cont2Ren\Capova Helena.xls”.

If you remove the -WhatIf, the rename would happen
Again, if your text/csv file has no column/info to match to the files on disk. You are stuck with manually renaming these files.