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.