I am trying to rename the code for a set of images in directories in the database with a new one using Perl.
The code is of the format XXXX_XXXX_V1.jpg
. All I need to do is replace the XXXX_XXXX
with a new code entered by the designers, the images are located lets say at /usr/site/html/images/p
.
What开发者_Python百科 should be my approach to work on this situation ? Should I go to the directory and then search for the existing code in the directories and then perform a swap?
once the codes are swapped I need to say "successfully swapped pictures", and then show the http://servername/images/p/n/ct/CODE_V1.jpg
view for all servers so that the person can view the pictures to confirm that the operation was successful.
Any inputs are welcomed, I am looking into this and I will paste some of the code soon.
Thanks
Perl, is well suited to the task, as I assume many other languages are as well. However, if you already know Perl, it's going to be a pretty simple job for you.
Look at File::Find::Rule as an easy means of finding and working with a lot of files in a directory tree. You can set up rules that will include only files that match your specific requirements, while bypassing all others. You may also set up a handler for those files that do meet your rules.
Within your handler, you can do the renaming that you deem necessary.
If the files are all within one directory, and there doesn't need to be a list of criteria (or rules) that determine which files qualify, you may just use opendir and readdir, along with file checks such as -f.
Once you post some code showing where you're stuck, and specific questions about the code, we can actually be of more specific help. Your question, as asked, is too broad to get a specific answer.
An update: I did some work looking at File::Find last night. It traversed about 100,000 files on my Windows computer in literally thousands of directories, and took about two minutes. sub wanted
just did a simple file and directory count, so that I could test pretty much only the efficiency of File::Find. On the first callback to wanted the script output a "Started". The reason I did this was because historically I found that it took a long time for File::Find to actually start its callback loop on Windows with large filesystems.
I then ran the same script on a similar size filestructure on my Linux computer.
I found that under Strawberry Perl 5.12.2 and $File::Find::VERSION of 1.15, the bug of which I previously spoke in comments seems to have been cleared up. The wanted callbacks began almost immediately, whereas when I used to play with it with 5.8.x the callbacks would take a long time before starting up. This is on windows of course. On Linux, I never had a problem.
By way of comparison, however, the File::Find test script working on similarly complex directory trees on my Windows system took about ten times longer to traverse 100,000 files as on my Linux system, on "first run". Subsequent runs the difference was even more dramatic as Linux was presumably doing some filesystem caching.
At any rate, I suggest that if you do use File::Find, or File::Find::Rule, you make sure you're running a recent Perl (5.12.2 or 5.14.1) along with the File::Find version that comes with that more recent Perl.
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Data::Dumper;
use List::MoreUtils qw/ uniq /;
my $localdir = 'images/p/';
my @filefound;
find(
sub {push @filefound, $File::Find::name if /.jpg$/ },
$localdir);
foreach (@filefound){
my @split = split('_',@filefound);
my @split1 = $split[0];
foreach (@split1) { print "$_ \n";}
}
精彩评论