Solved

Delete Files Using Javascript

Posted on 2007-04-04
3
15,289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

728 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