• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Append to Text File (Log) - Unique entries only

Participant ,
Mar 05, 2022 Mar 05, 2022

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();  

 

TOPICS
Actions and scripting , macOS

Views

363

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
LEGEND ,
Mar 05, 2022 Mar 05, 2022

Copy link to clipboard

Copied

File('~/desktop/someFile.txt').exists

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 05, 2022 Mar 05, 2022

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.

PSGuru_0-1646524828988.png

 

Thanks,

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 05, 2022 Mar 05, 2022

Copy link to clipboard

Copied

File('~/desktop/someFile.txt').length

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 05, 2022 Mar 05, 2022

Copy link to clipboard

Copied

Wouldn't this give the number of entries (lines) in the file?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 05, 2022 Mar 05, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2022 Mar 05, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 05, 2022 Mar 05, 2022

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.  😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2022 Mar 05, 2022

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?

 

logger.png

 

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.

 

toSource.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 05, 2022 Mar 05, 2022

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines