0

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)

sjoy
  • 502
  • 1
  • 4
  • 15
inovasyon
  • 460
  • 3
  • 12
  • Can you explain a little more what you want to do with each group of files, and what xxxxx is? And if you didn't say "drag" what word would you use? – sjoy Nov 29 '14 at 02:49
  • I have 5 different file convert tool each of them supports different types of files. I'm trying to make batch file to send my file to correct converter regarding to it's extension. The xxxx is just writtent to mention that list goes on. And there is no alternative to word drag since I will drag files over the batch file "to let bat file get the name as a variable". Thanks. – inovasyon Nov 29 '14 at 02:55
  • We can say that, they are completely folder independent. The files can be dragged from any folder in any path. And the processed file is only the file which dragged and dropped. There is no multiple file processing. Simply the only thing about file is the way using it's extension. My main matter is how to select the correct action regarding to a 5 different extension list. – inovasyon Nov 29 '14 at 03:05
  • Sorry misunderstanding. You want to literally drag a folder and drop it into the batch file, then the batch file processes each file based on extension. – sjoy Nov 29 '14 at 03:10
  • Ah no. There is no folder and nothing about a folder. I will just drag&drop a file. – inovasyon Nov 29 '14 at 03:17
  • Sorry for "folder brain." See answer. – sjoy Nov 29 '14 at 06:39

3 Answers3

0

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
sjoy
  • 502
  • 1
  • 4
  • 15
  • Regarding to your explanation, seems pretty flexible to handle the issue. But it's a bit advanced for me to edit and personalize. Until I'll be better in batch commands, I'm following simple solutions like all amateurs :) Thanks for your interest and help. – inovasyon Nov 29 '14 at 17:08
0
FOR %%a in (%cadfile%) do (
    if /i "%~x1"=="%%a" some_action "%~1"
)

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

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

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.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108