in reply to Rename All Files In a Directory

Code has been updated considering sugestions. However the error message produced is now :
. didn't match Could not rename:Device or resource busy at rename0 line 25.
Even with the closedir correction.

Replies are listed 'Best First'.
Re^2: Rename All Files In a Directory
by aukjan (Friar) on Jul 01, 2005 at 11:38 UTC
    This is still because you don't check if the file returned is a directory.... In this case it matches '..', so the result is that $1 and $3 are empty, and the image name is '.'. When you try to rename you try to rename  /path/to/directory/. to  /path/to/directory._. To fix this Incorporate a check to see if it is a file or an directory, like mentioned in an earlier post: Re: Rename All Files In a Directory
Re^2: Rename All Files In a Directory
by dReKurCe (Scribe) on Jul 01, 2005 at 12:24 UTC
    Well thanks for the suggestions.I made some upgrades to the code but ultimately wanted to get the job done:so I forged forward at the cmd line and solved the problem with:
    perl -e 'opendir(DH,"/home/yehuda/img/jpg");while(defined ($file=readd +ir(DH))){$file=~m|(.*)\.(.*)\.(.*)|g; rename ($file, $1.$3)}'
Re^2: Rename All Files In a Directory
by polettix (Vicar) on Jul 01, 2005 at 11:06 UTC
    It would be nice to see the corrected code - I also spotted a syntax error ($success is set inside the if, but then you use $sucess, with one less "c"). You could also put the print line before the rename, just to be sure that you're doing the right thing (note the brackets, just to be sure that there are no spaces):
    #.... print "About to rename [$path] to [$target]\n"; rename ($path,$target) or die "Could not rename: $!"; #...
    Moreover, you're probably on some kind of Win32 platform. Are you sure that the file you're trying to rename isn't open by some other application that is "locking" it?

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.