HI
I've been fiddling with perl for the last couple of days and am completely bit bamboozled by its intricacies - I'm finding this pretty hard as a perl newbie, and unfortunately my boss has me on a deadline.
3 folders exist:
C:\Converted PDFs, C:\Approved; C:\Archive
I'm looking for a script which does the following:
Some users generate PDFs from PPT's and drop them in a folder, C:\Converted PDFs - that bit is fine :)
1. I need to check that these files are in the right format - if, for example, they have a filename of
'Microsoft Excel - actual_filename.pdf'
'Microsoft Word - actual_filename.pdf'
'Microsoft Powerpoint - actual_filename.pdf'
where actual_filename can be random characters and underscores of no defined length, but probably not more than 25 chars.,
and '.pdf' is the suffix
for this part, strip off the 'microsoft excel -' etc.. so that I'm left with the 'actual_filename.pdf' component.
2. Check if 'actual_filename.pdf' already exists in C:\Approved.
If NO MATCH is found, ie C:\Converted PDFs\FILE1.PDF does not exist in C:\Approved, copy FILE1.PDF directly to C:\Approved.
If a filename match occurs, ie (C:\converted PDFs\FILE1.PDF = C:\Approved\FILE1.PDF)
then rename (the existing) C:\Approved\FILE1.PDF to include a time-date stamp so that it becomes
C:\Approved\File1_YYYY_MM_
DD__HH-MM-
SS.PDF
and move
C:\Approved\FILE1_YYYY_MM_
DD__HH-MM-
SS.PDF to C:\ARCHIVE\FILE1.PDF.YYYY_
MM_DD__HH-
MM-SS
then move the new FILE1.PDF to the C:\Approved folder.
(loop for each file found in C:\Converted PDFs)
---
that's it.
what I have so far probably isn't of much use, and is mainly gleaned from different postings and from the oreilly perl cookbook.
----
#!/usr/bin/perl -wall
use strict;
use Getopt::Std;
use File::Basename;
use File::Copy;
# static values - these can be parameterised for use elsewhere
############## GLOBAL VARIABLES ##############
my $src="C:\\Generated PDF" ;
my $dest="C:\\Approved";
my $arch="C:\\Archive" ;
##########################
##########
#####
my @firstDirNew;
my @secondDirNew;
my @unmatched;
my $unmatcheditem;
my $i = 0;
opendir(APPROVED, $dest);
my @firstDir = readdir APPROVED;
closedir APPROVED;
opendir(SOURCE, $src);
my @secondDir = readdir SOURCE;
closedir SOURCE;
#assign the directory lengths to a variable;
my $length1 = @firstDir;
my $length2 = @secondDir;
#call the sub;
compareDirs();
#print the results;
print "APPROVED Directory contains $length1 files.\n";
print "SOURCE Directory contains $length2 files.\n";
unless (@unmatched eq "") {
print "Items in the APPROVED Directory not in the SOURCE Directory:\n";
foreach $unmatcheditem (@unmatched) {
print $unmatcheditem . "\n";
}
}
sub compareDirs {
my $filename;
my %seen = ();
#build a lookup table
foreach $filename (@secondDir) { $seen{$filename} = 1 }
#find only elements in @firstDirNew not in @secondDirNew;
foreach $filename (@firstDir) {
unless ($seen{$filename}) {
#its not in %seen, so add to @unmatched
push(@unmatched, $filename);
}
}
}
-------------
# case where files do exist in the destination:
# datestamp each file
# move existing file from $dest to archive
# copy file from $src to $dest
#!/usr/bin/perl -w
use strict ;
use POSIX ;
use File::Copy ;
use File::Basename ;
use File::stat
chdir "$arch"
or die "Can't chdir to archive directory [ C:\\archive ] $!\n" ;
for my $file (<*.pdf>)
{ my ($name,$path,$suffix) = fileparse($file,"\.pdf") ;
my $info = stat($file);
# This time is the time when the file was last *MODIFIED*
# because you use "$info->mtime".
# When you want to have the date the file was last accessed,
# you have to use "$info->atime"
my $datestamp = strftime(".%Y-%m-%d_%H:%M:
%S", localtime($info->mtime));
copy $file,"W:\\archive\\$name$
datestamp$
suffix"
or warn "Cannot copy $file $!\n" ;
}
-----------
# Case where files do NOT exist in the destination - move the files from the array to the destination
#!/usr/bin/perl
use File::Copy;
my $movefile;
my @dirfile, @movelist;
# DO I HAVE A MOVELIST, OR IS THIS CREATED FROM SOME OTHER LOOP?
open (MOVELIST, path_to_movelist);
my $srcpath = $src;
my $movepath = $dest;
while () {
chomp $_;
push (@movelist, $_);
}
foreach $movefile(@movelist){
$movefile .= .pdf;
print $srcpath$movefile. . $movepath$movefile.\n;
copy($srcpath$movefile, $movepath$movefile);
}
ideally everything in one file is what I'm looking for, so I only need to poll one file.
that's it. any help you can give is greatly appreciated :)))
Start Free Trial