dReKurCe has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks:

I am attempting to rename all files in a directory based on a regex match of a compund file name.I have followed the example of The Perl Cookbook 9.9

The code as follows generates the following error mesage:
Could not rename:Device or resource busy at rename line 22.

Any insight into the nature of this error would be appreciated.
#! /usr/bin/perl $dir="/home/yehuda/img/jpg/"; opendir(DH,$dir)or die "FOO BAR! $!\n"; while (defined ($img=readdir DH)){ if ($img=~m/^(.*)\.(.*)\.(.*)/g){$success=1}; if ($sucess==1){ $newname="$1.$3"; }else{ print "$img didn't match\n"; } push @names,$img if $sucess==1; push @newnames,$newname if $sucess=1; } closedir DH; foreach $name(@names){ $i=$i+1 ; $newname_=$newnames[$i-1]; $path="$dir/$name"; $target="$dir$newname_"; rename ($path,$target) or die "Could not rename:$!"; print " $path is now $target \n"; ; }

Replies are listed 'Best First'.
Re: Rename All Files In a Directory
by aukjan (Friar) on Jul 01, 2005 at 09:33 UTC
    You are also trying to rename the directories and anything else listed (and matched) in that directory. (such as '..'). This is probably not what you want to do:

    Try this to read in only the files.
    @files = grep { -f "$dir/$_" } readdir(DH);
    UPDATE: Or in chapter 9.5 of the Cookbook:
    use DirHandle; sub plainfiles { my $dir = shift; my $dh = DirHandle->new($dir) or die "can't opendir $dir: $!"; return sort # sort pathnames grep { -f } # choose only "plain" files map { "$dir/$_" } # create full paths grep { !/^\./ } # filter out dot files $dh->read(); # read all entries }

    also please do not link to 'illegal' copies of books here...
Re: Rename All Files In a Directory
by anonymized user 468275 (Curate) on Jul 01, 2005 at 09:35 UTC
    My first impression is that you tried to close DH with close instead of closedir and perhaps this is why the resource (the directory) is busy and can't then accept the renames.

    One world, one people

      Good catch! This is the most reasonable guess so far. I didn't know that failing to closedir may have this side effect. But then I rarely explicitly myself.
Re: Rename All Files In a Directory
by blazar (Canon) on Jul 01, 2005 at 09:29 UTC
    As a general rule, you're missing two important lines:
    use strict; use warnings;
    Said this, I would use glob, rather than opendir & friends. In any case your code doesn't seem too bad, for you take care of specifying the full path to your files. But notice that
    $dir =~ m|/$|; # Do you get the point?!?

    Update: I'm an idiot - it had been chopped, and now I see why you did do it in the first place. Still, it makes for clumsy logic and code, IMHO. PS: $dir does not need to be chopped, and if it did, then it would have been better chomped instead. Also, consider using a more effective indenting style.

    Update: ditto as the (update) above!

    PS2: now that I look at your script more closely, I feel like pointing out too that since it's not Perl6 yet (thus we do not have zip), your logic is somewhat awkward: why not doing it all in one iteration rather than in two?!? That is what most people would do even if we already had better means to iterate over two arrays in parallel - but maybe even then an AoA would be preferable. TMTOWTDI, you know...

      A little walkthrough
      $dir="/home/yehuda/img/jpg/"; chop $dir;
      Here you hardcode a string and immedeatly chop off the last char. I cannot make any sense of that.
      while (defined ($img=readdir DH)){ $img=~m/^(.*)\.(.*)\.(.*)/g; $newname="$1.$3";
      Here you do not check if the regex matches. If there is file that does not meet your assumptions it will horribly fail.
      push @names,$img; push @newnames,$newname; }
      I'm not sure why you don't rename the files immedeatly, but ok. i can live with that. Even if you really have to store the filenames somewhere i would use a hash for that. old name as key, new name as value.
      close DH;
      close() closes a filehandle, not a dirhandle. use closedir();
      foreach $name(@names){ $i=$i+1 ; $newname_=$newnames[$i-1];
      Now that is the fun part. You add 1 to $i just to substract the 1 again when you use $i. Better to do the addition after the usage and leave out the substraction.
      $path="$dir/$name"; $target="$dir/$newname_";
      Two unneccessary variables.
      chomp($target,$path);
      Why chomp? there is no newline insight.
      rename ($path,$target) or die "Could not rename:$!"; print " $path is now $target \n";
      I'd prefer a warning here instead of sudden death.

      After all I doubt that you got this code from the cookbook. And if so, throw it into the trashcan. Slightly improved:
      $dir="/home/yehuda/img/jpg"; opendir(DH,$dir)or die "FOO BAR! $!\n"; while ( readdir DH) { if ( -f "$dir/$_" && $m/^(.*)\.(.*)\.(.*)/ ) { warn "cannot rename $dir/$_\n" unless rename "$dir/$_", "$dir/$1.$3"; } } closedir DH;


      holli, /regexed monk/
        $dir="/home/yehuda/img/jpg/"; chop $dir;
        Here you hardcode a string and immedeatly chop off the last char. I cannot make any sense of that.
        It does have some minimal amount of sense, if seen in the awkward context of all the rest of the code. Probably the trailing / conveys him the psychological feeling of having to do with a directory, but then later he does $dir/$somethingelse. Not to justify him - just trying to understand...

        Incidetally, why did you post your reply to the OP as a reply to my own reply?!?

Re: Rename All Files In a Directory
by blazar (Canon) on Jul 01, 2005 at 09:51 UTC
    2nd try! How about something along the lines of:
    #!/usr/bin/perl use strict; use warnings; my $dir='whatever'; chdir $dir; for (glob "*.*.*") { my ($u, undef, $v) = split /\./, $_, 3; my $dest="$u.$v"; rename $_, $dest or die "Can't rename `$_' to `$dest': $!\n"; print "`$_' => `$dest'\n"; } __END__
    (untested)
Re: Rename All Files In a Directory
by dReKurCe (Scribe) on Jul 01, 2005 at 10:28 UTC
    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.
      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
      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.
      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)}'