Solved

Using MS-DOS Commands Within Java App to Create Files

Posted on 2013-01-23
9
463 Views
Last Modified: 2013-01-23
I am using the Runtime.getRuntime().exec() method to run MS-DOS commands to, for example, create files.  One example is to execute “dir *abc* /s /b > abc.txt” to create a text file containing the full path names of every file whose filename contains the characters “abc”.

After the successful execution of that command, the Java app does not know that the new file, abc.txt, exists.

Is there something else that must be done to let Java know that this file exists?
0
Comment
Question by:david_m_jacobson
[X]
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
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 38811100
You need to make Java see the new file after the exec() returns.

Should be as easy as :

File f = new File("abc.txt");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38811265
a. you would need to execute a shell to use stream redirection successfully (i.e. your command must begin with cmd.exe)
b. you don't need to do that to produce a file of paths - Java already has the power to do that without invoking the OS
0
 

Author Comment

by:david_m_jacobson
ID: 38812135
Thank you for that suggestion, but it doesn't work for me.  Here is a snippet of code I am using:

commandLine = "cmd /c dir *\"" + param[0] + "\"* /s /b > \"" + input_filename + "\"";
System.out.println(commandLine);
process = Runtime.getRuntime().exec(commandLine, null, pictures_directoryName_File);
File aFile = new File(input_filename);
if (aFile.exists())
      System.out.println(input_filename + " exists");
else
      System.out.println(input_filename + " does NOT exist");
System.exit(0);

The output looks as follows:

cmd /c dir *"abc"* /s /b > "C:\Users\my_user_name\Desktop\Temporary\abc.txt"
C:\Users\my_user_name\Desktop\Temporary\abc.txt does NOT exist

So clearly, Java doesn't know that 'abc.txt' exists; yet it does exist.

Any other suggestions?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38812176
If you're determined to do it that way, you need to do it right. See
http://technojeeves.com/joomla/index.php/free/52-runtimeexec
0
 

Author Comment

by:david_m_jacobson
ID: 38812364
Thank you very much.  Reading through the suggested "theory" link brought my attention of the 'waitFor()' method of the Process class, and appears to have solved my problem.

Thank you very much!!
0
 

Author Comment

by:david_m_jacobson
ID: 38812387
I've requested that this question be closed as follows:

Accepted answer: 0 points for david_m_jacobson's comment #a38812364

for the following reason:

I was not rating my own comment here, but rather the Expert's previous comment.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38812388
I think you might have closed the question in a way you didn't intend ...
0
 

Author Comment

by:david_m_jacobson
ID: 38812723
You are right.  I am about to correct that.  Thank you again.
0
 

Author Closing Comment

by:david_m_jacobson
ID: 38812725
Thank you very much.
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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

728 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