in reply to Re: rename files
in thread rename files

You provided this code:
open(my $fh, $namefile) or die "$namefile: $!"; while (<$fh>) { rename ($_, "testfile$_") or warn "rname $_: $!"; } close($fh);

This does not meet the spec given, even if you chomp the lines. Even if the data file was changed to contain the current names of the files, it wouldn't meet the spec given. The names of the files pbaumgar has and the names desired cannot be reconciled by concatenation alone.

RoyCrowder's solution achieves the desired effect if the data file all follows from the example. Otherwise, keeping the file of new filenames as in the spec would be more desirable, like this minor modification to RoyCrowder's existing code:

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; my @files = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR); closedir(DIR); @files = sort {$a <=> $b} @files; # Ascending sort open my $names, '<', 'newnames.txt' or die "Cannot read newnames.txt: +$!\n"; foreach (@files) { my $name = <$names>; chomp $name; rename($_, $name) or warn "rename: $_ : $!"; }

Replies are listed 'Best First'.
Re^3: rename files
by lostjimmy (Chaplain) on Apr 08, 2009 at 16:49 UTC

    Your solution does meet the spec, but it's really just a disaster waiting to happen. Keeping the directory contents and the names file in sync will be a challenge and really seems unmaintainable.

    I'd really be interested in what pbaumgar is actually trying to solve here.

      The situation which pbaumgar is in is certainly a mess.

      If possible, RoyCrowder's solution is preferable so there is no file to synchronize. Without being sure the rest of the data meets the same restrictions, though, we can't tell if it does do the right thing.

      Either way, I would hope that this is a one-time conversion and not something that has to be repeated. Hopefully if there's an ongoing process that produces these files it will in the future just output them in the new naming scheme rather than requiring repeated conversions.

      As you say, it could be really helpful to know more about the situation.

      lostjimmy hit the nail on the head. That is why I wrote my snippet in such a manner as to exclude any use of an external names file. My snippet below will read the directory instead of using a pre-written sorted file.

      The Web is like a dominatrix. Everywhere I turn, I see little buttons ordering me to Submit. (Nytwind)
        I've said in two places now that if the desired new names all follow from the example that your solution is preferable to one that uses the separate file. If the desired new names do not follow from the example, then you've changed the behavior by deviating from the spec.

        Someone shouldn't have named a bunch of files with just digits to begin with. Just naming them "testfile1" through "testfilen" actually isn't much better for sake of description. One would hope there are no sequence holes in the new naming scheme at least, but without pbaumgar saying so we can't be sure.

      Thank you to everyone for the pointers in getting this working. Here's what I'm trying to solve:

      I have about 500 text files from an old wiki. The file names are all numbers. I have a dump of from the mySQL database of the wiki, from which I extracted the descriptions of each file. I want to rename each file to it's corresponding description. For example I have file '2', which the description is 'Server Build'. I want to rename the files to their description so I can then import them into a new wiki. The new wiki has a import utility script which you can pass it a file name and a title, however it only accepts one file at time.

      Thanks again everyone, I think have enough to get me going.