Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run Windows command in perl script?

Status
Not open for further replies.

hujirong

Technical User
Aug 22, 2005
13
CA
e.g. I want to run dir > temp inside a perl script. Thanks.
 
Have a look to "system" function and "backsticks".

In this forum there are a lot of examples..

dmazzini
GSM System and Telecomm Consultant

 
If all you want is to get a list of files in a directory so you can process them, you don't need to redirect the output to a file. Here's an example that gets the list of files into an array.
Code:
my @fileList = qx[dir "temp\\*.xml" /B];
chomp @fileList;
print join("\n", @fileList);
The /B switch on the dir command suppresses headings. The example gets all the *.xml files in the directory, but you can change it.
 
Code:
my @file_list = (<temp/*.xml>);

Why start an exra process? perl has plenty of file and directory handling features built in.

You'll note that I used a forward slash rather than a pair of backslashes. Windows can use either. If you always use forwardslahes, your code is easier to read, you'll never forget to backslash-escape a backslash and, who knows, it might run on *ix boxes as well. I did it 'cos I'm lazy.

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top