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
Solved

delete files in folder

Posted on 2004-05-01
9
1,900 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 47
Way to decrease size of apk file 9 86
Custom Java Application connects to database: Which one of the 20? 4 35
mysql jsp example issue 32 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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