Automating Data Collection with Batch Scripts

Thu 10 April 2014 by Eoin Travers

Batch files (.bat) are a handy, but sometimes bewildering tool for automating tasks on Windows. and are equivalent to shell scripts (.sh) on Linux/Unix/OSX, which I don't have the luxury of using for data collection.

I find these files useful for taking care of some of the simple tasks in data collection, including: - allocating and incrementing subject numbers; - counterbalancing participants between conditions; - running multiple experiment scripts in succession for the same participant - providing participants with a big, professional looking Start Experiment icon on the desktop to click when they're ready.

All of this requires that the software you use to run your actual experiments can be passed information from the command prompt. OpenSesame, which I mostly use, can, but E-Prime can't, so I'll give examples using OpenSesame.

The basic syntax of running an OpenSesame experiment from the command prompt goes

C:\path\to\opensesamerun C:\path\to\experiment.opensesame -s 101 -l C:\path\to\save_data\subject_101.csv -f

where -s 101 sets the subject number, -l C:\path\to\save_data\subject_101.csv specifies where to save the data file, and -f runs the experiment in full screen mode.

The subject number can be read from a file (in this case, I've created a file called subject_nr.txt, containing just the value 101:

set number_path=C:\path\to\subject_nr.txt
if exist %number_path% for /f "delims=" %%i in (%number_path%) do set /A subject_nr=%%i
echo Subject number = %subject_nr%.

It makes sense to increment the number in the file by 1, for the next participant.

set /A next_subject = %subject_nr% + 1
echo %next_subject% > %number_path%

From here, we can run the experiment, using this subject number in the experiment, and the data file name.

echo Starting experiment...
C:\path\to\openseame\opensesamerun C:\path\to\experiment\Part1.opensesame -s %subject_nr% -l C:\path\to\save\subject_%subject_nr%.csv -f

While we're at it, we can run a second experiment file when the first one finishes, using the same subject number.

echo Starting part 2...
C:\path\to\openseame\opensesamerun C:\path\to\experiment\Part2.opensesame -s %subject_nr% -l C:\path\to\save\part_2_subject_%subject_nr%.csv -f

As an extra touch, I leave the console onscreen for 2 seconds after everything has finished

echo Experiment complete!
echo Goodbye.

rem Pause for 2 seconds
ping 192.0.2.2 -n 1 -w 2000 > nul

Put together, the .bat file looks as follows. Note that lines begining in rem are comments - they don't do anything, but are there to explain the rest of the script. Lines begining in echo are printed to the console.

rem Prevent unwanted output to the user.
@echo off

rem Get subject number from file
set number_path=C:\path\to\experiment\data\subject_nr.txt
if exist %number_path% for /f "delims=" %%i in (%number_path%) do set /A subject_nr=%%i
echo Subject number = %subject_nr%.

rem Increase subject number by 1 for next participant
set /A next_subject = %subject_nr% + 1
echo %next_subject% > %number_path%

rem Run the experiment
echo Starting experiment...
C:\Users\40027000.ADS\Desktop\OpenSesame_2.8.0\opensesame_2.8.0-win32-2\opensesamerun C:\path\to\experiment\Part1.opensesame -s %subject_nr% -l C:\path\to\experiment\data\part1\subject_%subject_nr%.csv -f

echo Part 1 complete.
echo Starting part 2...

rem Goodbye message
echo Experiment complete!
echo Goodbye.

rem Pause for 2 seconds
ping 192.0.2.2 -n 1 -w 2000 > nul

Note how all the paths used are absolute (i.e. they being at the root of the C:\ drive). You can use relative paths, but it usually just causes confusion.

Launching from a Shortcut

To use this script, you have to first create the .bat file - the easiest way to do this is to paste it into Notepad, and save it with the extension .bat, rather than .txt, ususally in the same folder as your experiment. Next, you can right click on the desktop, create a shortcut, and set the target as your new .bat file (i.e. C:\path\to\experiemtn\RunExperiment.bat). You can also change the icon to something appropriate. On Windows Vista in our lab, this automatically opens up the contents of C:\Windows\System32\SHELL32.dll, which contains all the system icons, including a nice green 'play arrow'.

Fancier Options

Of course, while ugly, batch is still a programming language, and so there's plenty more you can do with it. One other possibility I've used in the past was to counterbalance conditions between participants - running one version of the experiment for half the participants, another for the rest. Of course, if the difference between the conditions is minor, it makes sense to implement the counterbalancing within OpenSesame.

rem Prevent unwanted output to the user.
@echo off

rem Get subject number from file
set number_path=C:\path\to\experiment\data\subject_nr.txt
if exist %number_path% for /f "delims=" %%i in (%number_path%) do set /A subject_nr=%%i
echo Subject number = %subject_nr%.

rem Increase subject number by 1 for next participant
set /A next_subject = %subject_nr% + 1
echo %next_subject% > %number_path%

rem Counterbalance
rem Get remainder of subject number / 3.
rem Note - quotes are important
set /a "remainder=subject_nr %% 3"

if "%remainder%" == "1" (set to_run=condition_A) 
if "%remainder%" == "2" (set to_run=condition_A) 
if "%remainder%" == "0" (set to_run=condition_C) 

rem Run the experiment
echo Starting experiment...
C:\Users\40027000.ADS\Desktop\OpenSesame_2.8.0\opensesame_2.8.0-win32-2\opensesamerun C:\path\to\experiment\%to_run%.opensesame -s %subject_nr% -l C:\path\to\experiment\data\part1\%to_run%_subject_%subject_nr%.csv -f
rem Note - In this example, the possible opensesame files are condition_A.opensesame, condition_B.opensesame, and condition_C.opensesame.

Comments