5 Replies Latest reply on Jun 1, 2018 7:09 AM by Antony Mooe

    Auto data merge all csv files in folder

    Antony Mooe Level 1

      Hello,

       

      I have a folder with 10 csv files and an Indesign template file.

       

      I need to:

       

      1. open Indesign template file

      2. get all csv files in folder to indesign

      3. merge and record all csv

      4. save indesign files with the cvs names

       

      Is that possible to do?

       

      Thank you Guys!

        • 1. Re: Auto data merge all csv files in folder
          davidc19923048 Level 1

          I think so:

           

          1. open Indesign template file

          var file = new File(templateFolder + "template.indt");
          app.open(file);
          

           

          2. get all csv files in folder to indesign

          for (var i = 0; i < 10; i++) {
               csvName = "csv"+i+".csv";
               csvFile = new File(csvPath + csvName);
          }
          

          3. merge and record all csv

          I didn't understand this

           

          4. save indesign files with the cvs names

          app.activeDocument.save(csvName);
          
          • 2. Re: Auto data merge all csv files in folder
            Antony Mooe Level 1

            Hello davidc19923048,

             

            I'm sorry, but the script it's not working.

            I get the error: "templateFolder is undefined"

             

            In the step 3 you didn't understood, I need to create merged documents.

             

            Thank you very much!

            • 3. Re: Auto data merge all csv files in folder
              Laubender Adobe Community Professional & MVP

              Hi Antony,

              you have to understand that you posted in the Adobe InDesign Scripting Forum where ( mostly ) scripters hang around.

              A snippet like the one by David is showing you just the way to write your own code or is just showing a principle of coding. It seldomly is meant to be a full script that is working in every case, in every occassion on data David cannot see into.

               

              About the error message: Of course it says "templateFolder is undefined.".

              It's your job to define the variable templateFolder so that it is a valid folder path to your data.

              David obviously cannot know where your data is.

               

              Regards,
              Uwe

              • 4. Re: Auto data merge all csv files in folder
                Antony Mooe Level 1

                Thank you Guys for your reply!

                 

                I'm a scripter beginner, but in the past few weeks i've been struggling to get the code right.

                I came out with this code witch It does almost what I need:

                 

                1. open Indesign template file

                2. get the data source file "Example_01.txt" from the folder "_txt" to indesign

                3. merge data

                4. save an indesign file with the name "File9.indd"

                 

                main () 
                function main () {      
                    // create a path for the template files   
                    var Templates = File("~/Desktop/_templates/Template_01.indt"); 
                    
                    // open the template  
                    app.open(Templates); 
                    
                    // create a path for a Indesign file    
                    var dataSource = Folder ("~/Desktop/_txt/Example_01.txt");  
                    
                    // merge and record data    
                    app.activeDocument.dataMergeProperties.selectDataSource(dataSource); 
                    app.activeDocument.dataMergeProperties.mergeRecords(); 
                    
                    // create a path for the new indesign file    
                    var folderOut = Folder ("~/Desktop/_folderOut/");  
                    
                    for (var i = 0; i < 10; i++) {  
                    inddName = "File"+i+".indd";  
                    inddFile = Folder (folderOut + new File (inddName));  }      
                }  
                    //save the file  
                    app.activeDocument.save(inddFile); 
                    
                    //close the file  
                    app.activeDocument.close();  
                

                 

                 

                 

                But what I really need is:

                 

                1. open Indesign template file - OK

                2. get all "txt" files  from the folder "_txt" to indesign (I've got 15 "txt" files)

                3. merge data - OK

                4. save individual indesign files with the original names of each "txt" files

                 

                 

                Can anyone help me?

                 

                Best regards,

                • 5. Re: Auto data merge all csv files in folder
                  Antony Mooe Level 1

                  Hello,

                   

                  I finally came out with this code that works perfectly to me:

                   

                   

                  // templateFile path
                  var myTemplate = File ("~/Desktop/_templates/Template_01.indt");
                  app.open(myTemplate);  
                  
                  
                  // OutputFolder path
                  var myOutputfolder = Folder ("~/Desktop/_folderOut/");  
                  
                  
                  // TxtFolder path
                  var myTxtFolder = Folder ( "~/Desktop/_txt/" );
                      
                      if ( myTxtFolder != null ) {
                      var myTxtFiles = [];
                      var myAllTxtFilesList = myTxtFolder.getFiles();
                  
                  
                              for (var f = 0; f < myAllTxtFilesList.length; f++) { 
                              var myFile = myAllTxtFilesList[f];
                              if (myFile instanceof File && myFile.name.match(/\.txt$/i)) { 
                                  myTxtFiles.push(myFile);
                              
                              // merge and record data    
                              app.activeDocument.dataMergeProperties.selectDataSource(myFile); 
                              app.activeDocument.dataMergeProperties.mergeRecords();
                              
                      // set the OutputFolder and fileExtension  to ".indd" 
                      var mySavedDocument = Folder (myOutputfolder + new File (myFile.name.replace(/\.txt$/i, ".indd"))); 
                                
                      // save the file  
                      app.activeDocument.save(mySavedDocument);
                  
                  
                      // close the file  
                      app.activeDocument.close();
                    }
                    }
                  }
                  

                   

                   

                  Thank you guys!

                  Best regards!