Results 1 to 21 of 21

Thread: Working with batch files

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Working with batch files

    Hello. I want to make a program in VB6 so that if you click a button the program asks you in a textbox for user input then it saves the input as a line in a batch file and runs it. Can anyone please help me? It isn't working. Thank you in advance.

    Code:
    Private Declare Function ShellExecute Lib _
         "shell32.dll" Alias "ShellExecuteA" _
         (ByVal hwnd As Long, ByVal lpOperation _
         As String, ByVal lpFile As String, ByVal _
         lpParameters As String, ByVal lpDirectory _
         As String, ByVal nShowCmd As Long) As Long
         
    Const SW_HIDE = 0
    Const SW_SHOWNORMAL = 1
    Const SW_NORMAL = 1
    Const SW_SHOWMINIMIZED = 2
    Const SW_SHOWMAXIMIZED = 3
    Const SW_MAXIMIZE = 3
    Const SW_SHOWNOACTIVATE = 4
    Const SW_SHOW = 5
    Const SW_MINIMIZE = 6
    Const SW_SHOWMINNOACTIVE = 7
    Const SW_SHOWNA = 8
    Const SW_RESTORE = 9
    Const SW_SHOWDEFAULT = 10
    Const SW_MAX = 10
    
    Private Sub Command1_Click()
    usercommand = InputBox("Enter the command")
            
            
            Dim intFF As Long
            intFF = FreeFile
            Open "C:\start.bat" For Output As intFF
            Print intFF, "somecodehere"
            'Close intFF
            
            Dim intFF2 As Long
            intFF2 = FreeFile
            Open "C:\command.bat" For Output As intFF2
            Print intFF2, "somecodehere"
              ' something
            'Close intFF2
            
            Dim intFF3 As Long
            intFF3 = FreeFile
            'Open "C:\command.bat" For Append As intFF3
            Print intFF3, "usercommand"
            
            ShellExecute 0&, vbNullString, "t", vbNullString, _
                 "C:\=start.bat", SW_SHOWDEFAULT
            't = Shell("C:\start.bat")
             ShellExecute 0&, vbNullString, "z", vbNullString, _
                 "C:\=command.bat", SW_SHOWDEFAULT
            'z = Shell ("C:\command.bat")
            
            Dim intFF4 As Long
            intFF4 = FreeFile
            Open "C:\=drive.txt" For Input As intFF4
            MsgBox (intFF4)
            Close intFF
            Close intFF2
            Close intFF3
    
    End Sub

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Working with batch files

    Well first off you have to put a # in front of your file number

    Second, I have no idea what you are writing to the files


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Do you really need to write the batch script to disk? How about executing it "in-memory" like this?

    Code:
    Shell "CMD.EXE /C @TITLE File-less BAT Script & @ECHO Hi %UserName%! & @ECHO. & @ECHO You typed """ & InputBox("Enter the command") & """ & @ECHO. & PAUSE", vbNormalFocus
    The /C switch "Carries out the command specified by the string and then terminates". The @ character prevents echoing the issued command to the standard output. The & character concatenates multiple commands together. Environment variables are denoted by %EnvVarName% and can be declared via the SET command.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Working with batch files

    Yes I need to because the program adds the user command to the batch and then runs it. It needs other program to run, that's why I work with batch files. Can you please help me fix my software? Thank you.

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Quote Originally Posted by cristyro View Post
    Can you please help me fix my software?
    Which one? The code in post #1?

    Can you please describe what you're attempting to do in more detail? Also, can you show the real code you have written so far? Your code in post #1 looks like just an example of what you're trying to achieve.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Working with batch files

    That's my only code. I am trying to make a program that does this: it shows a button, and if you click it then it asks you (in a VB6 box, not CMD line) for a command. That command gets saved in a file, and the other program (not mine) uses it (sends the SCSI command to the drive). There is already a batch file which runs that program, but that batch file needs to be updated by this software with the user specified command and then it must be started in order to make that other program (not mine) to send the SCSI command.

  7. #7
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    Re: Working with batch files

    Quote Originally Posted by cristyro View Post
    shows a button, and if you click it then it asks you (in a VB6 box, not CMD line) for a command. That command gets saved in a file
    Code:
    Private Sub Command1_Click()
    Dim something
    something = InputBox("What is the command ?", "Command to Enter")
    MsgBox "you entered " & something  '''' or 'save it where ever 
    End Sub
    something like that ?

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Quote Originally Posted by cristyro View Post
    It isn't working.
    It's probably because you were implicitly invoking the Form's Print method instead of specifying the Print # statement.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim FN As Integer, strUserCmd As String, strBuffer As String
    
        strUserCmd = InputBox("Enter the command")
    
        FN = FreeFile
        Open "C:\start.bat" For Output As FN
            Print #FN, "somecodehere"
        Close FN
    
        FN = FreeFile
        Open "C:\command.bat" For Output As FN
            Print #FN, "somecodehere"
        Close FN
    
        FN = FreeFile
        Open "C:\command.bat" For Append As FN
            Print #FN, strUserCmd
        Close FN
    
        Shell "C:\start.bat", vbNormalFocus
        Shell "C:\command.bat", vbNormalFocus 'or vbHide to make the console window invisible
    
        FN = FreeFile
        Open "C:\drive.txt" For Input As FN
            strBuffer = Input$(LOF(FN), FN)
            MsgBox strBuffer, vbInformation
        Close FN
    End Sub
    BTW, next time, don't just say "It isn't working". We generally have no idea what you mean by that. Instead, always be specific and state exactly what isn't working.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  9. #9
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Exclamation Re: Working with batch files

    another point what are you doing with it .somecodehere" i would suggest take one string type variable .and store data in it .together with this please use err handler . so that you will get identify issue .
    Code:
    Option Explicit
    Dim FN As Integer, strUserCmd As String, strBuffer As String
    
    Private Sub Command1_Click()
        
        
        strUserCmd = InputBox("Enter the command")
    
        FN = FreeFile
        Open "C:\start.bat" For Output As FN
            Print #FN, "somecodehere"
        Close FN
    
        FN = FreeFile
        Open "C:\command.bat" For Output As FN
            Print #FN, "somecodehere"
        Close FN
    
        FN = FreeFile
        Open "C:\command.bat" For Append As FN
            Print #FN, strUserCmd
        Close FN
    
        Shell "C:\start.bat", vbNormalFocus
        Shell "C:\command.bat", vbNormalFocus 'or vbHide to make the console window invisible
    
        FN = FreeFile
        Open "C:\drive.txt" For Input As FN
            strBuffer = Input$(LOF(FN), FN)
            MsgBox strBuffer, vbInformation
        Close FN
    End Sub
    BTW

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Working with batch files

    There is a problem: the other app isn't doing anything. It just opens and closes fast, like when ran directly from a folder and not using Start > Run > cmd and then running it.

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Quote Originally Posted by firoz.raj View Post
    another point what are you doing with it .somecodehere" i would suggest take one string type variable .and store data in it
    I think it's just a placeholder for the actual batch script.



    firoz.raj

    Why did you copy the code I've posted? This is the 2nd time you've done that.

    I think you've been warned before not to plagiarize other member's posts. Why do you keep doing that?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Working with batch files

    There is a problem: the other app isn't doing anything. It just opens and closes fast, like when ran directly from a folder and not using Start > Run > cmd and then running it.
    just put breakpoint at command1_click . and debug step by step .

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Quote Originally Posted by cristyro View Post
    There is a problem: the other app isn't doing anything. It just opens and closes fast, like when ran directly from a folder and not using Start > Run > cmd and then running it.
    Can you show the code for that?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Working with batch files

    I don't have the code for that app, it isn't written by me. My program just has to save a customized batch which will run that program and that program will send the scsi command.

  15. #15
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Quote Originally Posted by cristyro View Post
    I don't have the code for that app, it isn't written by me. My program just has to save a customized batch which will run that program and that program will send the scsi command.
    I mean, where is your code that shells that other app? How are you calling it?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  16. #16

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Working with batch files

    At the start:

    Code:
    strUserCmd = InputBox("Enter the command")
    
        FN = FreeFile
        Open "C:\start.bat" For Output As FN
            Print #FN, "start nameofthatprogram"
        Close FN
    The "nameofthatprogram" is that program. It runs in cmd prompt.

  17. #17
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    So, you are starting the program but you are not passing any command line parameters to it? I think the behavior you mentioned is, indeed, to be expected:

    Quote Originally Posted by cristyro View Post
    It just opens and closes fast, like when ran directly from a folder ...
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  18. #18

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Working with batch files

    I do pass command to it. Maybe the two
    Code:
    Shell
    from the code need to be ran at the same time? The first one starts the program and the second one sends the parameters to it...

  19. #19
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Working with batch files

    can you try the following way ? .why are you not using strusercmd after getting input .
    Code:
    strUserCmd = InputBox("Enter the command")
    
        FN = FreeFile
        Open "C:\start.bat" For Output As FN
            write #FN, strUserCmd  'cmd name will be storedstrusercmd
        Close  #FN
    together with this close statement also needs # sign in before . i would really suggest please debug step by step .
    after keeping break point at command1_click.
    Code:
    Option Explicit
    Dim FN As Integer, strUserCmd As String, strBuffer As String
    
    Private Sub Command1_Click()
        
        
        strUserCmd = InputBox("Enter the command")
    
        FN = FreeFile
        Open "C:\start.bat" For Output As FN
            Print #FN, "somecodehere"
        Close #FN
    
        FN = FreeFile
        Open "C:\command.bat" For Output As FN
            Print #FN, "somecodehere"
        Close #FN
    
        FN = FreeFile
        Open "C:\command.bat" For Append As FN
            Print #FN, strUserCmd
        Close #FN
    
        Shell "C:\start.bat", vbNormalFocus
        Shell "C:\command.bat", vbNormalFocus 'or vbHide to make the console window invisible
    
        FN = FreeFile
        Open "C:\drive.txt" For Input As FN
            strBuffer = Input$(LOF(FN), FN)
            MsgBox strBuffer, vbInformation
        Close #FN
    End Sub

  20. #20
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Working with batch files

    Quote Originally Posted by cristyro View Post
    Maybe the two Shell from the code need to be ran at the same time? The first one starts the program and the second one sends the parameters to it...
    Well, did you expect two separate instances of the command prompt window to be able to communicate with each other? Of course, they can't. You'll have to launch the app and pass parameters to it using a single batch file ...

    OR ...

    tell us exactly what the contents of the batch files you have now and I'll see if we can get rid of those batch files by using the technique in post #3.

    If you will expose more of what you're trying to do, better approaches/solutions can be suggested.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  21. #21
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Working with batch files

    On another note you should not be opening the file more than once here
    Code:
    FN = FreeFile
        Open "C:\command.bat" For Output As FN
            Print #FN, "somecodehere"
        Close FN
    
        FN = FreeFile
        Open "C:\command.bat" For Append As FN
            Print #FN, strUserCmd
        Close FN
    There is no reason to close the file and then open it again here, all that does is makes the code longer and slows down the program

    It should be more like this
    Code:
    FN = FreeFile
        Open "C:\command.bat" For Output As FN
            Print #FN, "somecodehere"    
            Print #FN, strUserCmd
        Close #FN

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width