How to open an exe and pass arguments to it from batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dipk11
Posts: 1
Joined: 24 Jul 2017 21:52

How to open an exe and pass arguments to it from batch file

#1 Post by Dipk11 » 24 Jul 2017 22:02

I want to create a batch file to run an exe placed on the desktop of my PC.
The exe needs some arguments to be passed when i run it from the cmd line.
I donot know how to make a batch file to execute this.

I do the following steps on the command line

cd Desktop
MPC2210.exe -help
MPC2210.exe getdevices

This displays some data on the command line.

I want to run this in a single batch file..but "help" and "getdevices " should be user inputs.

Can anybody help me?

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to open an exe and pass arguments to it from batch file

#2 Post by PaperTronics » 25 Jul 2017 03:25

Although I don't understand your problem correctly, I think this code just might do the trick:

Code: Select all

@echo off
:start
cls
set /p var="Enter any argument: "
MPC2210.exe %var%
echo.
echo.
echo Done!
pause >nul
goto :start 





PaperTronics

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to open an exe and pass arguments to it from batch file

#3 Post by SIMMS7400 » 25 Jul 2017 05:14

VBS is the best method. I leverage VBS a lot as one of my clients uses MS Access.

Here is a VBS that accepts arguments from a batch script:

Code: Select all

set objexcel = createobject("Excel.Application")

Dim objexcel
Dim objworkbook1, objworkbook2, objworkbook3, objworksheet, objWorksheet1, objWorksheet2, objWorksheet3
Dim i, startrow, intnewrow, endrow, countsheet
Dim Firstcell, Lastcell, LastCell2, LastCell3 , LastCell4
Dim CMSO

Dim args, HC_EXCEL_PREPPATH, HC_EXCEL_SUBPATH, HC_TEMP_UTIL_WB, HC_UTIL_WB, HC_ACT_FILE, HC_EXCEL_TAB, FILEXT
Set args = Wscript.arguments

HC_EXCEL_PREPPATH= args(0)
HC_EXCEL_SUBPATH= args(1)
HC_TEMP_UTIL_WB= args(2)
HC_UTIL_WB= args(3)
HC_ACT_FILE= args(4)
HC_EXCEL_TAB= args(5)
FILEXT= args(6)

CMSO = "SAP_to_Cost_Center_CMSO_MU_Reference_Mapping.csv"

Set objWorkbook2= objExcel.Workbooks.Open(HC_EXCEL_SUBPATH & HC_UTIL_WB)
   
