command-line commands within a Perl script

Hi,
I have a directory with several subdirectories, each full of several dozen
Word files. For each subdirectory,  I need to run the checksum app against
all of that directory's files and output a file into that directory with the
checksum results. How can I do this? I'm very unfamilar with running
command-line commands from within an Perl script. I know to change
directories, you can do something like this:
$changeDir = "cd ".$startingDir;
system($changeDir);
but what about retrieving the list of subdirectories from the starting
directory? How can I do this? 
Thanks for your help.

-Jose
0
Jose
5/23/2002 4:03:26 PM
📁 perl.beginners
📃 29388 articles.
⭐ 4 followers.

💬 10 Replies
👁️‍🗨️ 853 Views


 
If you do that, you will be invoking the shell, changing the current
directory for the shell, and then closing the shell.  What you want is to
use the chdir() Perl function to change the current directory of your Perl
script.  
perldoc -f chdir
-----Original Message-----
From: Torres, Jose
To: 'beginners@perl.org'
Sent: 5/23/02 9:03 AM
Subject: command-line commands within a Perl script
Hi,
I have a directory with several subdirectories, each full of several
dozen
Word files. For each subdirectory,  I need to run the checksum app
against
all of that directory's files and output a file into that directory with
the
checksum results. How can I do this? I'm very unfamilar with running
command-line commands from within an Perl script. I know to change
directories, you can do something like this:
$changeDir = "cd ".$startingDir;
system($changeDir);
but what about retrieving the list of subdirectories from the starting
directory? How can I do this? 
Thanks for your help.

-Jose

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
0
tjohnson
5/23/2002 4:27:01 PM
Hi,
A possible way :
#---------------
use File::Find;
use File::stat;
my $directory = "/user/IPlib/IPlib/";
find(\&search, $directory);
}
sub search() {
 my $file = $File::Find::name || shift;
  if ( -d $file ) { push @dirs,$file; }
  else { push @files,$file; }
 print @files;
 print @dirs;
  
}
Regs David
> Hi,
> 
> I have a directory with several subdirectories, each full of several dozen
> Word files. For each subdirectory,  I need to run the checksum app against
> all of that directory's files and output a file into that directory with the
> checksum results. How can I do this? I'm very unfamilar with running
> command-line commands from within an Perl script. I know to change
> directories, you can do something like this:
> 
> $changeDir = "cd ".$startingDir;
> system($changeDir);
> 
> but what about retrieving the list of subdirectories from the starting
> directory? How can I do this? 
> Thanks for your help.
> 
> 
> -Jose
> 
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 
0
vdgeerd
5/23/2002 4:38:35 PM
Thanks everyone for your help. It is much appreciated.

-----Original Message-----
From: David vd Geer Inhuur tbv IPlib
[mailto:vdgeerd@iclab.ce.philips.com]
Sent: Thursday, May 23, 2002 12:39 PM
To: beginners@perl.org; Torres, Jose
Subject: Re: command-line commands within a Perl script

Hi,
A possible way :
#---------------
use File::Find;
use File::stat;
my $directory = "/user/IPlib/IPlib/";
find(\&search, $directory);
}
sub search() {
 my $file = $File::Find::name || shift;
  if ( -d $file ) { push @dirs,$file; }
  else { push @files,$file; }
 print @files;
 print @dirs;
  
}
Regs David
> Hi,
> 
> I have a directory with several subdirectories, each full of several dozen
> Word files. For each subdirectory,  I need to run the checksum app against
> all of that directory's files and output a file into that directory with
the
> checksum results. How can I do this? I'm very unfamilar with running
> command-line commands from within an Perl script. I know to change
> directories, you can do something like this:
> 
> $changeDir = "cd ".$startingDir;
> system($changeDir);
> 
> but what about retrieving the list of subdirectories from the starting
> directory? How can I do this? 
> Thanks for your help.
> 
> 
> -Jose
> 
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 
0
Jose
5/23/2002 5:15:31 PM
Whoa!! Perl was not meant to make you work so hard!!=20
For changing directory...used function chdir (perldoc -f chdir)
For getting teh directories...well
opendir (DIR,"$myCurDir");
foreach (readir(DIR)) {
  if (-d $_) {
            Change to the directory
   }
}
Probably you an use recursion to achieve your ends...

