Solved

delete files in folder

Posted on 2004-05-01
9
1,899 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 116
Java Jpanels and Jframe 8 33
Weekend adv creator 3 39
Java: How do I open the default windows program for a given file type 3 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

821 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