0
$\begingroup$

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"}

$\endgroup$
  • $\begingroup$ Say lst is your list. DeleteCases[lst, x_ /; StringMatchQ[x, ___ ~~ "inelastic" ~~ ___]] will give you the list sans any with "inelastic" as part of name. $\endgroup$ – ciao Jul 14 '14 at 20:28
3
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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.) $\endgroup$ – Mr.Wizard Jul 16 '14 at 8:24
5
$\begingroup$

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
$\endgroup$
  • $\begingroup$ Thanks for pointing me to the better use of StringFreeQ! You have the most elegant answer to this question by far. $\endgroup$ – seismatica Jul 16 '14 at 8:36
3
$\begingroup$

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*"] &]
$\endgroup$
  • $\begingroup$ Thanks but that way i only choose the one I don't want $\endgroup$ – Kafkarudo Jul 14 '14 at 20:17
  • $\begingroup$ I tried 1*.txt and it works but left me out the ones with one number $\endgroup$ – Kafkarudo Jul 14 '14 at 20:20
  • $\begingroup$ @user18508 answer updated $\endgroup$ – Szabolcs Jul 14 '14 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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