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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through file with *.file extension

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi All,

Does anyone know script routine for looping through files with extension "*.file"?

For example I have the following files in the directory:
first.file
second.file
third.file
fourth.file
:
:
:
so on, so forth....

How can I open each file, look for the pattern within the file, then move to the next file (loop) until I am able to browse through all of the "*.file" in the directory...

Best Regards,
CGrace7
 
Try looking at thread219-313393
It is similar to what you're asking I think.

Does that help?

tgus

____________________________
Families can be together forever...
 
Hi tgus,
Well, not exactly, the thread you mentioned only discuss how to list the files. In my case, I want to go through the files 1 by 1 ("*.file") to open it and look for a specific pattern and so on....
Thanks,
CGrace7
 
CGrace7,
Using the other example in Thread219-313393 you can find the files.
Once you know the name of the file which will be provided by the example, you can then open the file and manipulate it.
Something like this;

open(FILE,&quot;<$text.txt&quot;);
@a = <FILE>;
close(FILE);

foreach $a (@a){
chomp
print $a;
}

See if theis helps.


tgus

____________________________
Families can be together forever...
 
You can use &quot;file globbing&quot; in a while loop to list the files in the current directory one by one, like this:

while(<*.file>) {
open(FILE, &quot;<$_&quot;) || die &quot;Can't open $_!&quot;;
my @lines = <FILE>;
foreach my $line (@lines) {
*** do something with each line ***
}
}

The &quot;open&quot; might need the absolute pathname with the filename, so you may need to add &quot;/path/to/file&quot; onto the front of the filename before the open.

and if you didn't want to chdir to the directory first, then you can probably do this(although I'm really not sure if this will work):

while(</path/to/dir/*.file>) {
*** do something with a file ***
}

HTH. Hardy Merrill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top