Solved

delete files in folder

Posted on 2004-05-01
9
1,897 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 32

Accepted Solution

by:
ldbkutty earned 50 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
0
 
LVL 7

Expert Comment

by:maheshexp
Comment Utility
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
Comment Utility
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
Comment Utility
thanks :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now