批处理文件循环跳过文件(如果名称包含) [英] Batch File Loop Skip File if name contains

查看:327
本文介绍了批处理文件循环跳过文件(如果名称包含)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating this batch file, that works with handbrakecli, to batch convert avi to mp4.

但是,我陷入了如何继续循环并在循环内跳过当前文件的问题.

However I am stuck in how to continue the loop and skip the current file inside a loop.

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
)

这目前在跳过包含预告片或样本的文件中不起作用

This currently doesn't work in skipping the files that contain trailer or sample

我尝试使用find或findstr,但都无法跳过.

I have tried using find or findstr and both fail to skip.

    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转换,而只是跳过它.

If a file contains either trailer or sample, I do not want to do any handbrakecli conversions, but to just skip it.

我会执行echo命令以显示要转换的文件,并且确实包含名称中包含Sample或sample的文件.

I do echo's to display which files get converted, and it does include files with Sample or sample in the name.

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

I have tried using find or findstr and both fail to set skip to yes

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

if skip == No do ( rem do conversion )

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

I only want to convert non-trailer/sample avi files.

谢谢您的时间.

推荐答案

尝试一下,将转换命令放入循环中,如果输出正常,则在handbrakecli之前删除单词echo:

try this, put your conversion commands in the loop and remove the word echo before handbrakecli if the output is OK:

@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!"中.

The file name+file path is in the variable "!fpath!".

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

Added some code concerning the needs of the 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

这篇关于批处理文件循环跳过文件(如果名称包含)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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