簡體   English   中英

在Windows中使用批處理從for循環中讀取文件

[英]Reading from a file in a for loop with batch in windows

我正在嘗試編寫一個批處理程序,它將通過目錄樹進行搜索,找到無法播放的視頻文件並將其刪除。 一切似乎都正常,我運行ffmpeg並將其輸出發送到txt文件。 當我從txt文件讀取內容時,它第一次運行良好。 第一個之后的所有讀取均不正確,並且似乎復制了先前讀取的結果。

@echo off
setlocal enabledelayedexpansion 
set ext=.avi .mkv .mp4
for /R %%i in (*.*) do ( 
    for %%a in (%ext%) do (
        if "%%a" == "%%~xi" (
            echo found video file "%%i", scanning integrity...
            ffmpeg.exe -v error -i "%%i" -f null - >output.txt 2>&1
            set /p error= < output.txt
            if "!error!" == "" (
                echo +video is good
            ) else (
                echo -video is bad... deleting it
                echo "(delete here)"
            )

        )
    )
)
pause

指出了所有這些內容之后,我就可以大大簡化我的代碼了:

@echo off
setlocal enabledelayedexpansion 
for /R %%i in (*.avi *.mkv *.mp4) do ( 
    echo found video file "%%i", scanning integrity...
    ffmpeg.exe -v error -i "%%i" -f null -
    if "!errorlevel!" == "0" (
        echo +video is good
    ) else (
        echo -video is bad... deleting it
        echo "(delete here)"
    )
)
pause

由於重現您的問題並不容易,我建議

        ffmpeg.exe -v error -i "%%i" -f null - >output.txt 2>&1
        SET "ERROR="
        set /p error= < output.txt

set /p的問題在於, 沒有輸入會使變量保持不變 ,它不會設置為none

你檢查了ffmpeg之后的errorlevel的值嗎? 如果它遵循的規則, errorlevel應該是0上的錯誤和成功非0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM