Tuesday, February 19, 2013

Search and Delete file from directory whose name in Regular Expression pattern

Search and Delete file from directory whose name in Regular Expression pattern

The below code deletes file whose name has "output" and ends with ".xls" from any directory.
Eg : output.xls
 123output123.xls
acvoutput.xls        etc.,





import
java.io.File;
import
java.io.IOException;
//import java.util.Locale;
import
javax.swing.JOptionPane;
import
jxl.*;
import
jxl.write.*;
import
jxl.write.Number;
import
jxl.write.biff.RowsExceededException;
import
jxl.read.biff.BiffException;
//import jxl.write.WritableSheet;
//import jxl.write.WritableWorkbook;
//import jxl.write.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
 
 
public
class Framework
{
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException
{
String sDir = JOptionPane.showInputDialog(
"Enter any Directory");
String sTemp;
String pattern =
"(.*)(output)(.*)(xls)";
int i;
//int iSize=Integer.parseInt(JOptionPane.showInputDialog("Enter array size"));
File oDir =
new File(sDir);
String[] aFiles = oDir.list();
for (i=0 ; i <= aFiles.length-1 ; i++ )
{
//sTemp=aFiles[i];
File oF=
new File(sDir+"\\"+(aFiles[i]));
if (cString_match(oF.getName(),pattern) == true ) //aArr[i]=JOptionPane.showInputDialog(i+1 +" ---Enter data");
{
if (oF.exists()== true )
{
oF.delete();
System.
out.print("File deleted ");
}
else
{
System.
out.print("file does not exist ");
}
}
}
 
JOptionPane.showMessageDialog(
null, "over");
}
public static String cCheck_File_Array(String aArr[] , String sPattern)
{
int i;
String sRes=
"";
//String spattern =sPattern; //"(.*)(output)(.*)(xls)";
Pattern oPattern = Pattern.compile(sPattern);
for (i=0 ; i <= aArr.length-1 ; i++ )
{
Matcher oMatcher = oPattern.matcher(aArr[i]);
if (oMatcher.find( )== true)
{
System.
out.println(aArr[i]+"--matches");
sRes=sRes+
","+i;
}
}
return sRes;
}
public static boolean cString_match(String sStr, String sPattern)
{
//String sRes="";
Pattern oPattern = Pattern.compile(sPattern);
Matcher oMatcher = oPattern.matcher(sStr);
if (oMatcher.find( )== true)
{
System.
out.println(sStr+"--matches");
return true;
}
else
{
System.
out.println(sStr +"--no matches");
return false;
}
}
}

'---------------------------------------------------------------------------------Formatted

import
java.io.File;

import
java.io.IOException;
//import java.util.Locale;
import
javax.swing.JOptionPane;
import
jxl.*;
import
jxl.write.*;
import
jxl.write.Number;
import
jxl.write.biff.RowsExceededException;
import
jxl.read.biff.BiffException;
//import jxl.write.WritableSheet;
//import jxl.write.WritableWorkbook;
//import jxl.write.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
 
 

public class Framework
{
public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException
{
String sDir = JOptionPane.showInputDialog("Enter any Directory");
String sTemp;
String pattern = "(.*)(output)(.*)(xls)";
int i;
//int iSize=Integer.parseInt(JOptionPane.showInputDialog("Enter array size"));
File oDir = new File(sDir);
String[] aFiles = oDir.list();
for (i=0 ; i <= aFiles.length-1 ; i++ )
{
//sTemp=aFiles[i];
File oF=new File(sDir+"\\"+(aFiles[i]));
if (cString_match(oF.getName(),pattern) == true ) //aArr[i]=JOptionPane.showInputDialog(i+1 +" ---Enter data");
{
if (oF.exists()== true )
{
oF.delete();
System.out.print("File deleted ");
}
else
{
System.out.print("file does not exist ");
}
}
}

JOptionPane.showMessageDialog(null, "over");
}
public static String cCheck_File_Array(String aArr[] , String sPattern)
{
int i;
String sRes="";
//String spattern =sPattern; //"(.*)(output)(.*)(xls)";
Pattern oPattern = Pattern.compile(sPattern);
for (i=0 ; i <= aArr.length-1 ; i++ )
{
Matcher oMatcher = oPattern.matcher(aArr[i]);
if (oMatcher.find( )== true)
{
System.out.println(aArr[i]+"--matches");
sRes=sRes+","+i;
}
}
return sRes;
}
public static boolean cString_match(String sStr, String sPattern)
{
//String sRes="";
Pattern oPattern = Pattern.compile(sPattern);
Matcher oMatcher = oPattern.matcher(sStr);
if (oMatcher.find( )== true)
{
System.out.println(sStr+"--matches");
return true;
}
else
{
System.out.println(sStr +"--no matches");
return false;
}
}
}

No comments:

Post a Comment