5 Replies Latest reply on Sep 19, 2015 12:35 AM by Johnwhite

    How to get file names

    Johnwhite Level 1

      Hi experts

       

      How get some pdf names and path, which put in some folders.

       

      I got this script for get subfolder names,

      but now I want to change function for get the name of PDF files.

       

      My target is those PDF file names.

       

      ================================================================================

      var myFolder=Folder("Y:/Announcement/2015/2015-12 (HKEX Adv)/");  

      var myFolderPath =myFolder.path;  

      var refCountSep = myFolderPath.split("/").length  

      var subFolderList = new Array;  

      var logFile = new File("~/Desktop/opt15/Subfolders titles/HKEx_ann12.txt");  

       

      getSubFolders(myFolder);  

       

      function getSubFolders(theFolder) {  

           var elementList = theFolder.getFiles();  

           for (var i= 0; i< elementList.length;i++) {  

                var aElement = elementList[i];  

                if (aElement instanceof Folder){  

                     subFolderList.push(aElement.displayName);  

                     countSep = aElement.path.split("/").length  

                     tabCount = countSep - refCountSep  

                     myIndent = Array(tabCount).join(';')  

                     writeLogFile(myIndent + aElement.displayName)  

                     getSubFolders(aElement); // recursive function  

                }  

           }  

          //$.writeln (subFolderList)  

      }  

       

      function writeLogFile(theString) {  

          logFile.open( "a" );  

          logFile.writeln(theString);  

          logFile.close();     

      }

      ================================================================================

       

      thanks

       

      Regard

      John

        • 1. Re: How to get file names
          Chinnadk Level 4

          Try this,

           

          var folder = Folder.selectDialog("Select a folder");
          var pdffiles = folder.getFiles("*.pdf");
          var filenames = "";
          for(var i=0;i<pdffiles.length;i++)
          {
                  filenames += pdffiles[i].name + "\r";
              }
          var txtfile = new File(folder.fullName + "/logfile.txt");
          txtfile.open("w");
          txtfile.write(filenames);
          txtfile.close();
          txtfile.execute();
          

           

          Regards,

          Chinna

          • 2. Re: How to get file names
            Johnwhite Level 1

            Thank you Chinna

            thank you for your help

             

            but, I change it like this:

             

            ===========================================

            var myFolder=Folder("Y:/Announcement/2015/2015-12 (HKEX Adv)/"); 

            var pdffiles = folder.getFiles("*.pdf"); 

            var filenames = ""; 

            for(var i=0;i<pdffiles.length;i++) 

                    filenames += pdffiles[i].name + "\r"; 

                } 

            var txtfile = new File(folder.fullName + "/logfile.txt"); 

            txtfile.open("w"); 

            txtfile.write(filenames); 

            txtfile.close(); 

            txtfile.execute(); 

            ==============================================

             

            assume all those pdfs put in some subfolders

             

            what I need is get those file names and subfolders names

             

            like:

             

            subfolder-xxxx.pdf

             

            Could you please help me to change?

             

            Regard

            John

            • 3. Re: How to get file names
              Chinnadk Level 4

              Try now,

               

              var myFolder=Folder("Y:/Announcement/2015/2015-12 (HKEX Adv)/"); 
              var _files = [];
              getFiles(myFolder);
              var filenames = ""; 
              for(var i=0;i<_files.length;i++) 
              {
                      if(_files[i].name.indexOf(".pdf")!=-1 || _files[i].name.indexOf(".PDF")!=-1)
                      {
                              filenames += _files[i].parent.name + "-"+_files[i].name + "\r";
                          }
                  } 
              var txtfile = new File(myFolder.fullName + "/logfile.txt"); 
              txtfile.open("w"); 
              txtfile.write(filenames); 
              txtfile.close(); 
              txtfile.execute(); 
              
              
              function getFiles(folder)
              {
                      var filelist = folder.getFiles();
                      for(var i =0;i<filelist.length;i++)
                      {
                              if(filelist[i] instanceof Folder)
                              {
                                      getFiles(filelist[i]);
                                  }
                              else if(filelist[i] instanceof File)
                              {
                                      _files.push(filelist[i]);
                                  }
                          }
                  }
              

               

              Regards,

              Chinna

              • 4. Re: How to get file names
                Johnwhite Level 1

                great!

                 

                Thank you Chinna

                thank you so much!

                 

                John

                • 5. Re: How to get file names
                  Johnwhite Level 1

                  Hi Chinna

                   

                  Could this script save a file in a specific location and not open it?

                   

                  like:

                   

                  var logFile = new File("~/Desktop/opt15/Subfolders titles/HKEx_ann12.txt"); 

                   

                  How can make it.

                   

                  Regard

                  John