【问题标题】:Windows cmd batch loop commandWindows cmd 批处理循环命令
【发布时间】:2013-01-25 09:03:22
【问题描述】:

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

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 中等等。

win cmd 目录的 for 循环...

【问题讨论】:

  • 使用 /f 开关,这里有很多关于 SO 的示例。
  • @PreetSangha /f 无济于事
  • @PreetSangha 你最好给一些代码....或者谷歌什么
  • 因为 /f 不起作用

标签: batch-file cmd imagemagick


【解决方案1】:

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

cd my
for /d %%d in (*) do (
  for %%f in ("%%~d\*.jp2") do (
    convert "%%~ff" -quality 25 "%%~dpf\jpg\%%~nf.jpg"
  )
)

【讨论】:

  • +1,不需要CD。你可以使用for /d %%d in (my\*) do ...
  • 我在 .bat 中需要这个?还有为什么你的比 Preet Sangha 解决方案更好? (通过它的解决方案,我在一小时后得到内存已满错误)
  • 我的代码应该进入批处理文件,是的。如果您想直接从命令提示符运行它,您必须将%% 替换为%,并且可能在每个语句之前添加@ 以避免命令回显:for /d %d in ("my\*") do @for %f in ("%~d\*.jp2") do @convert "%~ff" -quality 25 "%~dpf\jpg\%~nf.jpg"。我也不会说我的解决方案本身比@PreetSangha 建议的更好。不过,它确实避免来回切换目录,因为它使用的是绝对路径。
【解决方案2】:

在更高的目录试试这个

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

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

【讨论】:

  • 令牌错误(意外出现)(在 cmd 中运行命令)
  • 和你最后一次编辑也许会很好......但我有十亿张jpg)
【解决方案3】:

或者,您可以使用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 文件的文件夹,则此解决方案可能不适合您。

【讨论】:

  • 递归循环可能会处理 OP 不想处理的孙文件夹中的文件。
  • 一般来说,是的,谢谢,但是 OP 的文件夹结构和处理命令的参数行似乎都表明孙文件夹中不应该有 *.jp2 文件。不过,我可能会添加一个关于 /R 开关的实际效果的明确说明。
【解决方案4】:

以下将起作用

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

【讨论】:

  • 不,不会。 %f 不是有效变量,%str1% 将包含文件的绝对路径,脚本将(尝试)处理所有子目录中的所有文件,而不仅仅是扩展名为 .jp2 的文件。
猜你喜欢
  • 1970-01-01
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多