Introduction to VivoScript – Part II: Writing meta information to a local file

Introduction to VivoScript – Part II: Writing meta information to a local file

In this section we are having a look at how to extract meta information from studies in a project and writing them to a local text file. The looping through all studies of a project is described in Part I of this introduction.

// Write study information to a file:

var outFile = "report.txt";
VQ.writeFile(outFile, "Report\n"); // Creates new (empty) file
// Don't forget to add NewLine \n (not required for VQ.debug())

var dm = VQ.dataManager();

var dcmRepUrl   = 'ipacss://vqintro:blog42@training.ipacs.invicro.com';
var projectName = '/examples';

var dcmRep = VQ.dcmRep( dcmRepUrl );
dcmRep.setProject( projectName );

var studies = VQ.queryStudies(dcmRep, "Mouse*"); // search for patient "Mouse*"
for (var i=0; i<studies.length; ++i) {
    VQ.appendFile(outFile, "patient["+i+"]: " +studies[i].PatientsName+"\n");    // print meta-info
    VQ.appendFile(outFile, "  studyuid["+i+"]: "+studies[i].StudyDescription+"\n");

    var series = VQ.querySeries(dcmRep, studies[i].StudyInstanceUID);   // fetch series by study id
    for (var j=0; j<series.length; ++j) {
        VQ.appendFile(outFile, "    series["+j+"]: "+series[j].SeriesDescription + ", "+series[j].Modality+"\n");
    }
}

VQ.showMessage("Written report to: "+outFile);

The new part here are the VQ.writeFile(…) and VQ.appendFile(…) function:

VQ.writeFile(outFile, "Report\n"); // Creates new (empty) file
...
VQ.appendFile(outFile, "patient["+i+"]: " +studies[i].PatientsName+"\n");    // print meta-info

With VQ.writeFile(…) a new file is created (or an existing file overwritten). The first parameter outFile is the filename, followed by the text. Don’t forget to add newlines (\n) on the way. To add additional text to a file use VQ.appendFile(…) with the same calling syntax.

Instead of writing out text line by line (or piece by piece) you could also first gather all text in a string and then use a single VQ.writeFile(…) to commit it to a file:

var text = '';
for (...) {
  text += ...;
}
VQ.writeFile(outFile, text);

As always, please feel free to leave comments or questions below.