1

我有一个这样的文件夹结构:

my/
 1/
  jpg/
 2/
  jpg/
 3/
  jpg/

etc.

现在我在所有文件夹中手动运行它(比如 1,2,3(在其中我有 jp2 文件要转换))我的 imagemagick 命令:

for %f in (*.jp2) do (convert %f -quality 25 jpg/%~nf.jpg)

但是我如何编写批处理文件或将用完my文件夹并在循环中输入、压缩为 jpg、先输入1、然后输入2等的批处理文件。

只是for循环用于win cmd的目录...

4

4 回答 4

3

我可能会使用这样的东西:

cd my
for /d %%d in (*) do (
  for %%f in ("%%~d\*.jp2") do (
    convert "%%~ff" -quality 25 "%%~dpf\jpg\%%~nf.jpg"
  )
)
于 2013-02-10T00:32:35.300 回答
0

在更高的目录中试试这个

for /f "tokens=*"  %f  in ('dir /b/s *.jp2') do pushd "%~pf" & convert "%f" -quality 25 %~nf.jpg & popd

其他选项:

此外,增强了 FOR 变量引用的替换。您现在可以使用以下可选语法:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

请记住在批处理文件中使用 %% 而不是 %。

于 2013-02-09T22:47:13.930 回答
0

或者,您可以使用FOR /R循环,它是FOR递归的变体:

for /r "d:\root" %I in (*.jp2) do (convert "%I" -quality 25 "%~dpI\jpg\%~nI.jpg")

以上内容d:\root应替换为您的my\目录的路径。

请注意,您不必CD进入根目录。上面的命令可以从任何位置正常运行,并且只处理/R切换后指定的目录。

另请注意,该/R开关指示 FOR 循环处理整个目录树。如果树中可能包含您想要跳过的 .jp2 文件的文件夹,则此解决方案可能不适合您。

于 2013-02-10T00:22:52.507 回答
-1

以下将起作用

FOR /F "tokens=* delims=" %%G IN ('DIR /b /s /a-d  my') DO CALL :FileName "%%G"

Exit /b

:FileName
set str1=%~1
convert %f -quality 25 jpg/%str1%.jpg

Exit /b
于 2013-02-11T21:00:02.773 回答