Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Multiple File Rename

by perl_n00b (Acolyte)
on Jul 10, 2011 at 20:08 UTC ( #913622=perlquestion: print w/replies, xml ) Need Help??
perl_n00b has asked for the wisdom of the Perl Monks concerning the following question:

So it's been quite a while since I've done anything in Perl and I was never good to begin with, so I am blanking on how to do this:

All I want is to rename photos with the caption as the file name. I can search for /<rdf:li xml:lang=\"x-default\">(.+?)<\/rdf:li>/ and $1 will be the caption. I could use the rename function to rename a single file 's/$filename/$1/'...if I could remember how to read-in the file name, but I don't, and I also forgot how to read-in a whole directory.

If someone could kindly post the tiny script necessary to do this I would be much obliged. And if you PM me your PayPal email addy I will send you $5 for your trouble, for the first one to do so obviously.

Thanks in advance!

Replies are listed 'Best First'.
Re: Multiple File Rename
by GrandFather (Sage) on Jul 10, 2011 at 20:50 UTC

    Use perldoc -f xxx to see the docs for function xxx (perldoc -f rename for example). opendir, readdir and closedir are the directory scanning tools.

    Update: s/scandir/readdir/ - thanks kcott.

    True laziness is hard work

      Should that be readdir instead of scandir?

      -- Ken

Re: Multiple File Rename
by Anonymous Monk on Jul 10, 2011 at 20:59 UTC
Re: Multiple File Rename
by i5513 (Pilgrim) on Jul 10, 2011 at 20:42 UTC
    Tell Larry Wall thank you ! You can use his rename program:
    rename 'my $a=$_ ; open XML, "$a.xml"; while (my $l=<XML>) { if ($l =~ /<rdf:li xml:lang=\"x-default\">(.+?)<\/rdf:li>/) { $a=$1} } ; close XML; $_=$a' *
    This code suppose about if you have a file named "xx.png", then there are another file named "xx.png.xml" Regards,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://913622]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-12-18 09:43 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (475 votes). Check out past polls.