Solved

Delete Files Using Javascript

Posted on 2007-04-04
3
15,270 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
ID: 18854380
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
ID: 18854973
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
ID: 18876967
You are welcome.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…
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…

777 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