【问题标题】:How to choose one of multiple actions based on file extension, in batch如何根据文件扩展名批量选择多个操作之一
【发布时间】:2014-11-29 02:34:54
【问题描述】:

我是 FOR 命令用法的业余爱好者。我需要一个批处理文件,它将根据文件的扩展名运行 5 个文件转换工具之一。我想将一个文件放到批处理文件图标上并进行转换。

由于我的列表很大,我不能使用嵌套的 IF。

到目前为止我已经尝试过:

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

%%~x1 变量用于文件的文件扩展名,它拖放到批处理文件上。 (为更清楚而编辑)

【问题讨论】:

  • 你能多解释一下你想对每组文件做什么,xxxxxx是什么?如果你不说“拖动”,你会用什么词?
  • 我有 5 种不同的文件转换工具,每一种都支持不同类型的文件。我正在尝试制作批处理文件以将我的文件发送到有关其扩展名的正确转换器。 xxxx 只是为了提到这个列表还在继续。并且没有其他选择,因为我会将文件拖到批处理文件上“让 bat 文件将名称作为变量”。谢谢。
  • 我们可以说,它们完全独立于文件夹。可以从任何路径中的任何文件夹中拖动文件。处理后的文件只是拖放的文件。没有多文件处理。关于文件的唯一事情就是使用它的扩展名的方式。我的主要问题是如何针对 5 个不同的扩展列表选择正确的操作。
  • 抱歉误会。您想从字面上拖动一个文件夹并将其放入批处理文件中,然后批处理文件根据扩展名处理每个文件。
  • 啊,不。没有文件夹,也没有关于文件夹的内容。我只会拖放一个文件。

标签: batch-file multiple-conditions


【解决方案1】:
FOR %%a in (%cadfile%) do (
    if /i "%~x1"=="%%a" some_action "%~1"
)

...然后跟随弹跳球查看其余的实用程序/列表

【讨论】:

  • 所以他们会为他们的 5 个分机组中的每一个重复这个命令?
【解决方案2】:

我认为这对你有用。它在单个 For 循环中查看所有扩展组,当找到匹配的扩展时,调用一个标签,您可以在其中进行转换和任何相关任务。您需要完成“groupN”变量和标签。

@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

【讨论】:

  • 关于你的解释,处理这个问题似乎很灵活。但对我来说编辑和个性化有点先进。在我在批处理命令方面做得更好之前,我会像所有业余爱好者一样遵循简单的解决方案:) 感谢您的关注和帮助。
【解决方案3】:

以下方法允许您轻松添加和修改扩展/应用程序列表。请注意,您只需要编辑放置在第一个 FOR 命令中的值;该程序的其余部分是您不需要关心的解决方案...

@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 defined ext[%~x1] !ext[%~x1]! %1

有关数组概念的进一步解释,请参阅this post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 2012-01-08
    • 2014-08-10
    • 2010-09-13
    • 2017-06-05
    • 2010-10-07
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多