6

我正在创建这个与 handbrakecli 一起使用的批处理文件,以将 avi 批量转换为 mp4。

但是我被困在如何继续循环并跳过循环内的当前文件。

FOR /R "%somepath%" %%G in (*.avi) DO (

rem skip if filename contains word trailer

rem skip if file name contains word sample

rem do conversion
)

这目前不适用于跳过包含预告片或样本的文件

我尝试过使用 find 或 findstr 并且都无法跳过。

    echo "%%G" | c:\windows\system32\findstr /i "trailer" > NUL
    If %ERRORLEVEL% EQU 1 set skip Yes

这是示例。

    echo "%%G" | c:\windows\system32\findstr /i "sample" > NUL
    If %ERRORLEVEL% EQU 1 set skip Yes

如果文件包含预告片或样本,我不想进行任何 handbrakecli 转换,而只是跳过它。

我做 echo 来显示哪些文件被转换了,它确实包含名称中带有 Sample 或 sample 的文件。

我尝试使用 find 或 findstr 并且都无法将 skip 设置为 yes

如果跳过 == 不做(rem 做转换)

我只想转换非预告片/示例 avi 文件。

感谢您的时间。

4

5 回答 5

8

试试这个,把你的转换命令放在循环中,echo如果输出正常,删除 handbrakecli 之前的单词:

@echo off &setlocal
FOR /R "%somepath%" %%G in (*.avi) DO (
    set "fpath=%%G"
    set "fname=%%~nG"
    setlocal enabledelayedexpansion
    if "!fname!"=="!fname:trailer=!" if "!fname!"=="!fname:sample=!" (
        echo handbrakecli.exe "!fpath!" &rem put your conversion  command here
        >>"logfile.log" echo !fname!
    )
    endlocal
)

文件名+文件路径在变量中"!fpath!"

添加了一些有关 OP 需求的代码:

@echo off &setlocal
rem replace avi with mp4 files in my movie folder
rem grab 4 random folders with avi in them and no mp4

rem Settings for this Batch File
set "moviepath=H:\Movies"
set "logfile=C:\Documents and Settings\%USERNAME%\LogFiles\avi_converter.log"

