• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Global File Open

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
WesleyWee
Asked:
WesleyWee
  • 7
  • 5
  • 3
  • +2
1 Solution
 
mtrimpeCommented:
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
 
rj2Commented:
#!/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
 
mtrimpeCommented:
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
Independent Software Vendors: 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!

 
ozoCommented:
#if you're sure there's only one of such file
open FILE,"<".<*ABC.EFG> or warn $!;
0
 
WesleyWeeAuthor Commented:
Ozo i keep getting a warning saying that "Something is wrong..."
0
 
mtrimpeCommented:
I think that is because it just doesn't work. Try my or rj2's approach.
0
 
TintinCommented:

#!/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
 
WesleyWeeAuthor Commented:
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
 
WesleyWeeAuthor Commented:
Thnaks
0
 
WesleyWeeAuthor Commented:
Is there a free kind?
0
 
TintinCommented:
PAR is free
0
 
WesleyWeeAuthor Commented:
What i meant is the compiler to make a perl code exectuable free?

i can't seem to make it run also
0
 
TintinCommented:
PAR pretty much does what perl2exe does, except that it is free.
0
 
WesleyWeeAuthor Commented:
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
 
TintinCommented:
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
 
WesleyWeeAuthor Commented:
ThanKz alot!!!
0

Featured Post

Independent Software Vendors: 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!

  • 7
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now