-----Original Message-----
From: Torres, Jose [mailto:Jose.Torres@gs.com]
Sent: Thursday, May 23, 2002 12:03 PM
To: 'beginners@perl.org'
Subject: command-line commands within a Perl script

Hi,
I have a directory with several subdirectories, each full of several =
dozen
Word files. For each subdirectory,  I need to run the checksum app =
against
all of that directory's files and output a file into that directory with =
the
checksum results. How can I do this? I'm very unfamilar with running
command-line commands from within an Perl script. I know to change
directories, you can do something like this:
$changeDir =3D "cd ".$startingDir;
system($changeDir);
but what about retrieving the list of subdirectories from the starting
directory? How can I do this?=20
Thanks for your help.

-Jose

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
0
sksingh
5/23/2002 5:21:25 PM
what Perl function can I used to invoke something usually done at the
command line?
I want to execute:
sum * > SNP/020405/foo.txt
this will call checksum on everything and output to foo.txt in /SNP/020405.
Problem is, the * will sum all files in the current directory, not those in
SNP/020405. So then I'd instead use:
sum SNP/020405/* > SNP/020405/foo.txt
but the problem with this is the foo.txt will look like:
5134 1 SNP/020405/17D78401.001
instead of the much "cleaner":
5134 1 17D78401.001
I basically need to change directories to 020405 (chdir will do this ok),
and then call sum * and output the results to a file which should also be
located in 020405 (How do I do this part?). Thanks in advance for all help.

-Jose
0
Jose
5/23/2002 8:45:36 PM
Jose Torres wrote:
> 
> Hi,
Hello,
> I have a directory with several subdirectories, each full of several dozen
> Word files. For each subdirectory,  I need to run the checksum app against
> all of that directory's files and output a file into that directory with the
> checksum results. How can I do this? I'm very unfamilar with running
> command-line commands from within an Perl script. I know to change
> directories, you can do something like this:
> 
> $changeDir = "cd ".$startingDir;
> system($changeDir);
> 
> but what about retrieving the list of subdirectories from the starting
> directory? How can I do this?
> Thanks for your help.

This should give you some ideas on how to do it
use warnings;
use strict;
use File::Find;
my %files;
find( sub {
    # put all .doc files in the hash
    push @{$files{$File::Find::dir}}, $File::Find::name if /\.doc$/i
    # get the directory name from the command line
    }, @ARGV );
for my $dir ( keys %files ) {
    open CHK, "> $dir/checksum" or die "Cannot open $dir/checksum: $!";
    for my $file ( @$dir ) {
        # this is assuming 'checksum' prints to standard output
        chomp( my $checksum = qx[checksum $file] );
        print CHK "$file $checksum\n";
        }
    }
__END__

John
-- 
use Perl;
program
fulfillment
0
krahnj
5/23/2002 9:06:11 PM
"John W. Krahn" wrote:
> 
> This should give you some ideas on how to do it
> 
> use warnings;
> use strict;
> use File::Find;
> 
> my %files;
> find( sub {
>     # put all .doc files in the hash
>     push @{$files{$File::Find::dir}}, $File::Find::name if /\.doc$/i
>     # get the directory name from the command line
>     }, @ARGV );
> 
> for my $dir ( keys %files ) {
>     open CHK, "> $dir/checksum" or die "Cannot open $dir/checksum: $!";
>     for my $file ( @$dir ) {
That last line _should_ be:  :-)
     for my $file ( @{$files{$dir}} ) {

>         # this is assuming 'checksum' prints to standard output
>         chomp( my $checksum = qx[checksum $file] );
>         print CHK "$file $checksum\n";
>         }
>     }
> 
> __END__

John
-- 
use Perl;
program
fulfillment
0
krahnj
5/23/2002 10:01:52 PM
On Thursday, May 23, 2002, at 01:45 , Torres, Jose wrote:
> what Perl function can I used to invoke something usually done at the
> command line?
> I want to execute:
>
> sum * > SNP/020405/foo.txt
>
> this will call checksum on everything and output to foo.txt in 
> /SNP/020405.
> Problem is, the * will sum all files in the current directory, not those 
> in
> SNP/020405.
you might want to think in terms of
        Getopt::Std
to take command line arguments like
        what dir to start in,
        where to put the output file, probably best
                not to put it in the directory path you are walking...
then consider using the
        File::Finder
and write a fun 'call back' function that would do
the 'checksum' on files, but not on directories,
caching away the results into say a hash that
would trap things in a way that would be simple
for you to walk through later on....
ciao
drieux
---
0
drieux
5/23/2002 10:05:14 PM
Here's the code I currently have to do this:
$startDir = $ARGV[0];
## Main Program ##
$dir = ();
opendir (DIR, $startDir);
foreach $dir (readdir(DIR)) {
        if(($dir ne ".") && ($dir ne "..")){
                CreateChecksum($dir);
        }
}
closedir DIR;
sub CreateChecksum {
        my($dir) = @_;
        #change directories to one in which I want to run checksum and save
the resuls there
        chdir $dir;
        ## WHAT DO I DO HERE? I want to execute something like "sum * >
foobar.txt" ##
        #now change back to the starting directory
        chdir $startDir;
}
I tried something like: system("sum * > foobar.txt") but that's not working
because it looks like that call is being made in the directory the script is
running, instead of the directory I changed to with the "chdir $dir" line of
code. Any ideas? Is there a method that will execute code in the location
specified by a previous call to chdir (as is the case here)? Thanks for your
help.
>  -----Original Message-----
> From:      Torres, Jose  
> Sent:      Thursday, May 23, 2002 12:03 PM
> To:        'beginners@perl.org'
> Subject:   command-line commands within a Perl script
> 
> Hi,
> 
> I have a directory with several subdirectories, each full of several dozen
> Word files. For each subdirectory,  I need to run the checksum app against
> all of that directory's files and output a file into that directory with
> the checksum results. How can I do this? I'm very unfamilar with running
> command-line commands from within an Perl script. I know to change
> directories, you can do something like this:
> 
> $changeDir = "cd ".$startingDir;
> system($changeDir);
> 
> but what about retrieving the list of subdirectories from the starting
> directory? How can I do this? 
> Thanks for your help.
> 
> 
> -Jose
> 
0
Jose
5/24/2002 2:42:27 PM
On Friday, May 24, 2002, at 07:42 , Torres, Jose wrote:
> Here's the code I currently have to do this:
>
> $startDir = $ARGV[0];
>
> ## Main Program ##
> $dir = ();
> opendir (DIR, $startDir);
> foreach $dir (readdir(DIR)) {
>    if(($dir ne ".") && ($dir ne "..")){
>            CreateChecksum($dir);
>    }
> }
> closedir DIR;
[..]
you really would want to become aware of the basic problems
of tree traversals - since you have a variety of issues here...
like understanding that readdir is going to return a list
of 'names' out of the 'dirblock' - some of which will be
sub-directories in that dir, some of which are just flat files
you may want to look at something like:
        http://www.wetware.com/drieux/pbl/Sys/Admin/dirWalkerForCheckSum.txt
ciao
drieux
If you want to send me the check, check out my resume
it has the mailing address where I take donations to
put my kids through college... as well as try to work
on the fact that now that my daughter has opted out
of that TomBoy thing her fashion choices are going to
be limited if she really wants to follow in her mother's
trade - since there is like no way to get threat level III
body armour in under a strapless evening gown.... which I
guess the up side is that we can do scully's wardrobe
on a whole lot less money.....
Why couldn't she have gone into, I don't know, "a grunge thing"?
0
drieux
5/25/2002 9:07:27 PM
Reply: