Batch file with multiple conditions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Batch file with multiple conditions

#1 Post by karaziki » 28 Nov 2014 22:19

First I would like to explain that I'm amateur on usage of FOR condition.
I have about 50 pieces of file extension list seperated in 6 groups. And 6 different codes to execute for each group.

I would like to drag and drop a file over the batch file and want bat to execute correct code regarding to file extension.

What I tried so far is;

Code: Select all

@ECHO OFF

SET cadfile=.dwg .dxf .dwf
SET gsfile=.ps .eps .epi .epsp
SET xxxxxx=.xx .xx and goes on

FOR %%~x1 in (%cadfile%) do (
    Do some action
FOR %%~x1 in (%gsfile%) do (
    Do some other action
)
)


Note: U used the %%~x1 variable to define file extension. Normally working fine but failed in FOR
Thanks in advance.

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

Re: Batch file with multiple conditions

#2 Post by Squashman » 28 Nov 2014 22:42

Code: Select all

@ECHO OFF

SET cadfile=.dwg .dxf .dwf
SET gsfile=.ps .eps .epi .epsp

FOR %%G in (%cadfile%) do IF /I "%~x1"=="%%~G" echo do something
FOR %%G in (%gsfile%) do IF /I "%~x1"=="%%~G" echo do something

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Batch file with multiple conditions

#3 Post by karaziki » 28 Nov 2014 23:03

My first test working like a charm, thank you Squashman
By the way this is for converting files to import editor. If you're interedted in vector or raster design, I'll be glad to share result for you.

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

Re: Batch file with multiple conditions

#4 Post by Squashman » 28 Nov 2014 23:21

karaziki wrote:My first test working like a charm, thank you Squashman
By the way this is for converting files to import editor. If you're interedted in vector or raster design, I'll be glad to share result for you.

Your welcome. I don't have much use for that though. I do Big Data processing on mainframes. But if I ever get over my fear of traveling I will look you up over there across the Atlantic. Weather must be nice this time of year where you are from.

karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Batch file with multiple conditions

#5 Post by karaziki » 28 Nov 2014 23:29

Me, complicated actually.

My origin is Turkey but I choosed to live in Morocco due to great climate and calm environment. I'm living with design and web design. Despite of you I love to travel, even insane enough to prefer a country even I can not speak it's native languages ;)


karaziki
Posts: 22
Joined: 28 Nov 2014 22:16

Re: Batch file with multiple conditions

#7 Post by karaziki » 29 Nov 2014 11:18

Thank you Foxidrive to try to help and googled for my question
By the way, I'm impressed about google's indexing reaction.
Anyway, I already got the answer here in most simple and straight way.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file with multiple conditions

#8 Post by foxidrive » 30 Nov 2014 00:21

karaziki wrote:Thank you Foxidrive to try to help and googled for my question


You have the wrong end of the stick - I didn't search for your question in a search engine.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch file with multiple conditions

#9 Post by Aacini » 30 Nov 2014 02:17

I just posted my solution in S.O! :?

The following method allows you to easily add and modify your list of extensions/applications. Please note that you just need to edit the values placed inside the first FOR command; the rest of the program is the solution you don't need to care of...

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Define the list of extensions per application:
rem (this is the only part that you must edit)
for %%a in ("cadfile=.dwg .dxf .dwf"
            "gsfile=.ps .eps .epi .epsp"
            "xxxxxx=.xx .xx1 .xx2") do (

   rem The rest of the code is commented just to be clear,
   rem but you may omit the reading of this part if you wish

   rem Separate application from its extensions
   rem and create a vector called "ext" with an element for each pair
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      rem For example: %%b=cadfile, %%c=.dwg .dxf .dwf
      for %%d in (%%c) do set "ext[%%d]=%%b"
      rem For example: set "ext[.dwg]=cadfile", set "ext[.dxf]=cadfile", set "ext[.dwf]=cadfile"
      rem In the next line: set "ext[.ps]=gsfile", set "ext[.eps]=gsfile", etc...
   )
)

rem Now process the extension of the file given in the parameter:
if defined ext[%~x1] goto !ext[%~x1]!
echo There is no registered conversion tool for %~x1 extension
goto :EOF

:cadfile
echo Execute cadfile on %1 file
rem cadfile %1
goto :EOF

:gsfile
echo Execute gsfile on %1 file
rem gsfile %1
goto :EOF

etc...

If each conversion tool is executed in the same way and don't require additional parameters (just the filename), then you may omit the individual sections and directly execute the conversion tools this way:

Code: Select all

if defined ext[%~x1] !ext[%~x1]! %1

For further explanations on array concept, see this post.

Antonio

Post Reply