How to choose one of multiple actions based on file extension, in batch

183 Views Asked by At

I'm an amateur on the usage of the FOR command. I need a batch file that will run one of 5 file conversion tools based on a file's extension. I want to drop a file onto the batch file icon and have it converted.

Since my list is huge, I can't use nested IF's.

What I've tried so far:

@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
)
)

The %%~x1 variable is used for file extension of file, which dragged and dropped over the batch file. (edited to make more clear)

3

There are 3 best solutions below

1
On BEST ANSWER
FOR %%a in (%cadfile%) do (
    if /i "%~x1"=="%%a" some_action "%~1"
)

... and follow the bouncing ball for the rest of the utilities/lists

1
On

I think this will work for you. It looks through all your groups of extensions in a single For loop and when the matching extension is found, calls a label where you can do the conversion and any related tasks. You'll need to finish the "groupN" variables and labels.

@echo off
SETLOCAL EnableDelayedExpansion

    set file="%1"
    set ext=%~x1

    :: Set the 5 groups of extensions that have different converters
    set group1=.dwg, .dxf, .dwf
    set group2=.ps, .eps, .epi, .epsp

    For %%A in (1 2 3 4 5) do (
        set groupnum=group%%A
        call set thisgroup=%%!groupnum!%%
                :: Look for extension in this group
                echo.!thisgroup!|findstr /i /C:"%ext%" >nul 2>&1
                    if not errorlevel 1 call :group%%A
                    :: else go loop next group
    )
    echo Extension not found in any group &pause &goto end

:group1
    echo group1 file to convert is %file%
    goto end
:group2
    echo group2 file to convert is %file%
    goto end

:end
pause
exit
0
On

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...

@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:

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

For further explanations on array concept, see this post.