Solved

delete files in folder

Posted on 2004-05-01
9
1,898 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unexpected HttpURLConnection connection behavior 2 86
compre toata in where clue oracle 4 56
java 8 lambda expresssions exception handling 3 75
ForLoop Example 3 40
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 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

14 Experts available now in Live!

Get 1:1 Help Now