Results 1 to 7 of 7
- 10-11-2011, 08:30 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
How to delete files, based on a matching pattern in the filename in a directory.
Hi
Anyone pls tell me how to delete files for example having
files names like:-
12098_All_School_Reports.csv
12367_All_School_Reports.csv
:
:
I need to look for files having the pattern
_All_School_Reports.csv and delete only those files from a directory.
How to do that in java...
Anyhelp is appreciated...
Thank You
-
Re: How to delete files, based on a matching pattern in the filename in a directory.
A for loop that utilizes the String#contains(...) method should work nicely I think.
- 10-11-2011, 08:48 PM #3
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
Re: How to delete files, based on a matching pattern in the filename in a directory.
Sir
i dont get Contains method.
String deleteThisFile = "All_School_Reports.csv";
Files[] files = location.listFiles();
for (File file : files) {
if(file.equals
Can you pls send the code .
Thank You.
- 10-11-2011, 08:56 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 119
- Rep Power
- 0
Re: How to delete files, based on a matching pattern in the filename in a directory.
File[] files = location.listFiles();
for (File file : files)
{
if(file.toString().contains(deleteThisFile)) {
file.delete();
}
// // Delete each file
// if (!file.delete())
// {
// // Failed to delete file
// System.out.println("Failed to delete "+file);
// }
}
Thank You Sir, i got this to work.Thanks You.
- 10-11-2011, 08:56 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 81
- Rep Power
- 0
Re: How to delete files, based on a matching pattern in the filename in a directory.
Don't use file.equals. What I would do is have a String variable called ending with a value of "_All_School_Reports.csv", and, inside the for loop:
Java Code:if(file.length()>=ending.length()){ if(file.substring(file.length()-ending.length()).equals(ending)){ //whatever you want to do with the file } }
- 10-11-2011, 10:11 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 10-11-2011, 10:48 PM #7
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Re: How to delete files, based on a matching pattern in the filename in a directory.
Just as an observer trying to figure this out...
So if I had this: 12098_All_School_Reports.csv =28 in length, and ending being this: _All_School_Reports.csv = 23 in length
Performing the first if would result in 28>=23 true, so then the second if would produce _All_School_Reports.csv.equals(ending)...which is true.Last edited by kraigballa; 10-11-2011 at 11:24 PM.
Similar Threads
-
A Question Regarding Pattern Matching
By hadi in forum Advanced JavaReplies: 4Last Post: 01-21-2011, 04:38 PM -
pattern matching with two arrays
By jessie in forum New To JavaReplies: 10Last Post: 11-24-2010, 05:00 PM -
String matching a pattern
By vividcooper in forum New To JavaReplies: 7Last Post: 01-07-2010, 11:30 PM -
Reading a directory and getting the filename
By mrjunsy in forum Advanced JavaReplies: 1Last Post: 05-10-2008, 03:36 AM
Bookmarks