Starting a Windows batch script

Windows specific questions.
Post Reply
jaskin
Posts: 60
Joined: Sep 01, 2018 20:19

Starting a Windows batch script

Post by jaskin »

How does one kick off a batch script (.bat) from FB? No wait is required.
jimdunn
Posts: 13
Joined: Jan 28, 2016 15:07

Re: Starting a Windows batch script

Post by jimdunn »

I know this is 5 years old, but... here's some answers:

1) You could write an elaborate function to start a BAT file...

Code: Select all

Private Function jRun (ByVal wszLine As CWSTR, ByVal jWait As UBYTE = 0) As LONG
    Dim As STRING sProgram
    Dim As STRING sArgs
    Dim As STARTUPINFO siStartInfo
    Dim As PROCESS_INFORMATION piProcInfo
    Dim As LONG rc, x, InternalFlag = 0
    Dim As DWord dwPid, exitCode
    Dim As STRING InternalCommands(0 To 42) = {"ASSOC","BREAK","CALL","CD","CHDIR","CLS","COLOR","COPY","DATE","DEL","DIR","ECHO","ENDLOCAL","ERASE","EXIT","FOR","FTYPE","GOTO","GRAFTABL","IF","MD","MKDIR","MKLINK","MOVE","PATH","PAUSE","POPD","PROMPT","PUSHD","RD","REM","REN","RENAME","RMDIR","SET","SETLOCAL","SHIFT","TIME","TITLE","TYPE","VER","VERIFY","VOL"}
    On Local Error Goto exitFunction
    ' /// Top Of Body ///

    If InStr(wszLine," ") Then
        sProgram = Left(wszLine,InStr(wszLine," ")-1)
        sArgs = Mid(wszLine,InStr(wszLine," ")) ' let sArgs begin with a space or NIRCMD doesnt work
    Else
        sProgram = wszLine
        sArgs = ""
    EndIf
    For x = 0 to UBound(InternalCommands)
        If UCase(sProgram) = InternalCommands(x) Then
            InternalFlag = 1
        EndIf
    Next x
    If Right(UCase(sProgram),4) = ".BAT" or Right(UCase(sProgram),4) = ".CMD" Then
        InternalFlag = 1
    EndIf
    ' could also check the pwd and path for sProgram, if bat/cmd then flag = 1...
    If InternalFlag > 0 Then
        sProgram = AfxEnviron("ComSpec")
        sArgs = " /c " & wszLine ' let sArgs begin with a space just in case
    EndIf

    siStartInfo.cb = SizeOf( STARTUPINFO )
    siStartInfo.dwFlags = STARTF_USESHOWWINDOW ' required for below wShowWindow to work
    siStartInfo.wShowWindow = SW_SHOWNORMAL
        'SW_HIDE             0
        'SW_SHOWNORMAL       1
        'SW_NORMAL           1
        'SW_SHOWMINIMIZED    2
        'SW_SHOWMAXIMIZED    3
        'SW_MAXIMIZE         3
        'SW_SHOWNOACTIVATE   4
        'SW_SHOW             5
        'SW_MINIMIZE         6
        'SW_SHOWMINNOACTIVE  7
        'SW_SHOWNA           8
        'SW_RESTORE          9
        'SW_SHOWDEFAULT      10
        'SW_FORCEMINIMIZE    11
        'SW_MAX              11
    rc = CreateProcess(sProgram,sArgs,NULL,NULL,FALSE,CREATE_NEW_PROCESS_GROUP,NULL,"C:\Temp",@siStartInfo,@piProcInfo)
    If rc <> 0 Then
        dwPid = GetProcessId(piProcInfo.hProcess)
        If jWait <> 0 Then
            WaitForSingleObject(piProcInfo.hProcess, INFINITE)
            GetExitCodeProcess(piProcInfo.hProcess, @exitCode)
        Else
            WaitForInputIdle(piProcInfo.hProcess, INFINITE)
        EndIf
        Return 1
    Else
        Return 0
    EndIf

    ' /// End Of Body ///
    goto endFunction
exitFunction:
endFunction:
    Return -1 ' error
End Function
2) Or you could use PIPE...

Code: Select all

Private Function jRunPipe (ByVal wszLine As CWSTR) As CWSTR ' blocking
    Dim As LONG fp = FreeFile
    Dim As STRING sTemp, sCmd = jDQ(wszLine)
    Dim As CWSTR sBuf = ""

    If Len(sCmd) > 0 Then
        Open Pipe sCmd For Input As #fp
        Do Until EOF(fp)
            Line Input #fp, sTemp
            sBuf &= sTemp & CRLF
        Loop
        Close #fp
    EndIf
    Return sBuf
End Function
3) Or, just use one of the internal FreeBasic commands...

Code: Select all

Dim rc As Long = Run("command.com /c test.bat")
The key is that an EXE can just be run... but a BAT or CMD script must be called using "command.com"
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Starting a Windows batch script

Post by coderJeff »

Does this depend on windows version? I don't remember anymore. I think this is correct...

Waits for batch program to complete before returning to the calling program:

Code: Select all

shell "program.bat"
Does not wait for batch program to complete and calling program runs at same time as batch program:

Code: Select all

shell "start ""title"" program.bat"
check options for windows start command start /? for other behaviours
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Starting a Windows batch script

Post by deltarho[1859] »

Consider Exec (With arguments), Chain (Without arguments)
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Starting a Windows batch script

Post by Vortex »

This one should work too :

Code: Select all

#include "windows.bi"

WinExec("test.bat",SW_SHOW)
jimdunn
Posts: 13
Joined: Jan 28, 2016 15:07

Re: Starting a Windows batch script

Post by jimdunn »

I can't figure out how to THUMBS UP a post... so... this instead...
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Starting a Windows batch script

Post by deltarho[1859] »

Image
jaskin
Posts: 60
Joined: Sep 01, 2018 20:19

Re: Starting a Windows batch script

Post by jaskin »

Many thanks for the replies. The only one I didn't know about was the WinExec method by Vortex. I tried all the rest but for some reason they didn't work. They worked for .exe files. I will try again later.
Post Reply