Skip to main content
Stephen Marsh
Community Expert
Community Expert
October 11, 2023
Answered

Script to Open Multiple Docs from a Text File

  • October 11, 2023
  • 3 replies
  • 424 views

I'm having problems scripting the opening of multiple files from a text file source.

 

Example text file:

 

~/Desktop/files.log

 

~/Desktop/theFolder/theFile001.jpg
~/Desktop/theFolder/theFile002.jpg

 

There could be 1, 2 or more items to loop over in the log file.

 

 The following code opens the first file:

 

var logFile = File("~/Desktop/files.log");
logFile.open('r');
var filePath = logFile.readln();
logFile.close();
alert(filePath);
open(File(filePath));

 

However, I am having problems making it loop over each separate line in the log file to open all files (I have tried many different things without success). Can somebody please help me to open each line in the log file as a separate file? I would have thought that this would be a "common enough" request, however, I couldn't find anything that I could use.

 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Does this help? 

var theResult = readPref ("~/Desktop/aaa.log");
for (var m = 0; m < theResult.length; m++) {
    alert (theResult[m])
};
////// read prefs file //////
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return theText.split("\n")
//return String(theText)
}
};

3 replies

Legend
November 22, 2023

I would write the log file into an array, then close the file and loop through the array. That would let you go from either end or an arbitrary line, search for a specific file, close and then re-open files, etc.

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
October 11, 2023

Does this help? 

var theResult = readPref ("~/Desktop/aaa.log");
for (var m = 0; m < theResult.length; m++) {
    alert (theResult[m])
};
////// read prefs file //////
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return theText.split("\n")
//return String(theText)
}
};
Stephen Marsh
Community Expert
Community Expert
October 11, 2023

@c.pfaffenbichler 

 

Thanks, I have attached the log file, it is just a text file with UTF-8 encoding with Unix line feeds. The script that creates the log file would conditionally write Windows LF if that was the platform running the script. 

 

Your script returns each file path correctly via the alert:

 

alert (theResult[m])

 

It was then just a simple case of swapping the alert for the open:

 

open(File(theResult[m]));

 

Thank you!

c.pfaffenbichler
Community Expert
Community Expert
October 11, 2023

Can you provide the log-file?