Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have this list of files, i'm using FileNames[{"*.txt"}, NotebookDirectory[]] to import it, but i just want the ones without inelastic on the name. How can i select just by the name? Thanks

{"10.txt","11.txt","12.txt","13.txt","14.txt","15.txt","16.txt","17.txt","18.txt","19.txt","1.txt","20.txt","2.txt","3.txt","4.txt","5.txt","6.txt","7.txt","8.txt","9.txt","inelastico1.txt","inelastico2.txt","inelastico3.txt","inelastico4.txt","inelastico5.txt","inelastico6.txt","inelastico7.txt","inelasticov2-10.txt","inelasticov2-1.txt","inelasticov2-3.txt","inelasticov2-4.txt","inelasticov2-5.txt","inelasticov2-6.txt","inelasticov2-7.txt","inelasticov2-8.txt","inelasticov2-9.txt","inelasticov3-10.txt","inelasticov3-11.txt","inelasticov3-12.txt","inelasticov3-13.txt","*inelasticov3-14.txt","inelasticov3-14.txt","inelasticov3-15.txt","inelasticov3-16.txt","inelasticov3-1.txt","inelasticov3-2.txt","inelasticov3-3.txt","inelasticov3-4.txt","inelasticov3-5.txt","inelasticov3-6.txt","inelasticov3-7.txt","inelasticov3-8.txt","inelasticov3-9.txt"}

share|improve this question
    
Say lst is your list. DeleteCases[lst, x_ /; StringMatchQ[x, ___ ~~ "inelastic" ~~ ___]] will give you the list sans any with "inelastic" as part of name. –  rasher Jul 14 at 20:28

3 Answers 3

up vote 2 down vote accepted

To get the file list without the files containing "inelastic", you can use:

Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False]

Then, to import them:

Import[#] & /@ %

One-step solution:

Import[#] & /@ 
 Cases[{your file list here}, 
  x_String /; StringMatchQ[x, "*inelastic*"] == False]

Edit: Out of curiosity, I tried to trim the file list without using StringMatchQ, and using Pick with StringCases seems to work as, per its documentation, StringCases can thread over a list.

Mathematica graphics

StringCases[fileList, ___ ~~ "inelastic" ~~ ___]
(* {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}, {"inelastico1.txt"}, {"inelastico2.txt"}, \
{"inelastico3.txt"}, {"inelastico4.txt"}, {"inelastico5.txt"}, \
{"inelastico6.txt"}, {"inelastico7.txt"}, {"inelasticov2-10.txt"}, \
{"inelasticov2-1.txt"}, {"inelasticov2-3.txt"}, \
{"inelasticov2-4.txt"}, {"inelasticov2-5.txt"}, \
{"inelasticov2-6.txt"}, {"inelasticov2-7.txt"}, \
{"inelasticov2-8.txt"}, {"inelasticov2-9.txt"}, \
{"inelasticov3-10.txt"}, {"inelasticov3-11.txt"}, \
{"inelasticov3-12.txt"}, {"inelasticov3-13.txt"}, \
{"*inelasticov3-14.txt"}, {"inelasticov3-14.txt"}, \
{"inelasticov3-15.txt"}, {"inelasticov3-16.txt"}, \
{"inelasticov3-1.txt"}, {"inelasticov3-2.txt"}, \
{"inelasticov3-3.txt"}, {"inelasticov3-4.txt"}, \
{"inelasticov3-5.txt"}, {"inelasticov3-6.txt"}, \
{"inelasticov3-7.txt"}, {"inelasticov3-8.txt"}, \
{"inelasticov3-9.txt"}} *)

Pick[fileList, StringCases[fileList, ___ ~~ "inelastic" ~~ ___], {}]
(* {"10.txt","11.txt","12.txt","13.txt","14.txt","15.txt","16.txt",\
"17.txt","18.txt","19.txt","1.txt","20.txt","2.txt","3.txt","4.txt",\
"5.txt","6.txt","7.txt","8.txt","9.txt"} *) 

Interestingly, StringCases don't seem to work with the * pattern since only StringMatchQ seems to work with what's called "abbreviated string patterns".

Mathematica graphics

StringCases[fileList, "*inelastic*"]
(* {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}} *)

Edit 2: I just realized that StringMatchQ (and StringFreeQ) also threads across a list, so you can also do it like this (tweaks to StringFreeQ from @Mr.Wizard):

Pick[fileList, StringFreeQ[fileList, "inelastic"], True]

or

Pick[fileList, StringMatchQ[fileList, "*inelastic*"], False]

Note that only StringMatchQ (and not even StringFreeQ) can use the * notation.

Pick[fileList, StringFreeQ[fileList, "*inelastic*"], True]
(* this will give incorrect answer *)

All of the methods have identical speed (0.) for your example but I have no clue about their relative speeds for a large input.

share|improve this answer
1  
I missed your use of StringFreeQ before posting my answer, but you are making its use more complicated that necessary. Take a look. (And +1.) –  Mr.Wizard Jul 16 at 8:24

I would use StringFreeQ:

(* files = {"10.txt", "11.txt", . . ., "inelasticov3-8.txt", "inelasticov3-9.txt"}; *)

Select[files, StringFreeQ[#, "inelastic"] &]
 {"10.txt", "11.txt", "12.txt", "13.txt", "14.txt", "15.txt", "16.txt", "17.txt", 
  "18.txt", "19.txt", "1.txt", "20.txt", "2.txt", "3.txt", "4.txt", "5.txt", "6.txt", 
  "7.txt", "8.txt", "9.txt"}

Or:

# ~Pick~ StringFreeQ[#, "inelastic"] & @ files
share|improve this answer
    
Thanks for pointing me to the better use of StringFreeQ! You have the most elegant answer to this question by far. –  seismatica Jul 16 at 8:36

You need to use the pattern "*inelastic*.txt" in FileNames. This will only return the list of files you need.

Then use Map with pure functions (Function) to import all of them in one go: Import[#, "Table"]& /@ fileNames or similar.


To use everything except file names with "inelastic", you can use Select,Cases,DeleteCases, etc. withStringMatchQ`.

Example:

Select[fileNames, Not@StringMatchQ[#, "*inelastic*"] &]
share|improve this answer
    
Thanks but that way i only choose the one I don't want –  Kafkarudo Jul 14 at 20:17
    
I tried 1*.txt and it works but left me out the ones with one number –  Kafkarudo Jul 14 at 20:20
    
@user18508 answer updated –  Szabolcs Jul 14 at 20:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.