ajanth has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I have a wierd problem executing a *.bat file that
contains code to execute a perl file in windows 2000.
If I execute the *.bat file directly through windows,
the perl code launches fine.
The *.bat file contains:
(path to perl.exe) (path to perl file)file.pl < (inputfile with path) +> (outputfile with path) example: C:\Perl\bin\perl.exe C:\test.pl < C:\sometextfile.txt > C:\someresultf +ile.txt I call this *.bat file from within VB by using the following code: iTask = Shell(Environ$("COMSPEC") & " /c " & sPath, vbHide) pHandle = OpenProcess(SYNCHRONIZE, False, iTask) If pHandle <> 0 Then WaitForSingleObject pHandle, INFINITE CloseHandle pHandle End If ' sPath is the path to the bat file. ' You can do sPath = AddBackSlash(App.Path)&"batchfile.bat" ' to create the proper path in vb
The *.bat file pops up and then immediately closes.
Any help would be greatly appreaciated :)
Thanks,
-Ajanth


Solution:
Hi everyone, thanks for all your suggestions. Here is how the problem was solved:
My main.pl script had the following lines at the beginning:
use Common; use Output; use Analysis; &Output::beginlogging; #process file line by line while ( $record = <STDIN> ) { etc...
When the *.bat file was executed via VB Shell() function, it couldn't find Common.pm in @INC.
To fix it, I added the following as part of the execute string in the *.bat file
-I(path to directory of modules) as a command line argument to perl
So the final string that appears in the *.bat file is:
(Assuming perl is already in the Win system PATH)

Section 1
perl -I(path to dir. of modules) (path to script)\script.pl < (path to + inputfile)\inputfile.txt > (path to outputfile)\outputfile.txt
In Visual Basic:
1) Let the user pick input/output files via common dialog
2) Form the string as shown in section 1
(note: my program accepts input and output files)
3) Write the string to a *.bat file
4) Execute the *.bat file via the following code:
This part goes in your general part of the vb file:>>> Option Explicit Private Declare Function WaitForSingleObject Lib "kernel32" _ (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function OpenProcess Lib "kernel32" _ (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Private Const INFINITE = -1& Private Const SYNCHRONIZE = &H100000 This part goes in a function or subroutine:>>> Dim iTask As Long, ret As Long, pHandle As Long iTask = Shell(Environ$("COMSPEC") & " /c " & sPath, vbHide) pHandle = OpenProcess(SYNCHRONIZE, False, iTask) If pHandle <> 0 Then WaitForSingleObject pHandle, INFINITE CloseHandle pHandle End If
You can use sPath = AddBackSlash(App.Path) & "temp.bat"
to create the path to the batch file. AddBackSlash is
a function I wrote that adds a backslash to the end of
the Path if one is necessary.

And that's it :) Now you have a VB frontend to run
all those fancy perl scripts.
-Ajanth

Replies are listed 'Best First'.
Re: Executing *.bat containing perl using VB Shell
by holli (Abbot) on Jan 21, 2005 at 00:00 UTC
    try:
    Shell "C:\Perl\bin\perl.exe C:\test.pl < C:\sometextfile.txt > C:\some +resultfile.txt"

    holli, regexed monk
Re: Executing *.bat containing perl using VB Shell
by mpeters (Chaplain) on Jan 20, 2005 at 21:14 UTC
    This is not a perl question but a VB one. Try executing some other script/utiltity that is not perl and see if you get the same results. If so, find some VB gurus and ask them.
Re: Executing *.bat containing perl using VB Shell
by renz (Scribe) on Jan 20, 2005 at 22:21 UTC
    I know nothing about VB, so God only knows what is actually causing the problem. Maybe it's perl, maybe it's VB, but I don't have the necessary skillset to tell you for sure.

    But I will take this opportunity to mention BatWrap downloadable from Joseph Casadonte's page (it's pretty old -- ~1996), for all you Win32 cats who just cant seem to live with typing perl script.pl :). You may find it helpful too, though it seems a bit counterintuitive for this issue..

    Oh... And if anyone tries batwrap and notices that a final closing curly bracket on the last line of any given script is not carried over into the new batch file, I'd be interested to know.. It has worked for me before without doing this, but now every script I try to wrap, I have to edit and add a final }. I haven't really examined the code to see why this may be happening, as I just noticed it today and have been otherwise occupied. It may also be the case that I have never tried to wrap a script that has a single curly bracket on the last line before and this issue has always been present with the script.

    /renz.
      for all you Win32 cats who just cant seem to live with typing perl script.pl :)
      That's what pl2bat. BatWrap does something more.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.