Solved

Wildcard Deletion throughJava out-of-process

Posted on 2002-03-04
6
188 Views
Last Modified: 2010-03-31
I'm running JDK 1.3 under Unix environment.  If I want to delete all .pdf files in a directory, I can do so on the command line by just typing "rm *.pdf".  But I'm having problems doing this through Java, such as the following:

try
      {
          String commandString = "rm *.pdf";
          Process p = Runtime.getRuntime().exec(commandString);
          p.waitFor();
          p.destroy();
          System.out.println("done");
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }

If I specify a file exactly (like a.pdf), it does delete that specific file.  But if I use the wildcard (*), nothing happens.  How can I delete using wildcard?  Thanks in advance.
0
Comment
Question by:jove
6 Comments
 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 100 total points
Comment Utility
This should work.  The structure of Wildcard filter may be modified to parse wildcards and such, but this is the general idea....:

File f = new File(strReadDir);
               String[] aList = f.list(new WildcardFilter("pdf"));

for (int i = 0; i < aList.length; i++)
     aList[i].delete();

class WildcardFilter implements FilenameFilter {
     private String m_strFileSuffix = null;
         
     public XsltFilter(String strFileSuffix) {
          m_strFileNameSuffix = strFileSuffix;
     }
         
     public boolean accept (File f, String strFileName) {
          return strFileName.toLowerCase().endsWith(strFileSuffix);
     }
}
0
 
LVL 3

Expert Comment

by:saxaboo
Comment Utility
Hi,

I gather this comes from the fact you need a shell to interpret the wildcards.

Had you been in an NT environment, with a "del *.pdf" command, I would have suggested to replace it by "start del *.pdf"

I'm sure you can do something similar under Unix, by invoking bash or tcsh and passing it your command-line on the standard input, but I'm not sure of the syntax and haven't got any Unix prompt anywhere next to me. A heavier way to do this would be to have a shell script delpdf.sh containing your command, and to call "sh delpdf.sh"

Hope this helps,

-S
0
 
LVL 3

Expert Comment

by:saxaboo
Comment Utility
Oops, concurrent answer !

Sorry m_onkey_boy for my first line about wildcards ;-)
0
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

 
LVL 5

Expert Comment

by:kelfink
Comment Utility
On UNIX, it's the shell process which performs the wildcard.  Calling 'rm' directly bypasses this.

MonkeyBoy's suggestion is good for rolling your own wildcard implementation.  

If your goal is to pass commands to Unix, you have to get the Shell to process the arguments.

Example to modify your original post:

         String[] cmd = { "/bin/sh", "-c", "rm *.pdf" };
         Process p = Runtime.getRuntime().exec(cmd);

works.



0
 
LVL 4

Expert Comment

by:m_onkey_boy
Comment Utility
Another note:  My solution will take a bit longer to write code-wise, but it has the advantages of being platform-independant and faster that calling exec().

The main drawback is that you have to write your own parsing routines that are already in place for you by the OS.
0
 

Author Comment

by:jove
Comment Utility
Thanks much.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now