philnewbie
Programmer
Hi,
I am very new at Perl. I am trying to modify a script created by a co-worker that is no longer with the company. The script saves files based on their name. What I want to add is a print function to print the file. It is a pdf, and I can call the following command from a dos prompt to print the file.
acrord32.exe /t "file name" "printer share" or like this:
C:\Program Files\Adobe\Reader 9.0\Reader>acrord32.exe /t "c:\Documents and Settings\pgergen\Desktop\Verizon Bill.pdf" "\\redwdc001\rw_infosys"
I want to call this command from the script either before or after it saves the file. Below is my current script that I am trying to modify. Any help would be greatly appreciated. Thanks.
#!/Perl/bin/perl -w
use File::Copy;
$server_type = "windows";
##########################################################
# Your directories must have the trailing slash
##########################################################
$readdir = "G:/At_Work2/Work/AutoFile/PaperProof/";
$savedir = "G:/At_Work2/Work/";
$tempdir = "G:/At_Work2/Work/AutoFile/TempAutoFile/";
$badname = "G:/At_Work2/Work/AutoFile/TempAutoFile/BadFileNames/PaperProof/";
##########################################################
# Uncomment these 3 lines if being run on a Mac server
##########################################################
# $server_type = "mac";
# use Mac::Memory;
# use Mac::Resources;
##########################################################
$longdate = "";
# check for the temp folder and create if it doesn't exist
if(!-d $tempdir)
{
mkdir($tempdir);
}
if(!-d $badname)
{
mkdir($badname);
}
# get the current date (day only)
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
# use all the
$longdate = ($Minute."-".$Hour."-".$Day."-".$Month.$Year.$WeekDay.$DayOfYear.$IsDST);
if ($Day < 0)
{
$Day = "0".$Day;
}
$newbadname = ($Second);
$tdtempdir = ($tempdir.$Day);
if(!-d $tdtempdir)
{
mkdir($tdtempdir);
}
# if $readdir is a folder (directory)
if(-d $readdir)
{
# try to list folder, on error display message "Cannot open..." and quit
opendir(RDIR, $readdir) || die "Cannot opendir $readdir: $!";
# sort the list
foreach $name (sort readdir(RDIR))
{
# if the name isn't: ".", "..", and isn't locked and isn't a plain text file
if($name ne "." && $name ne ".." && !($name =~ m/\.loc$/) && !($name =~ m/^\./) && -f $readdir.$name)
{
print $name."\n";
if(-f $readdir.$name.".loc")
{
print " Locked\n";
}
else
{
# set LOCF to the file and open it with read/write access
open(LOCF, ">".$readdir.$name.".loc");
close(LOCF);
#print " Not Locked\n";
# set $s1 to the file size
$s1 = (-s $readdir.$name);
# sleep(1);
#set $s2 to the file size
$s2 = (-s $readdir.$name);
# if the file sizes are equal (ergo file isn't still saving/in use)
if($s1 == $s2)
{
########################################
# The Buck Starts Here
########################################
#*********************************************************
$tname = $name;
# delete alpha characters from the file name, commented out for red wing
# $tname =~ s/[a-zA-Z]//g;
# globally replace underscores, plus symbols, slashes, spaces, tabs and ñ with a dash
$tname =~ s/[_\+\/\s\t\.ñ\\:\*\^\+\?<>\"\|]/-/g;
# create an array called @tn2 of the text items of $tname?
@tn2 = split(/-/, $tname);
# set $tname to the first text item (ostensibly the SO number) of $tn2
$tname = $tn2[0];
#############################
# Z:\art_search\new\1150000-1199999\1156000-1156999\1156700-1156799\1156779-mi
# create the subfolder names from the file name
$td0 = $tname;
$td1 = $tname;
$td2 = $tname;
# $td3 = $tname; # uncomment this line if breaking folders down to thousands
$td4 = $tname;
$td0 =~ s/[0-9]{5}$//g;
$td1 =~ s/[0-9]{4}$//g;
$td1a = $td1;
$td1a =~ s/[a-zA-Z]//g;
if ($td0 !~ m/[a-zA-Z]/)
{
if(-e $badname.$name)
{copy($readdir.$name, $badname.$newbadname."-".$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
$newbadname = ++$newbadname;
print $name." moved to ".$badname."\n";
}
elsif (!-e $badname.$name)
{
copy($readdir.$name, $badname.$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
print $name." moved to ".$badname."\n";
}
}
$td4 =~ s/$td0{1}//g;
if ($td4 !~ m/[0-9]/)
{
if(-e $badname.$name)
{copy($readdir.$name, $badname.$newbadname."-".$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
$newbadname = ++$newbadname;
print $name." moved to ".$badname."\n";
}
elsif (!-e $badname.$name)
{
copy($readdir.$name, $badname.$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
print $name." moved to ".$badname."\n";
}
}
else
{
if(-d $savedir.$td0."00000-".$td0."99999")
{
$savedir = ($savedir.$td0."00000-".$td0."99999");
$savedir = $savedir."/";
}
$ndir = $td0.$td1a."0000";
$ndir = $ndir ."-". ($td0.$td1a."9999");
if(!-d $savedir.$ndir)
{
mkdir($savedir.$ndir);
}
#############################################################
$td2 =~ s/[0-9]{3}$//g;
$td2 = $td2 . "000-".$td2."999";
$ndir = $ndir . "/" . $td2;
if(!-d $savedir.$ndir)
{
mkdir($savedir.$ndir);
}
#####################################
# 1000s subfolder, red wing stops at 10k
# $td3 =~ s/[0-9]{2}$//g;
# $td3 = $td3 . "00-".$td3."99";
# $ndir = $ndir . "/" . $td3;
# if(!-d $savedir.$ndir)
# {
# mkdir($savedir.$ndir);
# }
#####################################
if(!-d $savedir.$ndir."/".$tname)
{
mkdir($savedir.$ndir."/".$tname);
}
if (!-d $savedir.$ndir."/".$tname."/"."Correspondence")
{
mkdir ($savedir.$ndir."/".$tname."/"."Correspondence");
}
#*********************************************************
$nname = $name;
$nname =~ s/[".pdf"]/_PP./o;
print "new name ".$nname."\n";
$nfile = $ndir."/".$tname."/"."Correspondence"."/".$nname;
if($server_type eq "windows")
{
if(-e $savedir.$nfile)
{
copy($savedir.$nfile, $tdtempdir."/".$nname);
}
copy($readdir.$name, $savedir.$nfile);
}
elsif ($server_type eq "mac")
{
if(-e $savedir.$nfile)
{
system 'ditto', '-V', '-rsrcFork', $savedir.$nfile, $tdtempdir."/".$name;
}
system 'ditto', '-V', '-rsrcFork', $readdir.$name, $savedir.$nfile;
}
if( (-s $readdir.$name) == (-s $savedir.$nfile) )
{
print " Copied Correctly\n";
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
}
else
{
print " File did not copy correctly\n";
print " Orig: ".$readdir.$name."\n";
print " Newf: ".$savedir.$nfile."\n";
}
########################################
# The Buck Stops Here
########################################
}
}
}
}
}
}
else
{
print "Directory not good ".$readdir."\n";
}
#########################################################
print "DONE";
exit;
I am very new at Perl. I am trying to modify a script created by a co-worker that is no longer with the company. The script saves files based on their name. What I want to add is a print function to print the file. It is a pdf, and I can call the following command from a dos prompt to print the file.
acrord32.exe /t "file name" "printer share" or like this:
C:\Program Files\Adobe\Reader 9.0\Reader>acrord32.exe /t "c:\Documents and Settings\pgergen\Desktop\Verizon Bill.pdf" "\\redwdc001\rw_infosys"
I want to call this command from the script either before or after it saves the file. Below is my current script that I am trying to modify. Any help would be greatly appreciated. Thanks.
#!/Perl/bin/perl -w
use File::Copy;
$server_type = "windows";
##########################################################
# Your directories must have the trailing slash
##########################################################
$readdir = "G:/At_Work2/Work/AutoFile/PaperProof/";
$savedir = "G:/At_Work2/Work/";
$tempdir = "G:/At_Work2/Work/AutoFile/TempAutoFile/";
$badname = "G:/At_Work2/Work/AutoFile/TempAutoFile/BadFileNames/PaperProof/";
##########################################################
# Uncomment these 3 lines if being run on a Mac server
##########################################################
# $server_type = "mac";
# use Mac::Memory;
# use Mac::Resources;
##########################################################
$longdate = "";
# check for the temp folder and create if it doesn't exist
if(!-d $tempdir)
{
mkdir($tempdir);
}
if(!-d $badname)
{
mkdir($badname);
}
# get the current date (day only)
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time);
# use all the
$longdate = ($Minute."-".$Hour."-".$Day."-".$Month.$Year.$WeekDay.$DayOfYear.$IsDST);
if ($Day < 0)
{
$Day = "0".$Day;
}
$newbadname = ($Second);
$tdtempdir = ($tempdir.$Day);
if(!-d $tdtempdir)
{
mkdir($tdtempdir);
}
# if $readdir is a folder (directory)
if(-d $readdir)
{
# try to list folder, on error display message "Cannot open..." and quit
opendir(RDIR, $readdir) || die "Cannot opendir $readdir: $!";
# sort the list
foreach $name (sort readdir(RDIR))
{
# if the name isn't: ".", "..", and isn't locked and isn't a plain text file
if($name ne "." && $name ne ".." && !($name =~ m/\.loc$/) && !($name =~ m/^\./) && -f $readdir.$name)
{
print $name."\n";
if(-f $readdir.$name.".loc")
{
print " Locked\n";
}
else
{
# set LOCF to the file and open it with read/write access
open(LOCF, ">".$readdir.$name.".loc");
close(LOCF);
#print " Not Locked\n";
# set $s1 to the file size
$s1 = (-s $readdir.$name);
# sleep(1);
#set $s2 to the file size
$s2 = (-s $readdir.$name);
# if the file sizes are equal (ergo file isn't still saving/in use)
if($s1 == $s2)
{
########################################
# The Buck Starts Here
########################################
#*********************************************************
$tname = $name;
# delete alpha characters from the file name, commented out for red wing
# $tname =~ s/[a-zA-Z]//g;
# globally replace underscores, plus symbols, slashes, spaces, tabs and ñ with a dash
$tname =~ s/[_\+\/\s\t\.ñ\\:\*\^\+\?<>\"\|]/-/g;
# create an array called @tn2 of the text items of $tname?
@tn2 = split(/-/, $tname);
# set $tname to the first text item (ostensibly the SO number) of $tn2
$tname = $tn2[0];
#############################
# Z:\art_search\new\1150000-1199999\1156000-1156999\1156700-1156799\1156779-mi
# create the subfolder names from the file name
$td0 = $tname;
$td1 = $tname;
$td2 = $tname;
# $td3 = $tname; # uncomment this line if breaking folders down to thousands
$td4 = $tname;
$td0 =~ s/[0-9]{5}$//g;
$td1 =~ s/[0-9]{4}$//g;
$td1a = $td1;
$td1a =~ s/[a-zA-Z]//g;
if ($td0 !~ m/[a-zA-Z]/)
{
if(-e $badname.$name)
{copy($readdir.$name, $badname.$newbadname."-".$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
$newbadname = ++$newbadname;
print $name." moved to ".$badname."\n";
}
elsif (!-e $badname.$name)
{
copy($readdir.$name, $badname.$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
print $name." moved to ".$badname."\n";
}
}
$td4 =~ s/$td0{1}//g;
if ($td4 !~ m/[0-9]/)
{
if(-e $badname.$name)
{copy($readdir.$name, $badname.$newbadname."-".$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
$newbadname = ++$newbadname;
print $name." moved to ".$badname."\n";
}
elsif (!-e $badname.$name)
{
copy($readdir.$name, $badname.$name);
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
print $name." moved to ".$badname."\n";
}
}
else
{
if(-d $savedir.$td0."00000-".$td0."99999")
{
$savedir = ($savedir.$td0."00000-".$td0."99999");
$savedir = $savedir."/";
}
$ndir = $td0.$td1a."0000";
$ndir = $ndir ."-". ($td0.$td1a."9999");
if(!-d $savedir.$ndir)
{
mkdir($savedir.$ndir);
}
#############################################################
$td2 =~ s/[0-9]{3}$//g;
$td2 = $td2 . "000-".$td2."999";
$ndir = $ndir . "/" . $td2;
if(!-d $savedir.$ndir)
{
mkdir($savedir.$ndir);
}
#####################################
# 1000s subfolder, red wing stops at 10k
# $td3 =~ s/[0-9]{2}$//g;
# $td3 = $td3 . "00-".$td3."99";
# $ndir = $ndir . "/" . $td3;
# if(!-d $savedir.$ndir)
# {
# mkdir($savedir.$ndir);
# }
#####################################
if(!-d $savedir.$ndir."/".$tname)
{
mkdir($savedir.$ndir."/".$tname);
}
if (!-d $savedir.$ndir."/".$tname."/"."Correspondence")
{
mkdir ($savedir.$ndir."/".$tname."/"."Correspondence");
}
#*********************************************************
$nname = $name;
$nname =~ s/[".pdf"]/_PP./o;
print "new name ".$nname."\n";
$nfile = $ndir."/".$tname."/"."Correspondence"."/".$nname;
if($server_type eq "windows")
{
if(-e $savedir.$nfile)
{
copy($savedir.$nfile, $tdtempdir."/".$nname);
}
copy($readdir.$name, $savedir.$nfile);
}
elsif ($server_type eq "mac")
{
if(-e $savedir.$nfile)
{
system 'ditto', '-V', '-rsrcFork', $savedir.$nfile, $tdtempdir."/".$name;
}
system 'ditto', '-V', '-rsrcFork', $readdir.$name, $savedir.$nfile;
}
if( (-s $readdir.$name) == (-s $savedir.$nfile) )
{
print " Copied Correctly\n";
unlink($readdir.$name); # deletes the original file
unlink($readdir.$name.".loc"); # deletes the lock file
}
else
{
print " File did not copy correctly\n";
print " Orig: ".$readdir.$name."\n";
print " Newf: ".$savedir.$nfile."\n";
}
########################################
# The Buck Stops Here
########################################
}
}
}
}
}
}
else
{
print "Directory not good ".$readdir."\n";
}
#########################################################
print "DONE";
exit;