Copy link to clipboard
Copied
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.
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)
}
};
Copy link to clipboard
Copied
Can you provide the log-file?
Copy link to clipboard
Copied
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)
}
};
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Sorry to bother you with this again... I can't figure out how to reverse the order of the opened files, can you please help?
I tried to reverse the loop:
for (var m = theResult.length - 1; m >= 0; m--) {
And also tried:
theString.split('\n').reverse().join('\n')
The other alternative is to write to the log file in reverse order, but I am having different issues with that!
EDIT:
OK, I worked out how to write to the log file in the required order, so no pressing need to read in reverse order... but it would be good to know for the sake of completeness.
Copy link to clipboard
Copied
The line you posted seems to work here.
var theResult = readPref ("~/Desktop/example-files.log");
for (var m = theResult.length-1; m >= 0; 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)
}
};
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now