none
Executing a Batch file from MS Access 2 RRS feed

  • Question

  •   As your time permits, can you let me know how to resolve this issue.  I am calling another batch file from MS Access.  I will be passing 3 parms.

    A.  I want to pass P1 as the folder I am doing to do a Change Directory into when I get into the batch file.

    B.  P2 will be the command I am going to execute with its parms.

    C.  P3 will be the output file I am creating.  I am creating a sfx zip file (FYI).

    When I get into the batch file, I display the 3 parms.  They are as follows:

    

    I think I need to do the CD w/o the quotes around the name for P1.  I do not think I need to have any of these parms in quotes.  Can you guide me on this.  Here is the command from MS Access:

            SfxDlrNameO = rstDelFldr!DlrName
            SfxDlrName = Replace(SfxDlrNameO, " ", "_")
            SfxProfileName = "winrar a -cp" & SfxDlrName
            SfxOutputName = SfxDlrName & "_ISS"
            SfxParms = """" & SfxPath & """ """ & SfxFolderPath & """ """ & SfxProfileName & """ """ & SfxOutputName & """"
            Call Shell(SfxParms, 1)
    '        If errorlevel = 0 Then

      Also, trying to get the errorlevel when I get back from the batch file to see if it is successful, but I will worry about that latter.  Thanx in advance.

    Friday, January 4, 2019 5:01 PM

Answers

  • Just an FYI, I have figured this one out.  Here is the code:

            SfxPath = CurrentProject.Path & "\CreateDlrSfx.bat"

            SfxDlrName = Replace(SfxDlrNameO, " ", "_")
            Select Case folderpath
                Case "C:\Cloud Data\Devl"
                    SfxProfileName = SfxDlrName & "_Devl"
                Case "C:\Cloud Data\Test"
                    SfxProfileName = SfxDlrName & "_Test"
                Case "C:\Cloud Data\Prod"
                    SfxProfileName = SfxDlrName & "_Prod"
            End Select

            SfxOutputName = SfxDlrName & "_ISS"
            Sfxcmd = "winrar " & "a " & "-cp" & SfxProfileName & " " & SfxOutputName & ""
            SfxParms = """" & SfxPath & """ """ & SfxFolderPath & """  """ & Sfxcmd & """"
            Call Shell(SfxParms, 1)

    1.  Found out it is best to not have spaces in the names.  So I use "_" between any of the filenames.

    2.  Then I determine what region I am, so the profile name I use will be based on DEVL, TEST or PROD.

    3.  I append the output name with company abbreviation.

    4.  I create the command string that I will use in the batch file.

    5.  I string them all together.  

         A.  SfxPath = Batch filename I am going to call.

         B.  SfxFolderPath = Folder where the files are that I am going to use.  This way I can do CD to the folder.

         C.  Sfxcmd = Command string, profile name & output name.  I am creating a self-extracting zip file.

     6.  I call the batch file to run.

    My Batch File:

    CD "%~1"

    REM echo Command is as follows:
    REM echo %~2
    REM pause

    %~2

    A.  I perform a CD to the folder.

    B.  I execute the command string I passed to the batch file.

      I hope this can help someone along the way.

    • Marked as answer by ballj_351 Sunday, January 6, 2019 12:26 PM
    Sunday, January 6, 2019 12:26 PM

All replies

  • Hi,

    Is this related to your previous thread?

    Batch files can take arguments passed to it. For example:

    filename.bat arg1 arg2

    Then, within the batch file, you can retrieve those arguments using %x, where:

    %0 = name of batch file

    %1 = arg1

    %2 = arg2

    Hope it helps...

    Friday, January 4, 2019 5:29 PM
  • kind of.  I have gotten past the issue with that one, since I can now pass in the parms with the double quotes around them and it works just fine.  Now, I am trying to pass in the parms and not have the quotes around them.  I can run the command from the DOS prompt and I do not use any quotes, so this is what I am trying to do in the batch file.

    I also used P1, P2 and P3, instead of P0, P1 and P2.  Thanx for the information and testing will begin!

    Friday, January 4, 2019 5:46 PM
  • Okay, let us know how it goes...
    Friday, January 4, 2019 6:51 PM
  • I have been testing this code and I am close.  I have now made the following changes.

    1.  When calling the batch file, my code is as follows:

     SfxParms = """" & SfxPath & """ """ & SfxFolderPath & """ " & SfxProfileName & " " & SfxOutputName & ""

    The batch file is as follows:

    CD "%~1"
    cd
    pause

    echo "Command is as follows:
    echo winrar a -cp"%2" "%3"
    pause
    winrar a -cp"%2" "%3"

    if errorlevel 1 goto error

    The CD to path works great.

    The winrar command is failing.  I need to get it to the point where there are no quotes around %2 and %3.

    While debugging the code, you can see the result of the command when I echo the command.  See pix

    I need the command to be as follows:

    winrar a -cpKaren_Radley_Acura Karen_Radley_Acura_ISS

    Can someone plz help me to understand how to get it there?  This is the final piece and then I believe I have it.  Thanx in advance.

    Saturday, January 5, 2019 7:38 PM
  • Just an FYI, I have figured this one out.  Here is the code:

            SfxPath = CurrentProject.Path & "\CreateDlrSfx.bat"

            SfxDlrName = Replace(SfxDlrNameO, " ", "_")
            Select Case folderpath
                Case "C:\Cloud Data\Devl"
                    SfxProfileName = SfxDlrName & "_Devl"
                Case "C:\Cloud Data\Test"
                    SfxProfileName = SfxDlrName & "_Test"
                Case "C:\Cloud Data\Prod"
                    SfxProfileName = SfxDlrName & "_Prod"
            End Select

            SfxOutputName = SfxDlrName & "_ISS"
            Sfxcmd = "winrar " & "a " & "-cp" & SfxProfileName & " " & SfxOutputName & ""
            SfxParms = """" & SfxPath & """ """ & SfxFolderPath & """  """ & Sfxcmd & """"
            Call Shell(SfxParms, 1)

    1.  Found out it is best to not have spaces in the names.  So I use "_" between any of the filenames.

    2.  Then I determine what region I am, so the profile name I use will be based on DEVL, TEST or PROD.

    3.  I append the output name with company abbreviation.

    4.  I create the command string that I will use in the batch file.

    5.  I string them all together.  

         A.  SfxPath = Batch filename I am going to call.

         B.  SfxFolderPath = Folder where the files are that I am going to use.  This way I can do CD to the folder.

         C.  Sfxcmd = Command string, profile name & output name.  I am creating a self-extracting zip file.

     6.  I call the batch file to run.

    My Batch File:

    CD "%~1"

    REM echo Command is as follows:
    REM echo %~2
    REM pause

    %~2

    A.  I perform a CD to the folder.

    B.  I execute the command string I passed to the batch file.

      I hope this can help someone along the way.

    • Marked as answer by ballj_351 Sunday, January 6, 2019 12:26 PM
    Sunday, January 6, 2019 12:26 PM