Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Reading files from directory

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Dec 16, 2003
135
foreach my $f (@files){
next if ($name eq ".");
next if ($name eq "..");

if (-d $f) {
if (($f eq "bin")) {
$loc = "$pwd\/bin";
#print "Entering bin and Folder = $f and Path = $loc\n";
&addFiletoHash($f,$loc);

}
if (($f eq "int")) {
$loc = "$pwd\/int";
#print "Entering int and Folder = $f and Path = $loc\n";
&addFiletoHash($f,$loc);
}
}
}

}

# Add files to the hash and also the location.
sub addFiletoHash($f,$loc){
my $folder = shift;
my $path = $loc;
print "Folder is $folder and location is $loc\n";
chdir($f) or die "Unable to enter $folder:$!`\n";
&readfile($folder,$path);
}

Adding this line causes grief chdir($f) or die "Unable to enter $path:$!`\n";

Could someone please tell me if there is anything wrong with the code. I am trying to search for files and check them if they are folders if the folder name is bin then change the directory to bin and read the file names and store the location. I have maintained an hash to store the filename as the key and location as the value. I would like to enter the filename to the Hash and also the location. But there is something that has gone wrong in the above code. Could some one please help.

Many thanks
 
what do you mean by causing "grief"? A warning? An Error?

the backtik (`) just before \n might be a syntax error:

chdir($f) or die "Unable to enter $folder:$!`\n";

remove it and see if that helps:

chdir($f) or die "Unable to enter $folder:$!\n";
 
Code:
foreach my $f (@files){
        next if ([b]$name[/b] eq "."); 
        next if ([b]$name[/b] eq "..");
What is $name here? Don't you mean $f?
Code:
sub addFiletoHash[b]($f,$loc)[/b]{
    my $folder = shift;
    [b]my $path = $loc;[/b]
    print "Folder is $folder and location is $loc\n";
        [b]chdir($f)[/b] or die "Unable to enter $folder:$!`\n";
    &readfile($folder,$path);
}
1. The ($f, $loc) after the sub name is not valid Perl syntax. Get rid of it.
2. To unpack your arguments:
Code:
my ($folder, $path) = @_;
3. I think you mean chdir($path), not chdir($f).

Put use strict and use warnings near the top of your script, and it will help you avoid errors like this.

HTH

 
Code:
sub scancm(){
	my ($dirname, $location) = @_;
	chomp $location;
	#print "The Directory name is $dirname and Location is $location\n";
	chdir($dirname) or die "Unable to enter dir $dirname:$!\n";
	$pwd = &cwd;
	#print "PWD = $pwd and $tm\n"; #DEBUG STATEMENT
	
	opendir(CM, ".") or die "Unable to open $workdir:$!\n";
    my @files = readdir(CM) or die "Unable to read $workdir:$!\n";
    closedir(CM);
	
	foreach my $f (@files){
        next if ($f eq "."); 
        next if ($f eq "..");

		if (-d $f) {
			if (($f eq "bin")) {
				$loc = "$pwd\/bin";
				#print "Entering bin and Folder = $f and Path = $loc\n";
			    &addFiletoHash($f,$loc);

			}
			if (($f eq "int")) {
				$loc = "$pwd\/int";
				#print "Entering int and Folder = $f and Path = $loc\n";
				&addFiletoHash($f,$loc);
			}
		}
	}
	
}

# Add files to the hash and also the location.
sub addFiletoHash(){
	my ($folder, $path) = @_;
	print "Folder is $folder and location is $loc\n";
	[bold]&readfile($folder,$path);[/bold]
}

sub readfile(){
	my ($f, $loc)=@_;
	chdir($f) or die "Unable to enter $path:$!\n";
}

As pointed out there was a syntax error in the print statement. When I run the script without the line
&readfile($folder,$path); I get the right output.
Expected Output:
Folder is bin and location is C:/TEST/CM12653/bin
Folder is int and location is C:/TEST/CM12653/int
Folder is bin and location is C:/TEST/CM8993/bin
Folder is int and location is C:/TEST/CM8993/int

But when I add &readfile($folder,$path); in the code I get
Folder is bin and location is C:/TEST/CM12653/bin
Folder is bin and location is C:/TEST/CM8993/bin

Could you please let me know why is this the case.

Many thanks.
 
I think the line &readfile($folder,$path); in subroutine addFiletoHash is causing the program to behave differently.

 
just curious, why are you coding your subs as prototypes? There is no need for that. For example:

sub scancm(){

you could write it like this:

sub scancm {

if you were using stict your script would bomb because $path is not packaged in sub readfile.
 
Copy and paste the code, and tell me if it works.
Code:
#!/usr/bin/perl -w

use strict;
use File::Find;

my %hash;

find(\&wanted, '.'); 

sub wanted{
   my $path = $dir = $File::Find::dir;
   if ($path =~ /bin$|int$/){
      $dir =~ s/.*\///;
      print "Folder is $dir and location is $path\n";
      $hash{$dir} = $path;
   }
}
Ithink is that simple, take a look at the module here ->[File::Find]


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I get this error when trying to execute the program.
Also the other thing that I noticed in the code here is that the folder that you add in here is one folder above what I require.

The structure I have is a
rootfolder/CM folder/bin
rootfolder/CM folder/int

and this pattern continues. So what I do is check if they are int or bin and then get them on to the appropriate hashes. with hash storing the name as the key and the path as the value.

Global symbol "$dir" requires explicit package name at scancm1.pl line 11.
Global symbol "$dir" requires explicit package name at scancm1.pl line 13.
Global symbol "$dir" requires explicit package name at scancm1.pl line 14.
Global symbol "$dir" requires explicit package name at scancm1.pl line 15.
Execution of scancm1.pl aborted due to compilation errors.

Thanks for your help.
 
Hi,

Try changing;

sub wanted{

to;

Code:
sub wanted{
 my $dir;
..rest of code

Hope that helps.

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top