Help needed with a script...
am 30.09.2005 03:49:37 von LCThe point of this script is to create a directory structure to logically
store work orders, and help me with a folder restructure that is desperately
needed.
The original use was to put a letter "A-Z" before the order #. This will
not work for my processes, as our ERP system does not use letters as the
first chr. in a work order.
a typical work order # would be 900123 or 1501234
So instead of placing a letter, I'd like to place a 3 or 4 character
argument at the beginning of the perl script that creates my folders that
start with 900 or 1501 versus A, or B.
I think this is the part that I would change, but I just don't know what to
change it to.
# Grab the command line argument if it exists -- else usage statement
if (@ARGV == 1) {
$parameter = $ARGV[0];
} else {
&usage;
}
# Parse the parameter to grab the first letter if it exists and convert
# to uppercase
($root) = $parameter =~ m<^([a-z|A-Z])$>;
if ( defined $root ) {
chomp($root);
$root =~ tr/[a-z]/[A-Z]/;
} else {
die "\nThe parameter \"$parameter\" is not a single letter!!\n\n";
}
Thank you for any help in advance!
-LC
---------- Start Code -----------------
#!/usr/bin/perl
# genfolders - automatically generate directory structure on a mounted
# network disk
#
#
# 2004 Sep 09 Original version
# 2004 Sep 14 Added mounting/dismounting of the network disk
# 2004 Sep 15 Corrected an added carriage return on the
# created directories
# Grab the command line argument if it exists -- else usage statement
if (@ARGV == 1) {
$parameter = $ARGV[0];
} else {
&usage;
}
# Parse the parameter to grab the first letter if it exists and convert
# to uppercase
($root) = $parameter =~ m<^([a-z|A-Z])$>;
if ( defined $root ) {
chomp($root);
$root =~ tr/[a-z]/[A-Z]/;
} else {
die "\nThe parameter \"$parameter\" is not a single letter!!\n\n";
}
# Define the prefix for the folder paths
$prefix = "/Volumes/Data2";
# Check to ensure $prefix is mounted -- if not mount and then dismount
# when done
if ( `df | grep $prefix` ) {
print("Volume is mounted.\n");
print("Volume will remain mounted after program completion.\n");
} else {
print("Volume is *not* mounted.\n");
print("Volume will be dismounted after program completion.\n");
print("Mounting Volume...\n");
mkdir "$prefix";
if (! -d "$prefix") {
die "Failed to create the mountpoint!\n";
}
`mount_afp 'afp://USERNAME:PASSWORD\@SERVER/Data2' $prefix`;
if (! `df | grep "$prefix"` ) {
die "Failed to mount Volume!\n";
}
$wasnt_mounted= 1;
}
# Iterate through loops creating folders with the right names
print("Creating folders...\n");
for ($i=0; $i < 2; $i++) {
$sub1 = $i * 5;
$sub2 = $sub1 + 4;
$section1 = "$root${sub1}0000-$root${sub2}9999";
for ($j=0; $j < 50; $j++) {
$sub3 = $sub1 * 10 + $j;
$sub3 = sprintf("%02d", $sub3);
$section2 = "$root${sub3}000-$root${sub3}999";
for ($k=0; $k < 10; $k++) {
$section3 = "$root$sub3${k}00-$root$sub3${k}99";
`mkdir -p \"$prefix/$section1/$section2/$section3\"`;
}
}
}
if ( defined $wasnt_mounted ) {
print("Dismounting Volume...\n");
`umount $prefix`;
}
print("Done.\n");
sub usage {
print("\nThis program accepts only a single letter as its"
.." argument.\n\n");
exit(1);
}