Home: Perl Programming Help: Intermediate:
How to Run a .bat file from perl Script



koti688
Novice

May 18, 2009, 6:45 AM


Views: 3518
How to Run a .bat file from perl Script

Hello Friends

I have a .bat file named make.bat in a folder , Can you please tell me how to run that file.

It will result a output folder when i run that bat file manually.

But when i open that .bat file through perl script as a file , it is not doing so.

This is my Script

$makedata="../../FinalSprint07\/fe_svs\/make.bat";
open my $TEST,'<', $makedata or die "'$makedata' file will not open $!";


Please help.
thanks in advance


hem
User

May 18, 2009, 8:06 AM


Views: 3512
Re: [koti688] How to Run a .bat file from perl Script

Hi

You can use a system or exec command in the perl script

system("xyz.bat");

That should run the batch file


KevinR
Veteran


May 18, 2009, 8:10 AM


Views: 3510
Re: [koti688] How to Run a .bat file from perl Script


In Reply To
Hello Friends

I have a .bat file named make.bat in a folder , Can you please tell me how to run that file.

It will result a output folder when i run that bat file manually.

But when i open that .bat file through perl script as a file , it is not doing so.

This is my Script

$makedata="../../FinalSprint07\/fe_svs\/make.bat";
open my $TEST,'<', $makedata or die "'$makedata' file will not open $!";


Please help.
thanks in advance


That opens a file for reading, not for running an application. Use system() if the bat file does not return any output back to your perl script. Or use qx{} if it does.

More than likely whatever your bat file does could be done by perl itself and avoid the entire issue.
-------------------------------------------------


koti688
Novice

May 18, 2009, 8:14 AM


Views: 3508
Re: [hem] How to Run a .bat file from perl Script

But that batch file is in another folder and my scripts are in another folder, how to give the path to that batch file now , please tell me i am a beginner.

Thanks


koti688
Novice

May 18, 2009, 8:22 AM


Views: 3507
Re: [koti688] How to Run a .bat file from perl Script

system ("cd C:\FinalSprint07\fe_svs");
system ("make.bat");


it says as

The filename, directory name, or volume label syntax is incorrect.
'make.bat' is not recognized as an internal or external command,
operable program or batch file.


FishMonger
Veteran / Moderator

May 18, 2009, 8:33 AM


Views: 3505
Re: [koti688] How to Run a .bat file from perl Script

Perl has it's own chdir function, so there's no reason to use a system call to cd into anthoer directory.


Quote
C:\test>perldoc -f chdir
chdir EXPR
chdir FILEHANDLE
chdir DIRHANDLE
chdir Changes the working directory to EXPR, if possible. If EXPR is
omitted, changes to the directory specified by $ENV{HOME}, if
set; if not, changes to the directory specified by $ENV{LOGDIR}.
(Under VMS, the variable $ENV{SYS$LOGIN} is also checked, and
used if it is set.) If neither is set, "chdir" does nothing. It
returns true upon success, false otherwise. See the example
under "die".

On systems that support fchdir, you might pass a file handle or
directory handle as argument. On systems that don't support
fchdir, passing handles produces a fatal error at run time.


Use forward slashes in the path.


Code
chdir 'C:/FinalSprint07/fe_svs'; 
system('make.bat');


If your batch file is using full paths, then there's no need to cd into the directory.

Code
system('C:/FinalSprint07/fe_svs/make.bat');


Of course, as Kevin mentioned, you could drop the batch file and do everything in the Perl script.


KevinR
Veteran


May 18, 2009, 8:36 AM


Views: 3503
Re: [FishMonger] How to Run a .bat file from perl Script

FYI....

http://www.perlmonks.org/?node_id=764614
-------------------------------------------------


FishMonger
Veteran / Moderator

May 18, 2009, 8:45 AM


Views: 3501
Re: [KevinR] How to Run a .bat file from perl Script


In Reply To


And
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_24417253.html


KevinR
Veteran


May 18, 2009, 8:55 AM


Views: 3496
Re: [FishMonger] How to Run a .bat file from perl Script

Ahh.... good old EE.
-------------------------------------------------


$_
Novice

May 18, 2009, 11:50 PM


Views: 3485
Re: [koti688] How to Run a .bat file from perl Script

Since you know Perl, I'd advice you to download cygwin... you'll have access to a true 'bash', much better than the .bats