From: S. B. Gray on
I have a folder like this: (SetDirectory is correctly set.)

FileNames[]

{Linkmax-n=06-3457873882.nb, (* This is a list of strings. *)
Linkmax-n=06-3487762236.nb,
Linkmax-n=07-3457879999.nb,
Linkmin-n=06-3457873882.nb,
Linkmin-n=06-3487762236.nb,
Linkmin-n=07-3457879999.nb,
Link-n=07, (* For testing.*)
Testname-n=07-3457879999.nb} (* For testing.*)

(* Why do some of these functions work and others do not? *)

In[856]:= FileNames["Link*06*"]
Out[856]= {
"Linkmax-n=06-3457873882.nb", \
"Linkmax-n=06-3487762236.nb",
"Linkmin-n=06-3457873882.nb", \
"Linkmin-n=06-3487762236.nb"} (* What I expect and what I want.*)

In[847]:= FileNames["Link*-n=*"]
Out[847]= {
"Linkmax-n=06-3457873882.nb", \
"Linkmax-n=06-3487762236.nb",
"Linkmax-n=07-3457879999.nb", \
"Linkmin-n=06-3457873882.nb",
"Linkmin-n=06-3487762236.nb", \
"Linkmin-n=07-3457879999.nb",
"Link-n=07"} (* Also what I would expect. *)

In[867]:= num = 6;
FileNames["Link*-n=" <> "06"] (* Can't I use <> here? *)
Out[868]= {}(* I thought this would give same as Out[847] ?? *)

In[861]:= num=6;
numstr=If[num<10, numstr="0"<>ToString[num],
numstr = ToString[num]]
FileNames["Link*" <> ToString[numstr]] (* No good also. ?? *)
Out[862]= "06"
Out[863]= {}(* I thought this would give same as Out[847] ?? *)

In[851]:= rname = "Link*-n=" <> numstr
FileNames[rname] (* Also not acceptable, but why? *)

Out[851]= "Link*-n=06"
Out[852]= {}(* I thought this would give same as Out[847] ?? *)

Clearly there is something missing in my understanding of file name
strings. What is the difference between *, __, and ~~, and where does
one use which of them? Is there a clear explanation of these issues
anywhere? I would certainly appreciate some help here. Thank you.

Steve Gray

From: David Bailey on
On 13/07/10 10:27, S. B. Gray wrote:

>
> In[867]:= num = 6;
> FileNames["Link*-n="<> "06"] (* Can't I use<> here? *)

The asterisk stands for zero or more characters. You should have used
FileNames["Link*-n="<> "06*"] since none of your file names ended in 06.


> In[861]:= num=6;
> numstr=If[num<10, numstr="0"<>ToString[num],
> numstr = ToString[num]]
> FileNames["Link*"<> ToString[numstr]] (* No good also. ?? *)

Again, this failed for the same reason. Note also, that you really
should decide if you are setting numstr inside the body of the If or
outside it!

Note also that from experiment, it would seem that you can use
"abbreviated patterns" - i.e. the asterisk, or string expressions (~~),
but you can't mix the two.

David Bailey

http://www/dbaileyconsultancy.co.uk

From: Albert Retey on
Am 13.07.2010 11:27, schrieb S. B. Gray:
> I have a folder like this: (SetDirectory is correctly set.)
>
> FileNames[]
>
> {Linkmax-n=06-3457873882.nb, (* This is a list of strings. *)
> Linkmax-n=06-3487762236.nb,
> Linkmax-n=07-3457879999.nb,
> Linkmin-n=06-3457873882.nb,
> Linkmin-n=06-3487762236.nb,
> Linkmin-n=07-3457879999.nb,
> Link-n=07, (* For testing.*)
> Testname-n=07-3457879999.nb} (* For testing.*)
>
> (* Why do some of these functions work and others do not? *)
>
> In[856]:= FileNames["Link*06*"]
> Out[856]= {
> "Linkmax-n=06-3457873882.nb", \
> "Linkmax-n=06-3487762236.nb",
> "Linkmin-n=06-3457873882.nb", \
> "Linkmin-n=06-3487762236.nb"} (* What I expect and what I want.*)
>
> In[847]:= FileNames["Link*-n=*"]
> Out[847]= {
> "Linkmax-n=06-3457873882.nb", \
> "Linkmax-n=06-3487762236.nb",
> "Linkmax-n=07-3457879999.nb", \
> "Linkmin-n=06-3457873882.nb",
> "Linkmin-n=06-3487762236.nb", \
> "Linkmin-n=07-3457879999.nb",
> "Link-n=07"} (* Also what I would expect. *)
>
> In[867]:= num = 6;
> FileNames["Link*-n=" <> "06"] (* Can't I use <> here? *)
> Out[868]= {}(* I thought this would give same as Out[847] ?? *)
>
> In[861]:= num=6;
> numstr=If[num<10, numstr="0"<>ToString[num],
> numstr = ToString[num]]
> FileNames["Link*" <> ToString[numstr]] (* No good also. ?? *)
> Out[862]= "06"
> Out[863]= {}(* I thought this would give same as Out[847] ?? *)
>
> In[851]:= rname = "Link*-n=" <> numstr
> FileNames[rname] (* Also not acceptable, but why? *)
>
> Out[851]= "Link*-n=06"
> Out[852]= {}(* I thought this would give same as Out[847] ?? *)

I think in all the examples that don't work you are just missing a
trailing "*", which you can append with StringJoin.

> Clearly there is something missing in my understanding of file name
> strings. What is the difference between *, __, and ~~, and where does
> one use which of them? Is there a clear explanation of these issues
> anywhere? I would certainly appreciate some help here. Thank you.

I don't think that there is any difference in file name patterns and
general string patterns. Using "*" in strings is the "old fashioned"
string pattern matching, which is less powerful but somewhat limited,
using ~~ in combination with _ and __ gives you a much more powerful way
to express your string patterns and the patterns will look more
mathematica like than the simple string patterns. I would guess you
should use what is simpler for you, but I found that some functions like
StringCases will only work as advertised for StringPattern (~~) when
e.g. used with a list of strings, so I think it usually is the better
choice. On the other hand the full power will probably have some
overhead so might be not the best choice when working with large or many
strings, but I haven't tested that...

hth,

albert