How to loop through files in a folder?

Particularly useful chunks of hoc and/or NMODL code. May be pedestrian or stunningly brilliant, may make you gasp or bring tears to your eyes, but always makes you think "I wish I had written that; I'm sure going to steal it."

How to loop through files in a folder?

Postby Krollibrius » Thu Mar 16, 2017 5:31 am

Hello all,

Is there a way I can loop through files of a folder in hoc language?

Basically, I am using a model from the NEURON database (https://senselab.med.yale.edu/modeldb/S ... del=151460) as a starting point for my project. What I need to do is running successive simulations with different input files for the waveform (no need to focus on what's the input file is, I don't think that's relevant). Everything's OK manually, but I have around 50 simulations (50 different input files) to run, so that's not a possibility in practice. Is there a way I can loop through the files in the folder so NEURON takes each as input for one simulation?

I also thought about having one big file which would contain all the files I need one after the other (everything appended basically), and somehow make NEURON read the values from line 0 to 500 (for eg.) for the first simulation, then line 501 to 1001 for the second, etc. However, first solution, if possible, would be much more elegant and practical...

Can someone help?

Thanks!
Krollibrius
 
Posts: 1
Joined: Wed Mar 15, 2017 5:48 pm

Re: How to loop through files in a folder?

Postby ted » Fri Mar 17, 2017 10:49 am

If the input data files are of the form
inputfilebasenamej.ext
and the output files are to be of the form
outputfilebasenamej[i].ext
where [i]j
is an index that runs from 1 to N,
you could (in pseudocode)
Code: Select all
for i=1,N {
  fill a string with the generated input file name
  open the file that has that name and do whatever you need to do with its contents
  run a simulation
  do whatever postprocessing is necessary
  fill a string with the generated output file name
  write the results to a file that has that name
}
Use sprint to generate the names of the input and output files, and use the File class's methods to deal with opening and closing the data files; sprint and the File class are well documented in the Programmer's Reference. An advanced search for posts that contain the string
sprint
that are written by
ted
will bring up many related examples of the use of sprint.

If instead the input data files have very different names, you could do this:
1. Produce a plain text file called inputfiles in which each line contains just one entry: the name of the input data file. Whether you do that manually with a text editor or by a shell command is up to you.
2. Do the following
Code: Select all
REPEAT
  read a file name from inputfiles into a string
  open the file that has that name and do whatever you need to do with its contents
  run a simulation
  do whatever postprocessing is necessary
  generate the name of the output file (you might want to include an index number in the name)
  write the results to a file that has that name; you might want the first line of this file to contain the name of the input data file
UNTIL no more input file names remain to be read
ted
Site Admin
 
Posts: 4875
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine


Return to NEURON hacks

Who is online

Users browsing this forum: No registered users and 1 guest

cron