DOS批处理文件For循环运行两次 [英] DOS batch file For loop runs twice

查看:85
本文介绍了DOS批处理文件For循环运行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,该文件以递归方式将视频编码到子文件夹"encode",然后删除原始文件并将新文件移动到原始目录.我遇到的问题是,在对每个视频进行编码之后,for循环会拾取新编码的视频并再次运行.之后,它将继续播放下一个视频.我不确定为什么它运行两次而不是一次或无限次.我有什么误会?我是一个新手,所以很抱歉,这是一个简单的错误.

I have a batch file that recursively encodes videos to a sub folder "encode" then deletes the original and moves the new file to the original directory. The problem I'm having is that after each video is encoded, the for loop picks up the newly encoded video and runs again. After that, it moves on to the next video. I'm not sure why it runs twice instead of once or infinitely. What am I misunderstanding? I am a complete novice, so my apologies if it's a simple mistake.

@echo off
set /A count = 0
pushd %~dp0
for /R %%f in (*.mp2, *.mpg, *.vob, *.avi, *.wmv, *.mov, *.mp4, *.m4v, *.mpeg) do (
mkdir %%~dpf\encode
C:\HandBrakeCLI -i "%%f" -o "%%~dpf\encode%%~nf.mp4"
del "%%f"
move "%%~dpf\encode\%%~nf.mp4" %%~dpf
rmdir %%~dpf\encode
set /A count+=1
)
popd
echo Count is: %count%
pause

推荐答案

处理两次的原因是因为文件转换会创建另一个视频剪辑,该视频剪辑与 FOR 循环中的条件匹配.

The reason it processes twice is because the file conversion creates another video clip that matches the criteria in the FOR loop.

为避免处理文件夹结构和同步,解决此问题的一种简单方法是将进程分为两个循环,首先执行 *.mp4 文件:

To avoid dealing with folder structures and syncing, a simple way to get around this is to split the process into two loops, performing the *.mp4 files first:

@echo off
set /A count = 0
pushd %~dp0
for /R %%f in (*.mp4) do (
   mkdir %%~dpf\encode
   C:\HandBrakeCLI -i "%%f" -o "%%~dpf\encode%%~nf.mp4"
   del "%%f"
   move "%%~dpf\encode\%%~nf.mp4" %%~dpf
   rmdir %%~dpf\encode
   set /A count+=1
   )
for /R %%f in (*.mp2, *.mpg, *.vob, *.avi, *.wmv, *.mov, *.m4v, *.mpeg) do (
   mkdir %%~dpf\encode
   C:\HandBrakeCLI -i "%%f" -o "%%~dpf\encode%%~nf.mp4"
   del "%%f"
   move "%%~dpf\encode\%%~nf.mp4" %%~dpf
   rmdir %%~dpf\encode
   set /A count+=1
   )
popd
echo Count is: %count%
pause

别忘了从第二个循环的文件列表中删除 *.mp4 .

Don't forget to remove the *.mp4 from the second loop's file list.

编辑:这是一种更优雅的解决方案.

Edit: Here's a solution that's more elegant.

@echo off
set /A count = 0
pushd %~dp0
for %%e in (mp4 mp2 mpg vob avi wmv mov m4v mpeg) do (
   for /R %%f in (*.%%e) do (
      mkdir %%~dpf\encode
      C:\HandBrakeCLI -i "%%f" -o "%%~dpf\encode%%~nf.mp4"
      del "%%f"
      move "%%~dpf\encode\%%~nf.mp4" %%~dpf
      rmdir %%~dpf\encode
      set /A count+=1
   )
)
popd
echo Count is: %count%
pause

确保首先列出 mp4 ,这样一旦处理了其他文件类型,它就不会重新转换.

Make sure the mp4 is listed first so it's not reconverted once the other file types are processed.

这篇关于DOS批处理文件For循环运行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