For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > October 2005 > Help needed with a script...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help needed with a script...
LC

2005-09-29, 9:56 pm

The 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);
}



Jim Gibson

2005-09-30, 6:56 pm

In article <RQ0%e.1493$cF6.1298@newssvr30.news.prodigy.com>, LC
<goalie22@hotmail.com> wrote:

> The 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.
>


Replace all of these lines below with:

$root = $ARGV[0];

That's it! Now you can enter any string you want 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

2005-10-01, 6:55 pm

that fixed it!

You da' man!

-LC

"Jim Gibson" <jgibson@mail.arc.nasa.gov> wrote in message
news:300920051009184977%jgibson@mail.arc.nasa.gov...
> In article <RQ0%e.1493$cF6.1298@newssvr30.news.prodigy.com>, LC
> <goalie22@hotmail.com> wrote:
>
>
> Replace all of these lines below with:
>
> $root = $ARGV[0];
>
> That's it! Now you can enter any string you want to.
>
>
>




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com