DosTips.com

A Forum all about DOS Batch
It is currently 22 Nov 2017 12:37

All times are UTC-06:00




Post new topic  Reply to topic  [ 9 posts ] 
Author Message
PostPosted: 28 Nov 2014 22:19 
Offline

Joined: 28 Nov 2014 22:16
Posts: 22
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.


Top
   
PostPosted: 28 Nov 2014 22:42 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3626
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


Top
   
PostPosted: 28 Nov 2014 23:03 
Offline

Joined: 28 Nov 2014 22:16
Posts: 22
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.


Top
   
PostPosted: 28 Nov 2014 23:21 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3626
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.


Top
   
PostPosted: 28 Nov 2014 23:29 
Offline

Joined: 28 Nov 2014 22:16
Posts: 22
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 ;)


Top
   
PostPosted: 29 Nov 2014 01:53 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
Multi-post http://stackoverflow.com/questions/2719 ... n-in-batch


Top
   
PostPosted: 29 Nov 2014 11:18 
Offline

Joined: 28 Nov 2014 22:16
Posts: 22
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.


Top
   
PostPosted: 30 Nov 2014 00:21 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
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.


Top
   
PostPosted: 30 Nov 2014 02:17 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1460
Location: México City, México
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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 9 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited