Page 1 of 1
Batch file with multiple conditions
Posted: 28 Nov 2014 22:19
by karaziki
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.
Re: Batch file with multiple conditions
Posted: 28 Nov 2014 22:42
by Squashman
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
Re: Batch file with multiple conditions
Posted: 28 Nov 2014 23:03
by karaziki
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.
Re: Batch file with multiple conditions
Posted: 28 Nov 2014 23:21
by Squashman
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.
Re: Batch file with multiple conditions
Posted: 28 Nov 2014 23:29
by karaziki
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

Re: Batch file with multiple conditions
Posted: 29 Nov 2014 01:53
by foxidrive
Re: Batch file with multiple conditions
Posted: 29 Nov 2014 11:18
by karaziki
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.
Re: Batch file with multiple conditions
Posted: 30 Nov 2014 00:21
by foxidrive
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.
Re: Batch file with multiple conditions
Posted: 30 Nov 2014 02:17
by Aacini
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