Jump to content

[SOLVED] Open File Based on Wild Card - put file name into string


DamienRoche

Recommended Posts

Here's what I want to do:

 

open a file in a directory which has a certain pattern in it's name..then, after opening the file, put the file name into a variable.

 


/// FILE ON SERVER = (1)damien.txt

$ptn = "(1)"

$lines = file("$ptn___I AM STUCK HERE___.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

/// output is sorted.

 

So, I need to open any file (there will only be one) with that pattern in, maybe by using somekind of wild card..but after opening the file, I need to know what the rest of the name was. I know, I said there would only be one...but that's what I need.

 

Any one got any ideas on how I can tackle this?

 

Thanks.

 

Link to comment
Share on other sites

Just checked that out..it is of some use and now I am one step closer.

 

so now I have used glob and it has found the name.

 

Let's say the name is (1)damien_full.txt

 

How do I put that file name into a string and then pull out everything between (1) and _full and put it in a variable. So, in essence, how do I get 'damien' from that file name into a variable?

 

Any ideas?

 

Thanks as well! that glob function is incredible!

Link to comment
Share on other sites

That last one hasn't worked. I need something relative not absolute. That seems to only work on the exact string (1)damien_full.txt

 

That regexp is seriously above my head. How can I alter it to make this work.

 

If I can use a substr(); it would be great but that seems to only work with numbering the pos in a string. Can I not number the pos based on special characters? namely, the ) and _ in the given string.

 

Any one else got any other ideas?

 

Thanks any way!

Link to comment
Share on other sites

hmm....

 

It seems to me what you are saying is that you have a list of files and you want to search for a file that contains for instance "dam" and that there's only going to be one file with that in it, and it's going to be for example (1)damien_full.txt so you want to be able to extract "damien" from that, but the catch is that file won't necessarily look like that, it could look like

 

(1)damien_full.txt

damien_full.txt

(1)damienfull.txt

damien.txt

etc...

 

Only way regex really works is if there are, well, regular expressions to work with.  If you are telling us that beyond using glob, virtually everything else in that filename could be variable, then there's really no way to extract your info from it.  

 

edit:

 

Okay well apparently that's not exactly what you're saying, as something seemed to work out for you...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.