In javascript how to delete files in a directory older than x days
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sreekanth_nagabhyru
Posted On:   Tuesday, July 8, 2008 02:36 AM


I need to delete files in a particular directory which are created older than 5 days on login.


I have the path of the directory...using that can we get the files based on created date ?? and how to delete those multiple files using javascript

Re: In javascript how to delete files in a directory older than x days

Posted By:   Anonymous  
Posted On:   Thursday, July 10, 2008 11:52 AM

You can't use javascript directly. As one member has already posted, you can use an ActiveXControl or something tightly integrated to the OS. HOWEVER, if you are writing a web app, a different solution would be to use Java's IO libraries in a servlet and invoke it using an AJAX call.


Just MHO.

Re: In javascript how to delete files in a directory older than x days

Posted By:   sreekanth_nagabhyru  
Posted On:   Wednesday, July 9, 2008 03:00 AM

Hi guys...here is what i have done and it worked for me...hope it wud be helpful for some of u


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();

//based on ur requirement it can be DateLastModified / DateLastAccessed in the below line

if(f.DateLastAccessed < dBefore ){

//alert("Deleting "+f.Name+" last accessed on: "+f.DateLastAccessed);

f.Delete(true);

}

}


}



PruneFiles("C:/xyz/",5);

About | Sitemap | Contact