Help needed with a script...

Help needed with a script...

am 30.09.2005 03:49:37 von LC

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

Re: Help needed with a script... [poor subject line]

am 30.09.2005 12:18:52 von Joe Smith

LC wrote:

> # Parse the parameter to grab the first letter if it exists and convert
> # to uppercase
>
> ($root) = $parameter =~ m<^([a-z|A-Z])$>;

But a vertical bar is an alphabetic character. Get rid of it.
m<^([a-zA-Z])$>

> if ( defined $root ) {
> chomp($root);

Why are you using chomp()? The previous regex guarentees the
there won't be anything there to chomp.

> $root =~ tr/[a-z]/[A-Z]/;

That's not proper for tr///. Better would be
tr/a-z/A-Z/ or tr[a-z][A-Z] or tr{a-z}{A-Z} or tr(a-z)(A-Z)

`perldoc perlop`

Note that "tr" does not do regular expression character classes
such as "\d" or "[:lower:]". The operator is not equiva-
lent to the tr(1) utility. If you want to map strings between
lower/upper cases, see "lc" in perlfunc and "uc" in perlfunc,
and in general consider using the "s" operator if you need reg-
ular expressions.

Lookup lc(), uc(), ucfirst(), and for s///: \l \L \u \U \Q \E.

Re: Help needed with a script...

am 30.09.2005 19:09:18 von Jim Gibson

In article , LC
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!

Re: Help needed with a script...

am 01.10.2005 15:36:12 von LC

that fixed it!

You da' man!

-LC

"Jim Gibson" wrote in message
news:300920051009184977%jgibson@mail.arc.nasa.gov...
> In article , LC
> 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!
>