in reply to Re^2: Mass file renaming
in thread Mass file renaming

You didn’t answer my question. I didn’t want to know what you think is in the variables; that’s just programming by coincidence. 99% of the bugs I encounter are due to variables containing things are than what I expected they would. Put an actual warn "$newfile $oldfile" if not $success; or some such in your code and make sure you know what the variables contain.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^4: Mass file renaming
by Anonymous Monk on Nov 07, 2005 at 15:39 UTC
    Hi

    You're right, I didn't, but I already have the following code which does what you say, so I do know that the vars going in to the rename are good:

    print qq{Attempting to rename $oldname to $newname\n};

    $success=rename $oldname, $newname;

    if ($success){print "Changed $oldname to $newname\n"}

    else {print "Failed to rename $oldname to $newname $!\n";exit};

    so I do actually know what's in there and its right enough - eg renaming Greenberfield.csv to 573427RSSt.csv

    Thanks again.

      Ah, okay. It would help to say something along those lines (“I’ve printed the variables, their values are as expected”) right from the start, so people know that’s not it.

      Makeshifts last the longest.

      Please read Writeup Formatting Tips, the code in your posts is quite difficult to read.

      This has probably nothing to do with your problem, but I'd suggest that you put a couple of markers around the variables you print, like in:

      if ($success){ print "Changed [$oldname] to [$newname]\n" } else { print "Failed to rename [$oldname] to [$newname] $!\n"; exit };
      just to be sure that there aren't stray trailing spaces wandering in your string. You could also go deeeper trying to see some hex dump of the strings, for example with:
      sub hex_dump { unpack "H*", $_[0]; } print "\$oldname = [", hex_dump($oldname), "]\t"; print "\$newname = [", hex_dump($newname), "]\n";
      If you don't want to check these hex dumps, you can ensure that your filename only contains letters, digits and dots:
      $newname =~ tr/a-zA-Z0-9\.//cd;

      Back to your particular case: are you sure that the destination file -- i.e. 573427RSSt.csv -- does not exist or, if it exists, it is writeable?

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

      Don't fool yourself.
        Some useful tips thanks.

        I'll improve with the formatting, but I checked the vars with encapsulating '$var' for trailing spaces / other chars. Doing it with your code will improve on this, tho.

        The dir is local, so I've checked and am clear that the rename-to files don't exist.

        Thanks for your suggestions.