Solved

delete files in folder

Posted on 2004-05-01
9
1,902 Views
Last Modified: 2009-12-16
How can I delete all files of a certain extension (.doc) that are in a folder?
0
Comment
Question by:OsdNA
[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
  • 2
  • 2
  • +2
9 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 10969466
Hi OsdNA,

Unless you specify your OS and/or your programming language I suggest:

Windows/Dos: del *.doc
UNIX: rm *.doc

=====
Werner
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 10969493
To Delete files in JAVA:-
===============

final String BASE = "c:\\temp";
        File dir = new File(BASE);
        File[] files = dir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().toLowerCase().endsWith(".doc");
            }
        });
        for (int i = 0; i < files.length; i++) {
          String name = files[i].getName();
          boolean deleted= files[i].delete();
          if(deleted)
            out.println("Deleted " + name);
          else
            out.println("Problem deleting " + name);
        }

Best Regards.
0
 

Author Comment

by:OsdNA
ID: 10969725
I meant to ask this in java programming, thanks for the replys, it is java I wanted to do it in :).
Would this work?:

final String BASE = "c:\\temp";
        File dir = new File(BASE);
        File[] files = dir.listFiles();
           
        for (int i = 0; i < files.length; i++) {
          String name = files[i].getName();
          if(name.endswith("c"){
          boolean deleted= files[i].delete();
}
          if(deleted)
            out.println("Deleted " + name);
          else
            out.println("Problem deleting " + name);
        }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Accepted Solution

by:
ldbkutty earned 50 total points
ID: 10969805
Yep...Just 2 comments:

1) Replace          
>> if(name.endswith("c") as
if(name.endsWith("c") //Replacing by Caps.

The best option to delete all *.doc files is to give  if(name.endsWith(".doc").

2) to delete  '*.DOC' , as well as '*.doc' ,

instead of  >> String name = files[i].getName();
use,
String name = files[i].getName().toLowerCase(); // as said in my previous post.

All the Best.
         
         
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970333
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10970356
File f = new File("path here");
            
            f.listFiles(new FilenameFilter() {
                  public boolean accept(File dir, String name) {
                        if(name.toLowerCase().endsWith("doc")){
                              File f1 = new File(dir.toString() + "/" + name);
                              f1.delete();
                        }
                        return true;
                  }
            });
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10970680
Assume the .doc files are in c:\temp directory.

For win nt/2k/xp use

    Runtime.getRuntime().exec("cmd del c:\\temp\\*.doc");

For win 95/98/me

        Runtime.getRuntime().exec("command del c:\\temp\\*.doc");  

For linux/unix

            Runtime.getRuntime().exec("rm -f \\root\temp\\*.doc"); // not sure
0
 

Author Comment

by:OsdNA
ID: 10971612
thanks :)
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

734 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