Friday, November 2, 2012

Delete files from a folder


/*
 * LoadRunner Java script. (Build: 946)
 *
 * Script Description:
 *                    
 */


import lrapi.lr;
import java.io.*;


public class Actions
{
public int flagFile = 0;


public int init() throws Throwable {
return 0;
}//end of init


public int action() throws Throwable {

   String dirPath="D:\\OrderIDs\\Copyof20090407\\"; //change the Directory accordingly
   String ext = "log";
//    File dir_path = new File(dirPath);
//      File[] fileList = dir_path.listFiles();

   File[] fileList = new File(dirPath).listFiles();


   for(int j = 0; j < fileList.length; j++)
   {
   try
   {
   if(fileList[j].getName().endsWith(ext)){
//System.out.println("fileList[j] : " + fileList[j]);
String fileName = fileList[j] + "";
//System.out.println("fileName : "+ fileName);
File f1 = new File(fileName);
boolean success = f1.delete();

if (!success){
 System.out.println("Deletion failed.");
 //System.exit(0);
}else{
 System.out.println(fileName + " - File deleted.");
}
//System.exit(0);
   }
   if (!fileList[j].getName().endsWith("log")) {
flagFile = 1;  
}

   }
   catch(Exception e)
   {
   System.out.println("Error deleting file." + fileList[j]);
   continue;
   }
   }
   if (flagFile == 1) {
   System.out.println("No " + ext + " file exists");  
   }
   
 
return 0;
}//end of action


public int end() throws Throwable {
return 0;
}//end of end
}

No comments:

Post a Comment