Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Renaming Every File in a directory

by Anonymous Monk
on Apr 28, 2015 at 03:38 UTC ( [id://1124950]=perlquestion: print w/replies, xml ) Need Help??

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

I have this code to delete every file in a directory and skip directories... but I'm not remembering how to use the sub to rename files...

#!/usr/bin/perl use CGI qw(:standard); use strict; use File::Find; use vars qw($_amountOfFiles); my $_excludeList = 1; my $_expcludeMain = 1; my $dir = "/home/path/public_html/files"; my %_excludeListing = ( 'cgi-bin' => "cgi-bin", ); #my $dir = shift || die "usage $0 <dir>\n"; print "Content-type: text/plain\n\n"; $_amountOfFiles = 0; finddepth(\&do_this, $dir); print "Renamed: $_amountOfFiles files\nDone\n"; #rmdir $dir; sub do_this { # ignore . and .. return if /^\.\.?$/; if (-d) { if($_excludeList && exists($_excludeListing{$_})) { print "Skipping dir: $File::Find::name\n"; return; } if($_expcludeMain && $_ eq "$dir") { print "Skipping dir: $File::Find::name\n"; return; } # print "Removing dir: $File::Find::name\n"; # rmdir; } else { print "."; $_amountOfFiles++; unlink; } }

Where unlink is, what do I put to rename the file, IF it ends in .jpg to a Random string.jpg?

Instead of unlink?

I have not been writing Perl programming for a few years, and I feel like I'm getting dementia... I am at a total loss on it.

Can you point me in the right direction?
Please.

Rich

Replies are listed 'Best First'.
Re: Renaming Every File in a directory
by Athanasius (Archbishop) on Apr 28, 2015 at 03:47 UTC

    Hello Rich,

    Can you point me in the right direction?
    • To rename a file, use the built-in function rename.
    • To select only files ending in .jpg, use if (/\.jpg$/) { ... }.
    • To generate a random string, maybe try String::Random (a module I haven’t used see below).

    P.S. Always code with use warnings as well as use strict.

    Update: Example use of String::Random:

    use strict; use warnings; use String::Random; my $oldfile = 'picture.jpg'; my $random = String::Random->new; if ($oldfile =~ /\.jpg$/) { my $oldname = $oldfile =~ s/\.jpg$//r; my $newfile = $random->randpattern('.' x length $oldname) . '.jpg' +; print "$oldfile will be renamed $newfile\n"; }

    Example output:

    14:18 >perl 1230_SoPW.pl picture.jpg will be renamed O^aK&!R.jpg 14:19 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Ok... so in the sub routine... do_this
      How do I get the file name?

      unlink is just said as unlink not unlink(filename)...

      so the rename one you showed me, has this:
      rename OLDNAME,NEWNAME

      How do I get the Oldname in the routine??

      Thank you,
      Rich
Re: Renaming Every File in a directory
by Don Coyote (Hermit) on Apr 28, 2015 at 06:44 UTC

    Make backups of your server directories.

    Re-read the documentation for the File::Find module.

    Remember to use fullpaths, with/or the $File::Find::dir var, examine the chdir no_chdir flags. Do some extensive testing pre-production run, to ensure your routine does what you want it to do / think it does.

    my $count = 1; sub do_this{ ... next unless $_ =~ m/(.*)(\.jpg)$/; # untaint = $1,$2 # rename(OLDFILE, NEWFILE); rename($File::Find::dir.$1.'.jpg', $File::Find::dir.$count++.'.jpg'); }

    Also IIRC you will want to next rather than return inside the &do_this routine.


    Scavenging Leftovers since the Naughties
Re: Renaming Every File in a directory
by Marshall (Canon) on Apr 28, 2015 at 05:09 UTC
    I am not sure what you are trying to do.
    It would be helpful if you could explain in plain text a description of what you are trying to accomplish and what that has to do with cgi-bin. I am not sure based upon this code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1124950]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found