If HC_EXCEL_TAB = "Start" Then

   WScript.Echo "Updating 'HC_Input_Data' of " & HC_UTIL_WB & " with all " & HC_TEMP_UTIL_WB & " content"
   
   Set objworkbook1= objExcel.Workbooks.Open(HC_EXCEL_PREPPATH & HC_TEMP_UTIL_WB)
   
   '::-- Get LastCell of 'HC_Input_Data' tab of HC_UTIL_WB--::'
   Set objWorksheet1 = objWorkbook2.Worksheets("HC_Input_Data")
   xlUp = -4162
   Set LastCell = objWorksheet1.Range("A" & objWorksheet1.Rows.Count).End(xlUp)
   
   '::-- Clear specified range in HC_UTIL_WB --::'
   objRange = objWorkbook2.Worksheets("HC_Input_Data").Range("A2:E" & LastCell.Row).Clear
   objRange = objWorkbook2.Worksheets("HC_Input_Data").Range("H3:O" & LastCell.Row).Clear
   
   '::-- Put all Sheets in for loop in HC_TEMP_UTIL_WB --::'
   objExcel.Application.DisplayAlerts = False
   
   countSheet = objworkbook1.Sheets.Count
   intnewrow=2
   i = 1
   for i = 1 to countsheet
   Set objWorksheet = objWorkbook1.WorkSheets(i)
   objworksheet.Activate
   startrow = 2

   '::-- Count the number of used rows --::'
   endrow = objWorkbook1.Worksheets(i).UsedRange.Rows.Count

   '::-- Copy the data --::'
   
   objWorkbook1.Worksheets(i).Range("A" & startrow &":E"& endrow).Copy
   
   '::-- Paste in HC_UTIL_WB --::'
   objWorkbook2.Worksheets("HC_Input_Data").Cells(intNewRow,1).Pastespecial
   
   '::-- Increment the row --::'
   intNewRow = intNewRow + (endrow - startrow + 1)

   Next
   
   '::-- Adjust formula columns to match last row on Column A --::'
   Set objWorksheet1 = objWorkbook2.Worksheets("HC_Input_Data")
   xlUp = -4162
   Set LastCell = objWorksheet1.Range("A" & objWorksheet1.Rows.Count).End(xlUp)
   Set SourceRange = objWorksheet1.Range("H2:O2")
   Set FillRange = objWorksheet1.Range("H2:O" & LastCell.Row)
   SourceRange.AutoFill FillRange
   
   objWorkbook2.RefreshAll
   
   objExcel.Application.DisplayAlerts = False
   WScript.Echo " "
   WScript.Echo "Updating Master CMSO CostCenter List..."

   Set objworkbook3= objExcel.Workbooks.Open(HC_EXCEL_PREPPATH & CMSO)
   
   '::-- Clear used range in HC_UTIL_WB --::'
   objRange = objWorkbook2.Worksheets("Master_CC_Data").UsedRange.ClearContents

   '::-- Find last cell in CMSO Workbook --::'
   Set objWorksheet3 = objworkbook3.Worksheets("SAP_to_Cost_Center_CMSO_MU_Refe")
   xlUp = -4162
   Set LastCell = objWorksheet3.Range("A" & objWorksheet3.Rows.Count).End(xlUp)   
   
   '::-- Copy Column A from CMSO Workbook and paste in tab "Master_CC_Data" of HC_Utility Workbook --::'
   objRange = objworkbook3.Worksheets("SAP_to_Cost_Center_CMSO_MU_Refe").Range("A2:A" & LastCell.Row).Copy
   objWorkbook2.Worksheets("Master_CC_Data").Range("A2:A" & LastCell.Row).PasteSpecial objRange
   
   ''
   '::-- Find last cell in CMSO Workbook --::'
   Set objWorksheet2 = objworkbook2.Worksheets("Master_CC_Data")
   xlUp = -4162
   Set LastCell = objWorksheet2.Range("A" & objWorksheet2.Rows.Count).End(xlUp)
   
   objWorkbook2.Worksheets("Master_CC_Data").Range("C2").Value = "=TRIM(A2)"
   
   Set SourceRange = objWorksheet1.Range("C2")
   Set FillRange = objWorksheet1.Range("C2:C" & LastCell.Row)
   SourceRange.AutoFill FillRange
   
   objWorkbook2.Worksheets("Master_CC_Data").Range("C2:C" & LastCell.Row).Copy
   objWorkbook2.Worksheets("Master_CC_Data").Range("A2:A" & LastCell.Row).PasteSpecial -4163
   objRange = objWorkbook2.Worksheets("Master_CC_Data").Range("C2:C" & LastCell1.Row).Clear   
   ''
   
   '::-- Used LastCell to add "CMSO" to Column B of "Master_CC_Data" of HC_Utility Workbook --::'
   objWorkbook2.Worksheets("Master_CC_Data").Range("B2:B" & LastCell.Row).Value = "CMSO"
      
   objworkbook1.close
   objworkbook3.close   
   
Else

'::-- Prepare Ascend FTE Submission Files --::'

a=Split(HC_EXCEL_TAB,"$")
b=ubound(a)
For i=0 to b

HC_EXCEL_TAB = a(i)
HC_ACT_FILE = a(i) & FILEXT

   '::-- Get LastCell in HC_EXCEL_TAB of HC_UTIL_WB --::'
   Set objWorksheet2 = objWorkbook2.Worksheets(HC_EXCEL_TAB)
   xlUp = -4162
   LastCell2 = objWorksheet2.Range("C" & objWorksheet2.Rows.Count).End(xlUp).Row
   xlUp = -4163
   
   '::-- Data starts on Row 6 in all Workbooks --::'
   '::-- If LastCell returns less than 6, it means no data exists for that HC_EXCEL_TAB --::'
   
   If LastCell2 > 5 Then
   
      WScript.Echo "Entity " & HC_EXCEL_TAB & " has Headcount Data"
      WScript.Echo "Building " & HC_ACT_FILE & " Ascend FTE Submission File"
      WScript.Echo "=========================================================================="
   
      Set objWorkbook3= objExcel.Workbooks.Open(HC_EXCEL_SUBPATH & HC_ACT_FILE)
         
      '::-- Get LastCell in HC_ACT_FILE --::'
      Set objWorksheet3 = objWorkbook3.Worksheets("Sheet1")
      xlUp = -4162
      LastCell3 = objWorksheet3.Range("A" & objWorksheet3.Rows.Count).End(xlUp).Row
                  
      objRange = objWorkbook3.Worksheets("Sheet1").Range("A2:Z" & LastCell3).Clear
      
      '::-- Subtract 4 cells to make up the difference between where content starts between HC_EXCEL_TAB & HC_ACT_FILE  --::'
      LastCell4 = LastCell2 - 4
      
      '::-- Copy content from HC_UTIL_WB in HC_EXCEL_TAB to HC_ACT_FILE  --::'      
      objWorkbook2.Worksheets(HC_EXCEL_TAB).Range("H6:M" & LastCell2).Copy
      objWorkbook3.Worksheets("Sheet1").Range("A2:F" & LastCell4).PasteSpecial -4163
         
      objworkbook3.save
      objworkbook3.close
         
   Else
   
      WScript.Echo "Entity " & HC_EXCEL_TAB & " has no Headcount Data"
      WScript.Echo "Ascend FTE Submission File will not be built"
      WScript.Echo "=========================================================================="
            
   End If
