Solved

Delete Files Using Javascript

Posted on 2007-04-04
3
15,245 Views
Last Modified: 2008-08-24
How to list the contents of a directory in javascript and delete files older than x days...

The code below is vbs but I need javascript.
Thanks

Function PruneFiles (FolderPath)
      on error resume next
      Dim oFSO
      Dim sDirectoryPath
      Dim oFolder
      Dim oFileCollection
      Dim oFile
      
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      sDirectoryPath = FolderPath
      set oFolder = oFSO.GetFolder(sDirectoryPath)
      set oFileCollection = oFolder.Files
      
      For each oFile in oFileCollection
            If oFile.DateCreated < Now() Then
                  oFile.Delete(True)
            End If
      Next
      
      'Clean up
      Set oFSO = Nothing
      Set oFolder = Nothing
      Set oFileCollection = Nothing
      Set oFile = Nothing
End Function
0
Comment
Question by:NTGuru705
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
Comment Utility
Here an example:





function PruneFiles(FolderPath, xDays) {
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var oDir = fso.GetFolder(FolderPath);
  var eFiles = new Enumerator(oDir.Files);
  var dBefore = new Date();
  dBefore.setDate(dBefore.getDate()-xDays);
  eFiles.moveFirst();
  for(;!eFiles.atEnd(); eFiles.moveNext()){
     var f = eFiles.item();
     if(f.DateLastAccessed < dBefore ){
        //WScript.Echo("Deleting "+f.Name+"  last accessed on: "+f.DateLastAccessed);
        f.Delete(true);
     }
  }

}

PruneFiles("D:/NTGuru705/",21);





0
 
LVL 1

Author Comment

by:NTGuru705
Comment Utility
You rock...
Being a vbscript guy the syntax is a killer on javascript.
Thank you for your help
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
You are welcome.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now