473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

opening file using wild card

Looking2Learn
Hey,
I'm trying to open a txt file, but I won't know the first three characters of the filename. For example, I want to open a "user.txt" file, and I know it will have three digits in front of it, like "123_user.txt", but they could be any three digits. Is there a way to introduce a wild card into the open command to specify this?

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2.  
  3. open (FILE, "***_user.txt") or die "Can't find file!" ;
  4.     print "Found file!" ;
  5. close (FILE) ;
  6.  
Jul 3 '08 #1
3 6611
nithinpes
410 Expert 256MB
Wildcard cannot be used with open function. Are you trying to open any file/all files in the current directory that matches your criteria?
I am assuming that you want to search for files that match the criteria and open them. In this case, you can parse through the directory and fetch all files that match the criteria and open them for reading as below:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use strict;
  3.  
  4. my @files;
  5. opendir(DIR,".") or die "opening directory failed:$!";  # '.' for pwd.Use dir path if required. 
  6. while(my $filename=readdir(DIR)){
  7.  push @files,$filename if($filename=~/^\d\d\d_user\.txt$/);
  8. }
  9. closedir(DIR);
  10. foreach my $file (@files) {
  11. print "found $file\n";
  12. open (FILE, "$file") or die "Can't find $file!" ;
  13. while(<FILE>) {
  14. ### read
  15. }
  16. close (FILE) ;
  17. }
  18.  
Jul 3 '08 #2
nithinpes
410 Expert 256MB
In case you want to open any single file that match the criteria assign $filename to $file when a match is found and quit the while(my $filename=readdir(DIR)) {} loop.
Jul 3 '08 #3
Great, thanks!

And yes, there will only be one matching file. So, I'll use:

Expand|Select|Wrap|Line Numbers
  1. while(my $filename=readdir(DIR)){
  2. my file = $filename if($filename=~/^\d\d\d_user\.txt$/);
  3. }
  4.  
Jul 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gerry Viator | last post by:
Hi all, I want to remove the test after a period? Why does the wild card * not work? Dim test As String = "This is just a test report. ERCP" MessageBox.Show(Replace(test, ".*", "."))
1
by: Tempy | last post by:
Could somebody please tell me if it is possible and if how? I am wanting to do : Iif(="*PE","Y","N") In other word i want it to fine HZPE, AEPE, HEPE etc... Les Stout *** Sent via Devdex...
1
by: MFRASER | last post by:
Is there a quick way to get the most recently created file based on a wild card search? for example if I want to get the latest file in c:\temp\*.xml
1
by: =?Utf-8?B?S3Jpc3RpbmUgdGhlIERyYWdvbkxhZHk=?= | last post by:
Can anyone advise re spam emails which come through with the word V***gra (where the * are varying letters) - how do I specify the word just as I've typed the word with the asterisks? I can't find...
5
by: Dave | last post by:
I need to filter an Access 2000 result set in ASP 30 using the ADO recordset.filter. I build the filter in pieces. The first clause of the filter is this... WHERE word LIKE 'S%' ... to...
8
by: hiro | last post by:
Hi there, I'm very new to python, the problem I need to solve is whats the "best/ simplest/cleanest" way to read in multiple files (ascii), do stuff to them, and write them out(ascii). --...
2
by: psbasha | last post by:
Hi, How to get a File Wild Card using Tkinter Module?. Could anybody has used this option.If so, the smaple snippet of the code will really help me. Thanks PSB
0
by: jmelasic1 | last post by:
I am working on a form now that has two things in it: a text box and a subform. The subform is from a query. The text box is just a normal text box. However, I would like to link the text box to...
7
by: W. eWatson | last post by:
Is it possible to do a search for a wild card string in another string. For example, I'd like to find "v*.dat" in a string called bingo. v must be matched against only the first character in bingo,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.