Results 1 to 15 of 15

Thread: Pass a string to a DOS batch file

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Pass a string to a DOS batch file

    I am using shell to open a DOS batch file to run a program.

    I can hard code the path to use in the batch file, but, I would prefer to get it from VBA which is getting the Activeworkook.path "\filename". That way the program can be installed differently in at different workstations.

    Is there a way to do this?

    Thanks

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Pass a string to a DOS batch file

    You can also output a text based file with the .BAT extension in which you place the information you need..

    What exactly are you wanting the batch to do??
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    I have a set of Excel workbooks that I am using to perform a number of calculations with a wizard for data entry. Some of the data that needs to be entered comes from other sources including web links and other programs.

    I can't just use the shell command alone because the other programs don't work properly that way. I have to first open the DOS emulator and then enter the command line argument to open the program I want.

    I'd like to specify a dynamic path for the file to open rather than a static one.

    So how do I go about creating and outputting a batch file?

    Thanks

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Pass a string to a DOS batch file

    You would open the file though File I/O (look up Write, Print or Line Input in vba help)

    VB Code:
    1. Dim FFile As Integer
    2.   FFile = FreeFile
    3.   Open "C:\MyBatch.Bat" For Output As #FFile
    4.   Print #FFile, "D:\"
    5.   Print #FFile, "CD MyFolder"
    6.   Print #FFile, 'other dos commands
    7.   Close #FFile
    8.   'now shell out to the batch file
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    Thanks. It took me a while to respond, by username had a typo and I had trouble logging back in.

    That works great. I have another problem though. Since I am using this to create a batch file that will run an application wherever it is installed I am having problems. The structure of the batch file has to be different if the app is installed on a network drive or the c: drive.

    If it's on the network drive the batch file would be:
    [drive]:
    appname.exe

    If it's on the c: drive however, the command prompt from the shell command is at c:\...My Documents as that is the homedrive. The only way I can get it to work is to have a batch file that is:
    cd c:\
    apppath
    appname.exe

    I think maybe the answer lies in creating a path statement for appname.exe.

    I can see how to do it manually, but don't know how to do it with code.

    Does anyone have any ideas?

    Thanks

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Pass a string to a DOS batch file

    Your answers in your first post.. get excel to writeout the ActiveWorbook.Path followed by the application Name
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    That works on a network drive but not if it's on the c:\ drive.

    For some reason, even if I have the full file name and path, it doesn't work from the c:\...My Documents prompt.

    I have to do cd: c:\
    then I can enter the path

    ???

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Pass a string to a DOS batch file

    Ok so then if it is not the c drive leave it as it is.. if it is the c drive then place the first line in to move to the C:\
    VB Code:
    1. If Left(ActiveWorkbook.Path,1) = "C" Then
    2.     'Print out the C Drive Version
    3.   Else
    4.     'print the other one
    5.   End If
    6.   Close #FFile
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    Thanks, I've got it working for the c: drive and the network drives.

    It gets curioser and curioser though as I threw a flash drive into the mix and it behaved differently in this situation???

    You'd think the if not Left() = c routine would work on this as well, however, I'm not going to worry about it as I'm more concerned with how it behaves in normal installations.

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    I think I spoke too soon.

    I took it home and tried it on a couple of other computers with different profiles and had to be changed to work on thos which I suppose means it won't work on the orginal.

    I stumbled on the setlocal command and thought I had the answer. I put together a batch file:

    setlocal
    path=pathtoapp; %path%
    app.bat

    and ran it and it worked.

    Then I made my dynamic batch file the way you described to create the file (only at first I left off the %path%) and the dos prompt got stuck in a loop until I got a message saying that the setlocal recursive limit had been reached.

    I put the %path% in and tried the original batch file and also tried app.exe at the DOS prompt and got the same results.

    I didn't use endlocal so I ran a batch file that had just that command and tried again with no luck.

  11. #11
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Pass a string to a DOS batch file

    Where exactly will the program that needs to be shelled out to be residing??

    You could take it away from the batch file process and hardcode the same commands into the xls file..

    Add the reference to "Microsoft Shell Controls And Automation" in the Tools, References in the VBE (Alt+F11)

    Then

    VB Code:
    1. Dim obsh As Shell
    2.   Dim strPathFile As String
    3.   strPathFile = "set this to where the file is going to be placed through code"
    4.   Set obsh = New Shell
    5.   obsh.ShellExecute strPathFile, , , , True
    6.   Set obsh = Nothing
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    It will be residing in [activeworkbook.path] which could vary from installation to installation.

    Tell me more please

  13. #13
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Pass a string to a DOS batch file

    then change the code to

    VB Code:
    1. Dim obsh As Shell
    2.   Dim strPathFile As String
    3.   strPathFile = ActiveWorkbook.Path & "\appname.exe"
    4.   Set obsh = New Shell
    5.   obsh.ShellExecute strPathFile, , , , True
    6.   Set obsh = Nothing
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    That opens the program and it behaves the same as if I just select it. It opens but doesn't work properly.

    If I go to the command prompt and manually navigate to the directory and then type the file it runs properly.

    When opening the command prompt I see it likes to go the the ...\My Documents directory which is a user profile setting of some sort.

    I was thinking that by usin the setlocal command in the batch file I could overide. I did sort of get that sorted out but I think as soon as the program starts the setlocal value is lost. I then think elsewhere in the program that I opened it is needed to still be there???

  15. #15

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    10

    Re: Pass a string to a DOS batch file

    Thanks,

    That code worked great. I got some help from the group alt.msdos.batch.nt as to how to construct the batch file:

    @ECHO OFF
    pushd "f:\discharge request screener"
    main.exe
    popd

    Anyway for anyone else wishing to open a program that behaves similarly to the ones I had, here is the code in the command button that creates the batch file and then calls it (the batch file creation code comes from help from a VBForums thread):

    Private Sub cmbRREL_Click()

    Dim FFile As Integer
    Dim strpathmain As String
    Dim strshortpath As String
    Dim line1 As String
    Dim line2 As String
    Dim line3 As String
    Dim line4 As String


    Dim RetVal

    strshortpath = ActiveWorkbook.Path
    strpathmain = ActiveWorkbook.Path & "\treat.bat"

    line1 = "@ECHO OFF"
    line2 = "pushd " & strshortpath
    line3 = "main.exe"
    line4 = "popd"

    FFile = FreeFile
    Open strpathmain For Output As #FFile

    Print #FFile, line1
    Print #FFile, line2
    Print #FFile, line3
    Print #FFile, line4
    Print #FFile, line5
    Print #FFile, line6

    Close #FFile


    'shell out to the batch file


    RetVal = Shell(Environ("comspec") & " /c " & Chr(34) & strpathmain & Chr(34), 1)


    End Sub

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