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

In reply to Executing *.bat containing perl using VB Shell by ajanth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.