rem check if log file exists
if not exist "%logfile%" echo(>"%logfile%"

rem create empty convert file
copy nul "convert_movies.bat" >nul 2>&1

rem add echo off
echo @echo off >>"convert_movies.bat"

rem set counter
SET /A COUNT=1

FOR /R "%moviepath%" %%G in (*.avi) DO (
    set "fpath=%%~fG"
    set "fname=%%~nG"
    setlocal enabledelayedexpansion

    rem check if count greater than 4
    if !COUNT! gtr 4 goto:eof

    if "!fname!"=="!fname:trailer=!" if "!fname!"=="!fname:sample=!" (
        rem echo handbrakecli.exe "!fpath!" &rem put your conversion  command here

            rem Send File To HandBrakeCLI
            CALL :DOHandBrakeCLI "!fpath!"

            rem Delete File
            CALL :DeleteOldFile "!fpath!"

            rem Add Log Entry
            CALL :LogEntry "!fpath!"

            rem add line break space
            echo( >>"convert_movies.bat"

            endlocal
            rem increment counter
            SET /A COUNT+=1

    ) else endlocal  
)
rem end main program, to close cmd window replace it with EXIT
goto:eof

:DOHandBrakeCLI
rem skip if the parameter is empty
IF "%~1"=="" goto:eof
For %%A in ("%~1") do (
    Set "Folder=%%~dpA"
    Set  "Name=%%~nxA"
)
rem echo %Folder%%Name%
echo start /b "" "c:\handbrakecli\HandBrakeCLI.exe" -i "%~1" -o "%Folder%%~n1.mp4" --preset="High Profile">>"convert_movies.bat"
exit /b

:DeleteOldFile
rem skip if the parameter is empty
IF "%~1"=="" goto:eof
For %%A in ("%~1") do (
    Set "Folder=%%~dpA"
    Set "Name=%%~nxA"
)
rem sends parameters to deletefile which will make sure new file exists before deleting old one
echo c:\projects\deletefile.bat "%~1" "%Folder%%~n1.mp4">>"convert_movies.bat"
exit /b

:LogEntry
rem skip if the parameter is empty
IF "%~1"=="" goto:eof
echo "%~1">>"%logfile%"
exit /b
于 2013-06-03T05:21:17.300 回答
3

这应该有效:

@echo off
FOR /R "%somepath%" %%G in (*.avi) DO (
echo "%%~nG" |findstr /i "trailer sample">nul || (
  rem do conversion
 )
)
于 2013-06-03T07:13:42.463 回答
2

很难看出您的帖子在哪里是伪代码以及实际代码在哪里。

第一个示例仅包含REM语句,因此它显然什么也不做也就不足为奇了。

您的第二个和第三个样本实际上是相同的 - 唯一的区别是目标字符串。skip变量未设置为不足为奇,Yes因为正确的语法是

if %errorlevel% equ 0 set skip=Yes

您发布的语法将REPORT未定义skip- 它忽略Yes

HOWEVER此语法仅可用于OUTSIDEa "block statement"- 即多指令语句(括在括号中)或级联&by&&ersands。首先批处理PARSES一个完整的语句 - 从FORif到适当的右括号并THEN执行它。作为PARSING阶段的一部分,any %var%-include%errorlevel%被替换为整个语句时的值,而parsed不是由于for.

为了在值发生变化时使用它,您需要使用

if errorlevel 1 (do_something) else (do_something_else)

wheredo_somethingdo_something_else)可能本身是复合语句。

或者

if defined variable (do_something) else (do_something_else)

变量是否已定义

或者

setlocal enabledelayedexpansion
....
if !errorlevel! equ x (do_something) else (do_something_else)

或者

 if !var! neq something (do_something) else (do_something_else)

但很有可能

FOR /R "%somepath%" %%G in (*.avi) DO (
 echo(%%G|findstr /i "sample trailer" >nul
 if errorlevel 1 echo %%G
)

会给你一个合适的骨架。

通过回显文件名并查找不区分大小写FINDSTR的“sample”或“trailer” 。/i如果找到任一目标字符串,Findstr 设置错误级别 0,否则设置为 1 -语法适用于循环内if errorlevel x的动态值。errorlevel

于 2013-06-03T06:12:08.667 回答
1
@ECHO on &SETLOCAL

REM This script was inspired by Endoro's expanded script
(https://stackoverflow.com/a/16891696/10572786).

REM This batch script will recursively search for all .mp4 files that don't
have (x265) in the file name. Any valid results will be encoded with x265
using FFmpeg. The original .mp4 file will remain unchanged in it's original
folder with the new x265 version.

REM Example: %PATH%\A.mp4 > %PATH%\A(x265).mp4

REM If you don't have ffmpeg.exe on your PC you must download or build it
with Microsoft Visual Studios. I recommend you download and run media
autobuild suite on GitHub: (https://github.com/jb-alvarado/media- 
autobuild_suite).

REM Once ffmpeg is compiled/downloaded make sure to set it's folder path as
an environmental variable in Windows before running the script. Change the
script's working directory to your .mp4 files root folder using the "cd" 
command.

REM !!BEGIN SCRIPT!!

cd /d %USERPROFILE%\Desktop\Vids\
REM or perhaps use [cd /d %OneDrive%\Desktop\Vids]

REM Set mp4PATH to the root folder you wish to recursively search.
SET "mp4PATH=%USERPROFILE%\Desktop\Vids\"

REM Create empty convert file.
COPY NUL "convert_movies.bat" >NUL 2>&1

REM Add ECHO off.
ECHO @ECHO off >>"convert_movies.bat"

REM Recursively search root folder.
FOR /R "%mp4PATH%" %%G IN (*.mp4) DO (
    SET "fpath=%%~fG"
    SET "fname=%%~nG"
    SETLOCAL enabledelayedexpansion

REM Ignore all files that have "(x265)" in the file name.
IF "!fname!"=="!fname:*(x265)=!" (
    CALL :DO_FFmpeg_CLI "!fpath!"
    ECHO(>>"convert_movies.bat"
        ) ELSE ENDLOCAL
    )
)
GOTO:EOF

REM CALL variables for use in FFmpeg's command line.
:DO_FFmpeg_CLI
IF "%~1"=="" GOTO:EOF
FOR %%I IN ("%~1") DO (
    SET "Folder=%%~dpI"
    SET "Name=%%~nxI"
)

REM Export info to "convert_movies.bat and run ffmpeg.exe's command line in the cmd.exe window.
ECHO ffmpeg -y -i "%~1" -c:v libx265 -preset slow -crf
18 -c:a aac "%Folder%%~n1(x265).mp4">>"convert_movies.bat" && ffmpeg |
ffmpeg -y -i "%~1" -c:v libx265 -preset slow
-crf 18 -c:a aac "%Folder%%~n1(x265).mp4"
EXIT /B
PAUSE
于 2018-10-29T05:59:41.423 回答
0

下面的批处理文件解决了原始问题并将转换文件的数量限制为给定数量(原始问题中没有出现):

@echo off
setlocal EnableDelayedExpansion
rem Insert in the next line the list of files to skip
set skip=/trailer/sample/
set count=0
FOR /R "%somepath%" %%G in (*.avi) DO (
   if /I "!skip:/%%~nG/=!" equ "%skip%" (
      echo Current file name is not in skip variable
      echo Do conversion on: %%G
      set /A count+=1
      if !count! equ 20 goto :endLoop
   )
)
:endLoop
echo Converted files: %count%
于 2013-06-03T08:22:02.823 回答