954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to write output to a file from cmd?

Hi,

I am trying to write the output generated by a simple command "dir" to a file. Can you please help me do that?

Here is what I am doing --

system ("dir");

this will run dir command on my D: drive now, I want to write this ouput (in short whatever is displayed on cmd) to file. How can I do that?

Paryushan
Newbie Poster
9 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

you mean you want to save the information displayed on your
command prompt to an msword or notepad file?

Frank Wallis
Posting Whiz in Training
217 posts since Jul 2008
Reputation Points: 10
Solved Threads: 15
 

yes

Paryushan
Newbie Poster
9 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

Next time I would prefer to see some effort to solve your programming reqirements before posting code. Here is one way it can be done:

my $dir = qx{dir};
open (my $OUT, ">", 'dir.txt') or die "$!";
print $OUT $dir;
close $OUT;
KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

Sure.... thanks so much for the help. Actually I am a beginner of per and I was using only system command and was not aware of qx.

Thanks...

Paryushan
Newbie Poster
9 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

As a beginner you should read the documentation for functions and modules and such things as you try and use them. You would notice that system() does not return any application output back to your perl script.

If you don't know how to read the perl documentation locally, you can read it online:

http://perldoc.perl.org/

the version you have installed locally may not be the latest version of perl, which the online documentation is. But most of the core functions are still the same.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You