Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Renaming Multiple Files with Different Names

by graff (Chancellor)
on Nov 02, 2011 at 07:22 UTC ( #935315=note: print w/replies, xml ) Need Help??

in reply to Renaming Multiple Files with Different Names

I couldn't understand how your code relates to your description, except for this part:

if I change one part of the name (%03d), then I get errors about the file not being clobbered.

That would be happening because you are trying to open file names for output when the files already exist in your current working directory -- your posted code specifically dies when this is the case. You could just create a new directory and run the script there (with adjustments to the input file name).

For all your concerns about renaming files, I'm wondering why you don't show any use of the rename function. If you have hundreds of files to rename, you just need a list of the existing file names, and a corresponding list of the intended file names (or a method for deriving intended names from existing names, e.g. by adding and/or replacing something). Then just loop over the list:

for my $old ( @existing_names ) { my $new = $old . ".foobar"; rename $old, $new or warn "rename failed for $old -> $new\n"; }

Replies are listed 'Best First'.
Re^2: Renaming Multiple Files with Different Names
by Feral_Akodon (Initiate) on Nov 02, 2011 at 16:10 UTC

    I am just using the rename function in command line.

    rename 001.tabular 3972

    As I read your code, it is renaming the files by simply adding the extension of .foobar to each one. I need to completely rename them, so I need to designate $old as $ARGV[0] and $new as $ARGV1 from a text file but it needs to be the column and not the element of the array.

    Even when the file doesn't exist, if I change the "%03d" it still says won't clobber. If I can fix that code to rename the files as the value of column 1 that I am sorting on, I don't have to worry about the renaming code. But when I make changes to the code, I either get won't clobber or still get the exact same names as before (that have no meaning and need to be changed).

      This may achieve what you are trying to do. Your first column is "$key". If you want the files generated to use this, Just replace the corresponding line with:
      $file{$key}{sequence} = $key; # Old stuff was : sprintf '%03d +.tabular', $file{$key}{ID};

                  "XML is like violence: if it doesn't solve your problem, use more."

        Perfect it worked. Thanks so much NetWallah!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935315]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2018-04-20 06:02 GMT
Find Nodes?
    Voting Booth?