• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15313
  • Last Modified:

Delete Files Using Javascript

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.

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
            End If
      'Clean up
      Set oFSO = Nothing
      Set oFolder = Nothing
      Set oFileCollection = Nothing
      Set oFile = Nothing
End Function
  • 2
1 Solution
ZvonkoSystems architectCommented:
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();
  for(;!eFiles.atEnd(); eFiles.moveNext()){
     var f = eFiles.item();
     if(f.DateLastAccessed < dBefore ){
        //WScript.Echo("Deleting "+f.Name+"  last accessed on: "+f.DateLastAccessed);



NTGuru705Author Commented:
You rock...
Being a vbscript guy the syntax is a killer on javascript.
Thank you for your help
ZvonkoSystems architectCommented:
You are welcome.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now