?
Solved

Global File Open

Posted on 2003-03-23
17
Medium Priority
?
230 Views
Last Modified: 2010-03-05
How to open any files (taking that there is only one file of that file name inside)

Eg:
the file's name 56754ABC.EFG
the filename is always changing automatically (at any point in time; there is always only one of such file) the only thing that doesn't change is ...ABC.EFG
if the numbers keep changing how do i make my code to open the file of random file name? only reconicgion is the ...ABC.EFG name.
0
Comment
Question by:WesleyWee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +2
17 Comments
 
LVL 1

Expert Comment

by:mtrimpe
ID: 8190288
to do this you'll have to scan the directory in which the files are placed first.

This is an example:
------------------------------------------------------
use File::Find;

find(\&Check, @directories_to_seach);

sub Check {
   if ( substr($_, 6) eq "ABC.EFG" ) {
      # OPEN YOUR FILE AND DO THE PROCESSING HERE
      # THE FILE NAME IS IN $_ AND THE FULL PATH NAME
      # IS IN $File::Find::name
   }
}
------------------------------------------------------

The rest should be adaptable to your specific needs. For more info, mail me on mtrimpe@inspirex.nl
0
 
LVL 10

Expert Comment

by:rj2
ID: 8190312
#!/usr/bin/perl
$dir='/home/WesleyWee';
opendir(DIR, $dir) || die("Can not open dir because $!");
@files = grep /ABC.EFG$/, readdir DIR;
closedir(DIR);
if(scalar(@files)==1) {          
     open(FILE,$dir . '/' . $files[0]) || die("Can not open file because $!");
     #do something with file
     close(FILE);
} else {
     die("Invalid number of entries: " . scalar(@files));
}
0
 
LVL 1

Expert Comment

by:mtrimpe
ID: 8190331
OK, that'll work as well, as long as you don't need to search the subdirectories.

The advantage of File::Find is that is far more platform-independent.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 8190681
#if you're sure there's only one of such file
open FILE,"<".<*ABC.EFG> or warn $!;
0
 

Author Comment

by:WesleyWee
ID: 8191677
Ozo i keep getting a warning saying that "Something is wrong..."
0
 
LVL 1

Expert Comment

by:mtrimpe
ID: 8191754
I think that is because it just doesn't work. Try my or rj2's approach.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 80 total points
ID: 8192331

#!/usr/bin/perl

foreach my $file (<*ABC.EFG>) {
   open FILE, $file or die "Can not open $file $!\n";

   while (<FILE>) {
      # do whatever
   }

   close FILE;
}
0
 

Author Comment

by:WesleyWee
ID: 8192806
Thanks rj2 , mtrimpe and tintin for helping me.

jus a question:
Is there such a thing as Perl Compiler making it *exe? for windows?
0
 

Author Comment

by:WesleyWee
ID: 8195940
Thnaks
0
 

Author Comment

by:WesleyWee
ID: 8196064
Is there a free kind?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8199775
PAR is free
0
 

Author Comment

by:WesleyWee
ID: 8207281
What i meant is the compiler to make a perl code exectuable free?

i can't seem to make it run also
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8207304
PAR pretty much does what perl2exe does, except that it is free.
0
 

Author Comment

by:WesleyWee
ID: 8208540
oh... ok ok

Now i've gotten the PAR source how do i run it?

for perl2exe i jus drag *pl to the perl2exe.exe and the *exe is made but doesnt' work. and it's also a pay warez
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8209518
I haven't actually used PAR myself, but the relevant doco you need to read is:

http://search.cpan.org/author/AUTRIJUS/PAR-0.66/script/pp
0
 

Author Comment

by:WesleyWee
ID: 8218596
ThanKz alot!!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question