2
$\begingroup$

I have set of files in the format 20160615-1-0.asc, 20160615-2-0.asc,... 20160615-5-0.asc etc. The following commands can List the file names at once which can be used to import later.

  files = Range[1, 5]; 
  c = "D:\\20160615\\20160615-" <> ToString[#] <> "-0.asc" & /@files

I want to use "0" part of the "-0.asc" string as a variable so that I can make different lists of multiple files like {-1-0.asc, -2-0.asc, ...-5-0.asc}, {-1-1.asc, -2-1.asc, ...-5-1.asc},{-1-3.asc, -2-3.asc, ...-5-3.asc}..and so on.

$\endgroup$
  • $\begingroup$ Why not use Table[]? Maybe use StringTemplate[] or StringForm[], too. $\endgroup$ – J. M. will be back soon Jun 17 '16 at 12:35
2
$\begingroup$

a good use for the "new in 10" StringTemplate :

 Table[
    StringTemplate["D:\\20160615\\20160615-`1`-`2`.asc"][i, j], {i, 3}, {j, 3}]

enter image description here

incedentally, if you need to zero pad your numbers as they often occur in file names you can use this mess:

Table[
 StringTemplate[
   "D:\\20160615\\20160615-<*TemplateExpression[IntegerString[`1`,10,2]]*>-`2`.asc"
    ][i, j], {i, 3}, {j, 3}]

enter image description here

$\endgroup$
4
$\begingroup$

It seems that a simple modification of your code will work.

fileList[prefix_String, m_Integer, n_Integer] :=
  With[{dash = "-"}, 
    prefix <> dash <> ToString[#] <> dash <> ToString[n] <> ".asc"] & /@ Range[m]

Then

fileList["D:\\20160615\\20160615", 5, 1]

gives

{"D:\\20160615\\20160615-1-1.asc", "D:\\20160615\\20160615-2-1.asc", "D:\\20160615\\20160615-3-1.asc", "D:\\20160615\\20160615-4-1.asc", "D:\\20160615\\20160615-5-1.asc"}

$\endgroup$

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.