Re: Filenames with ToString
- To: mathgroup at smc.vnet.net
- Subject: [mg99969] Re: Filenames with ToString
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Wed, 20 May 2009 05:02:19 -0400 (EDT)
- References: <guu2ef$67d$1@smc.vnet.net>
Alain Mazure wrote: > Hi again > > I have several files with extension .jpg of the type > > "C1822_8_025_1.jpg", "C1822_8_025_2.jpg" > "C1943_11_025_1.jpg", "C1943_11_025_2.jpg" ..... > > I know the first part of the name which was given by : "" ~~ > ToString[NAME2] with NAME2: C1822, C1943 ... I don't understand what you try to achieve with: "" ~~ ToString[NAME2] it is a string pattern that matches an empty string followed by a string representation of the content of NAME2, which matches just the string itself and thus is obviously automatically simplified to just the string by mathematica... > How to use FileNames in order to have, for a given NAME2 all the *.jpg > files in order to import them ? > > The command: FileNames["" ~~ ToString[NAME2]*".jpg"] does not work I think the (string) pattern you are looking for would look like this: ToString[NAME2] ~~ __ ~~ ".jpg" If you need that the filename starts with ToString[NAME2], this would be the correct syntax: StartOfString ~~ ToString[NAME2] ~~ __ ~~ ".jpg" here are some checks: NAME2 = C1822 names = {"C1822_8_025_1.jpg", "C1822_8_025_2.jpg", "xC1822_asdf.jpg", "C1943_11_025_1.jpg", "C1943_11_025_2.jpg"} StringCases[names, ToString[NAME2] ~~ __ ~~ ".jpg"] if you want the third name not to be matched, this is what you need: StringCases[names, StartOfString ~~ ToString[NAME2] ~~ __ ~~ ".jpg"] now this should do what you need: FileNames[ToString[NAME2] ~~ __ ~~ ".jpg"] hth, albert