Link to home
Start Free TrialLog in
Avatar of chokka
chokkaFlag for United States of America

asked on

How to write If - else if in dos command to call .exe file?

How to write If - else if  in dos command to call .exe file?

DOS Command - Windows Batch file

We need to call the Test.exe file based on file naming convention. We pass the input parameter based on the file name convention.

Source folder has around 10 different naming convention zip files.  

SET str1=ABC_399_asasasasas.zip
SET str2=ABC_400_nsasasasas.zip
SET str3=ABC_401_bsasasasas.zip

if %str1:~0,7%==ABC_399 (call test.exe 399)
pause
if %str2%==ABC_400 (call test.exe 400)
pause
if %str3%==ABC_401(call test.exe 401)
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Will the files always be "[Something with No Underscores]_[Test Code]_[Something with no Underscores].[Extention]"?

EG:

Always:  ABC_###_XYZ.zip
Never:  A_BC_###_XYZ.zip -or ABC_###_X_YZ.zip -or A_BC_###_X_YZ.zip

If so we can make a loop to parse the names for you and strip away the code in order to call the commands without any IF statements.

We can do the same with the example you've given:

:: Script: RunCMDbycode.cmd
@(
   SETLOCAL ENABLEDELAYEDEXPANSION
   ECHO OFF
   REM SET "_RootFolder=C:\Your\Root\Folder"
   SET "_TestCMD=C:\Path\To\testcmd\test.exe"
   SET str1=ABC_399_asasasasas.zip
   SET str2=ABC_400_nsasasasas.zip
   SET str3=ABC_401_bsasasasas.zip
   SET "_StrList="!str1!" "!str2!" "!str3!""
   REM SET "_Test=TRUE"
   SET "_eLvl=0"
)

CALL :Main

(
   ENDLOCAL
   EXIT /B %_eLvl%
)

:Main
   REM Loop through names
   FOR %%S IN (%_StrList%) DO (
      FOR /F "tokens=1 delims=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_!.= " %%C IN ("%%~nS") DO (
         ECHO.File Name: = %%~S
		 ECHO.Code: %%~C
		 ECHO.Example Of Callin Command: CALL %_TestCMD% %%~C
      )
   
   )
GOTO :EOF

Open in new window

Avatar of chokka

ASKER

@Ben, Thank you. I have not tested your syntax.

Yes, file will always be in this format

<CompanyName>_<ClientProfileId>_<FileName>_<Date>.zip

ABC_399_
ABC_400_

is the expected format. And the file format will remain the same.

Based on 399 or 400 or 401, our exe file will load the data to the database.
Avatar of chokka

ASKER

@Ben, Sorry.. Small correction .. We don't have a problem on Unzip process. Each zip will have around 100 to 200 .pdf file with the same naming convention.

Currently, we unzip all the zip files and put these .pdf files in the same folder. So one folder will have around 100 "399" client id .pdf files and another 100 "400" .pdf files.

When we load these files through .exe file.. some how .. we are loading incorrectly. This is due to .. we are calling the .exe as

call test.exe 399
call test.exe 400

.. there is no if condition before loading the file through .exe file.

I need IF Condition to fetch client id before calling the .exe file
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Author

  Because your data contains the code, there is no need to use "IF".

  In my example, I show how we can get the name of the file and extract the code from the file name, and then call the test CMD with the code from the file name.

  Since the IF is checking for the code from the file name and then calling the executable there isn't a need for the IF because the resulting code is the same as the one we test for in the name.

  If you needed to run a DIFFERENT code then you could use an IF to accomplish this.  Otherwise, it is redundant code for your purposes.

  In my original code I used your examples to demonstrate how this is possible:

User generated image
  My one question, is how the test.exe knows what file name to work on, how do you currently supply test.exe with the file name for each file?

  Would the actual call be something like this?:

CALL "C:\Path\To\TestCommand\Test.exe" 499 "C:\Path\To\File\ABC_499_ksdjuhfhaksdjhf.pdf"

Open in new window


  Furthermore, if you have to run this against a specific directory I noted you could get all of the file names using a loop directly instead of needing to enter them in variables.

  The below code allows you to point the script to a particular directory, find the names of every file and extract the code, and then call the text.exe command with the code for the file.

  However, how do you expect test.exe to be run against a particular file without supplying it to the command?  I imagine you would need to somehow.


REM Script: RunCMDbycode.cmd
REM Version: 1.2.0
@(
	SETLOCAL ENABLEDELAYEDEXPANSION
	ECHO OFF
	SET "_TestCMD=C:\Path\To\testcmd\test.exe"
	SET "_FileFolder=C:\Admin\Experts-Exchange\29051665\Files"
	SET "_FileGlob=*_*_*.pdf"
	SET "_eLvl=0"
)

CALL :Main

(
	ENDLOCAL
	EXIT /B %_eLvl%
)

:Main
	REM Loop through folders
	FOR %%S IN ("%_FileFolder%\%_FileGlob%") DO (
		FOR /F "tokens=2 delims=_" %%C IN ("%%~nS") DO (
			ECHO.File Full Path: = %%~fS
			ECHO.File Name: = %%~nxS
			ECHO.File Code: %%~C
			ECHO.Call Test.exe Command: CALL %_TestCMD% %%~C
		)
	
	)