Next
End If

'::-- Exit Protocol --::'
   
objworkbook2.save
objworkbook2.close
objExcel.Quit
WScript.Quit 0



And here is the portion of batch that calls this particular VBS:

Code: Select all

>>"%LOGFILE%" (
ECHO.                                          
ECHO ********************************************************
ECHO Generate Headcount Submission File^(s^) for Ascend                            
ECHO ********************************************************
ECHO.                                          
)

SET "PREP=Start"
SET "ENTITY_1=HCT_ACT_US01"
SET "ENTITY_2=HCT_ACT_06"
SET "ENTITY_3=HCT_ACT_07"
SET "ENTITY_4=HCT_ACT_07_CEI"
SET "ENTITY_5=HCT_ACT_08"
SET "ENTITY_6=HCT_ACT_09"
SET "ENTITY_7=HCT_ACT_10_PD"

SET "ENTITY_ALL=%ENTITY_1%$%ENTITY_2%$%ENTITY_3%$%ENTITY_4%$%ENTITY_5%$%ENTITY_6%$%ENTITY_7%"

::-- Invoke VB Script Usage Details --::
   ::-- cscript <VB Script Name> "Argument0" "Argument1" "Argument2" "Argument3" "Argument4" Argument5"
      ::-- Argument0 = HC_EXCEL_PREPPATH --::
      ::-- Argument1 = HC_EXCEL_SUBPATH  --::
      ::-- Argument2 = HC_TEMP_UTIL_WB   --::
      ::-- Argument3 = HC_UTIL_WB        --::
      ::-- Argument4 = HC_ACT_ABBREV         --::
      ::-- Argument5 = HC_Utility Tab    --::
      ::-- Argument6 = Excel File Ext    --::
   
      ::-- All arguments are passed to the VB Script dynamically - no changes are required to VB Script ever --::

SET "NF5="
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%H IN ( %PREP% %ENTITY_ALL% ) DO (
   CLS
   CSCRIPT %HC_SF_VBS% "%HC_EXCEL_PREPPATH%" "%HC_EXCEL_SUBPATH%" "%HC_TEMP_UTIL_WB%" "%HC_UTIL_WB%" "%HC_ACT_ABBREV%_%%H.%EXT%" %%H .%EXT% && (
      
         IF "%%H"=="Start" ECHO Completed ^: Prepare Main Utility File >>"%LOGFILE%"
         IF "%%H" NEQ "Start" (
            ECHO Completed ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            )
         ) || (
         IF "%%H"=="Start" (
            ECHO Failed    ^: Prepare Main Utility File >>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         ) ELSE (
            ECHO Failed    ^: Create Headcount Submission File^(s^)>>"%LOGFILE%"
            SET "NF5=T"& GOTO AbnormalExit
         )
         )
PING -n 2 127.0.0.1>nul   
)

CALL :COPY_HC_ACT_FS %ENTITY_ALL%


let me know if you need any help.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to open an exe and pass arguments to it from batch file

#4 Post by Squashman » 25 Jul 2017 08:38

SIMMS7400 wrote:VBS is the best method. I leverage VBS a lot as one of my clients uses MS Access.

Here is a VBS that accepts arguments from a batch script:


Maybe a smaller example showing the basic use would help the user more.

Post Reply