?
Solved

Delete Files Using Javascript

Posted on 2007-04-04
3
Medium Priority
?
15,293 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

762 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