批处理文件循环跳过文件,如果名称包含

时间:2013-06-03 04:54:36

标签: batch-file mp4 avi handbrake

我正在创建这个与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转换,只是跳过它。

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

我尝试过使用find或findstr,但两者都未能设置skip to yes

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

我只想转换非预告片/样本avi文件。

感谢您的时间。

5 个答案:

答案 0 :(得分:7)

尝试此操作,将转换命令放入循环中,如果输出正常,请在handbrakecli之前删除单词echo

@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

答案 1 :(得分:3)

这应该有效:

@echo off
FOR /R "%somepath%" %%G in (*.avi) DO (
echo "%%~nG" |findstr /i "trailer sample">nul || (
  rem do conversion
 )
)

答案 2 :(得分:2)

很难看到你的帖子在哪里是伪代码以及实际代码的位置。

第一个样本只包含REM个语句,所以它显然什么都不做也就不足为奇了。

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

if %errorlevel% equ 0 set skip=Yes

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

HOWEVER此语法仅可用于OUTSIDE的{​​{1}} - 即多指令语句(括在括号中)或级联&& ampersands。首先批量"block statement"一个完整的陈述 - 从PARSESFOR到适当的右括号,if执行它。作为THEN阶段的一部分,任何PARSING - 包括%var%都会被其值替换为整个语句%errorlevel%时的值 - 而不是因为它更改到期对parsed

的操作

为了在更改时使用该值,您需要使用

for

其中if errorlevel 1 (do_something) else (do_something_else) do_something本身可能是复合语句。

OR

do_something_else)

其中变量是否已定义

OR

if defined variable (do_something) else (do_something_else)

OR

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 ) 回显文件名,并查找“sample”或“trailer”FINDSTR不区分大小写。如果找到任一目标字符串,Findstr设置errorlevel 0,否则设置为1 - 并且/i语法对循环内的if errorlevel x的动态值起作用。

答案 3 :(得分: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 %USERPROFILE%\OneDrive\Desktop\Vids\

REM Set mp4PATH to the root folder you wish to recursively search.
SET "mp4PATH=%USERPROFILE%\OneDrive\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 %%A IN ("%~1") DO (
    SET "Folder=%%~dpA"
    SET "Name=%%~nxA"
)

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

答案 4 :(得分: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%