GOTO :EOF

Open in new window


Here is an example of that code running, and the output.

User generated image
You would replace the paths set in the beginning with the ones relevant to your needs.
Avatar of chokka

ASKER

Thank you @Ben.. I will have to test the code...

Test.exe is a .Net Console Application .. which put an entry in the sql database...

Insert entry ..  in the Table..

Client Id : 399, 400, 401 are associated with each different clients...

In our incoming folder, we get all these pdf files .. for different clients.. and we load the details to our database ..

So .exe is a common file .. based on parameters passed to the exe file .. varies the data entered in the database.
how does the exe know what file you are associating?

Wouldn't you need to pass the name of the file being added to the exe so it can be associated int he database?

Sommethign like this:

CALL "C:\Path\To\TestCommand\Test.exe" 499 "C:\Path\To\File\ABC_499_ksdjuhfhaksdjhf.pdf"

Open in new window

Avatar of chokka

ASKER

yes, something like that.. due to public posting .. i didn't share all the details.. yes, you are right .. exe will have clientid, filename..

problem we are currently having .. is .. we load incorrect clientid for the filenames..

in order to avoid that, i need to write some condition .. hope your script logic will help me.. i need to test this tonight..
Avatar of chokka

ASKER

Clarification on this piece of syntax



REM Loop through folders
      FOR %%S IN ("%_FileFolder%\%_FileGlob%") DO (
            FOR /F "tokens=2 delims=_" %%C IN ("%%~nS") DO (
                  ECHO.File Full Path: = %%~fS
                  ECHO.File Name: = %%~nxS
                  ECHO.File Code: %%~C
                  ECHO.Call Test.exe Command: CALL %_TestCMD% %%~C
            )



I believe, i don't need to write ECHO in the real time .. unless, i need to pause and see..

This .exe file has only one parameter..  what if i need to add more parameters?

                  ECHO.Call Test.exe Command: CALL %_TestCMD% %%~C
Avatar of Bill Prew
Bill Prew

Did you see / test my suggestion posted earlier ?


»bp
Avatar of chokka

ASKER

@Bill, I am sorry .. I missed it. Looks like same For loop syntax. I will test both the script and will update the comments. Thanks !!
Hello Chokka,

  First off in response to this:

I believe, i don't need to write ECHO in the real time .. unless, i need to pause and see..

  Yes, the ECHOs are so you can test and see the logic in real-time so you could then edit the code and make it do what you wanted.

  the code as and only ECHOs and does not do the actual call because I wasn;t sure of the exact syntax of the executable you are calling.

This .exe file has only one parameter..  what if i need to add more parameters?

                  ECHO.Call Test.exe Command: CALL %_TestCMD% %%~C

So I'm not sure how you normally supply this syntax, which is why I was inquiring about this information previously.

Lets assume you just need to put in additional parameters after the file name

I assume the syntax for this is probably something like as follows:

CALL "<C:\Path\to\Command\YourCommand.exe>" <ClientID> "<C:\Path\to\File\FileName.pdf>" <Additional Parameter> <Additional Parameter> <Additional Parameter> <Additional Parameter>

Open in new window


I could put in a variable to hold all of these parameters if they all go before or after the other code.


Clarification on this piece of syntax



REM Loop through folders
      FOR %%S IN ("%_FileFolder%\%_FileGlob%") DO (
            FOR /F "tokens=2 delims=_" %%C IN ("%%~nS") DO (
                  ECHO.File Full Path: = %%~fS
                  ECHO.File Name: = %%~nxS
                  ECHO.File Code: %%~C
                  ECHO.Call Test.exe Command: CALL %_TestCMD% %%~C
            )
      )
REM The Outer For-Loop command Loops through files, this FOR Loop will look for every file in the given Filepath held in the variable "%_FileFolder%", and find every file that matches the glob "%_FileGlob%" which we defined as "*_*_*.pdf" Meaning it will match files in the pattern (Any characters UNDERSCORE any characters UNDERSCORE any characters .pdf)  ie "ABC_ClientID_AEFAWEF.PDF" or "xyz_ClientID_oiueflkkihsdsf.pdf" would be matched.


      FOR %%S IN ("%_FileFolder%\%_FileGlob%") DO (


REM The Inner For-Loop command takes the file name returned by the previous c=ommand and looks at only the name, it then looks for only the Client ID which sits between the two underscores, and puts that into a temporary variable to allow us to use it.


            FOR /F "tokens=2 delims=_" %%C IN ("%%~nS") DO (


REM This echos out the full file path:
                  ECHO.File Full Path: = %%~fS

REM This echos out just the file name and extention
                  ECHO.File Name: = %%~nxS

REM This echos out just the ClientID which we have found in the client name
                  ECHO.File Code: %%~C

REM This echos out an example of calling the command
                  ECHO.Call Test.exe Command: CALL %_TestCMD% %%~C

REM Note: This would actually call the command:
                  CALL "%_TestCMD%" %%~C

REM This closes the Inner For-Loop Command
            )

REM This closes the Outer For-Loop Command
      )

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial