gpt4 book ai didi

batch-file - 批处理文件循环跳过文件(如果名称包含)

转载 作者:行者123 更新时间:2023-12-04 13:32:59 33 4
gpt4 key购买 nike

我正在创建与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

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

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

我执行echo来显示要转换的文件,并且确实包含名称中带有Sample或sample的文件。

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

如果skip ==不做(rem做转换)

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

感谢您的时间。

最佳答案

尝试此操作,将转换命令放入循环中,如果输出正常,请在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

关于batch-file - 批处理文件循环跳过文件(如果名称包含),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16890205/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com