Solved

Using MS-DOS Commands Within Java App to Create Files

Posted on 2013-01-23
9
419 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
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:david_m_jacobson
Comment Utility
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
Comment Utility
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
Comment Utility
I think you might have closed the question in a way you didn't intend ...
0
 

Author Comment

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

Author Closing Comment

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

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

12 Experts available now in Live!

Get 1:1 Help Now