Sunday, 18 August 2013

Weird behaviour in PowerShell bulk file renaming

Weird behaviour in PowerShell bulk file renaming

I'm new to PowerShell and recently I try to rename 120 files in a folder
and I encounter a weird behavior. What I have is files with named from
0001.txt, 0002.txt, ... 0120.txt a total of 120 files. I want to add an
'a' in front of each of the file name. I come up with this command:
ls | ren -newname {$_.name -replace '(\d+)','a$1'}
But after execution, I get numerous error like this: "The specified path,
file name, or both are too long. The fully qualified file name must be
less than 260"
And when I look into my folder, all I get is file name from
aaaaaaaaaaaaaaaaaaaaaaaaa(repeat until it hit system limit on path
length)....a0001.txt ... ... ... aaaaaaaaaaaaaaaaaaaaaaaaa(repeat until it
hit system limit on path length)....a0120.txt
Upon further inspection using the -cf switch, turn out that PowerShell
attempt the renaming process recursively. After first pass of renaming all
the 120 files, it went on again applying the command to a0001.txt
effectively adding another 'a' in front of the filename. And this went on
until it hit the path length limit and reporting error.
Can anyone tell me if there is anything wrong in my renaming command?

No comments:

Post a Comment