Copy link to clipboard
Copied
I am able to create a text file log and append to it (code below). I am writing file names to it as I save them. If for some reason I need to resave the file, I don't want to duplicate the entry in the log.
Can the ammend process include checking existing entries in the log and only appending only if it doesn't exist. I'm only expecting a maximum of 1000 file names per log. The script is called by another script so it will have to evaluate the entries each time it runs before appending. I'm guessing Photoshop would have to load existing linesinto an array and compare the test to be written.
I'm on OS X Catalina 10.15.7 using CC 2021 and 2022
Could someone please help with this code?
Cheers!
Limey
//Write Log File
var fname = fileName;
var a = new File(logsFolder + "/processed_" + todaysDate + ".txt");
a.open('a');
a.write(fname + "\n");
a.close();
Copy link to clipboard
Copied
File('~/desktop/someFile.txt').exists
Copy link to clipboard
Copied
Thanks for the speedy response Kukurykus! That looks as if it's checking if the file exists, not the entry within the file. What I need is to check the contents of the file to prevent a duplicate entry if the filename already exists.
Thanks,
Copy link to clipboard
Copied
File('~/desktop/someFile.txt').length
Copy link to clipboard
Copied
Wouldn't this give the number of entries (lines) in the file?
Copy link to clipboard
Copied
You must read the content of file to take it to variable. Then check one of lines that should be same as that you want to append by something like content.split(newLne).length. If the length is greater than 1 then you know same line was already used.
Copy link to clipboard
Copied
I'm just shooting from the hip here...
You will have the current filename in a variable. You may or may not need to convert this to a string.
Then you will read in the log file and perform a .test(); or a .match(); or other regular expression string comparison with a conditional to either write the filename or not depending on whether there is a match.
I have not written the code, so from experience, there will be at least 10 other things that I didn't think about that will need to added to make this work as expected.
Copy link to clipboard
Copied
Thanks Stephen, That's what I figured. The file contents will need to be read in as some sort of array and then compared against fName. I wonder if I can just change the file extension when creating the log file to .csv?
var fileName = decodeURI(doc.name);
I wonder if I can just change the file extension when creating the log file to .csv? I do have the following code I used to read from an Excel doc saved as a CSV file. This first opens a dialog to chose a csv file. I'd replace it with a hardcoded path.
//Open dialog to locate CSV file
var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
datafile = new File(csvFile);
//Open dialog to provide location of files to import
var searchFolder = Folder.selectDialog ("Select folder to search in")
var csvString = [];
if (datafile.exists) {
//open CSV file into memory
datafile.open('r') ;
// Skip first line
datafile.readln();
// read one line at a time until end of file
while(!datafile.eof) {
csvString.push( datafile.readln().replace(/^\s+|\s+$/g, '') );
}
datafile.close(); //Close CSV after reading into memory
}
The thing then would be to make the comparison and IF the entry does not exist, append fName to the file.
I am learning to think, I am just behind on the doing! I'm learning though. 😉
Copy link to clipboard
Copied
I don't have time now for the .test(); or .match(); so I’ll presume that you have this covered, as well as the conditional write based on the result of the test/match etc.
Here is a screenshot of a simplified read process, presuming that the content is line by line I don't know if you need to turn it into an array?
Edit: Using a .toSource() on the alert shows that it is a string with \n breaks, which may need to be factored into the regular expression.
Copy link to clipboard
Copied
Thanks Stephen! This is very helpful and much simplified. I believe test() works with a string. I'll play with it and create a condition to append if false.