Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   [решено] Помогите организовать циклическую обработку файлов (http://forum.oszone.net/showthread.php?t=332852)

interestingyy 29-01-2018 15:41 2794393

Помогите организовать циклическую обработку файлов
 
привет друзья! помогите мне организовать обработку файлов, которые указываю в переменных %file1%-%file40% в виде цикала .. ну знаете как в PHP есть цикл FOR со счетчиком и прочими атрибутами и он перебирает все значения эллементов массива и выполняет над ними те операции что вам нужно..

помогите плиз
вобщем вот мой код.. он очень грамоздкий.. надо в цикл это все дело заключить,чтобы циклически обрабатывать все 40 файлов, что указаны в переменных file1-file40

@echo off
setlocal enabledelayedexpansion

set ext=avi
set bit_rate=133K

set dir_vid_scr=C:\Users\yura\Downloads\Video\



set file1=video1
set file2=video2
set file3=video3
set file4=video4
set file5=video5
set file6=video6
set file7=video7
set file8=video8
set file9=video9
set file10=video01
set file11=video11
set file12=video12
set file13=video13
set file14=video14
set file15=video15
set file16=video16
set file17=video17
set file18=video18
set file19=video19
set file20=video20
set file21=video21
set file22=video22
set file23=video23
set file24=video24
set file25=video25
set file26=video26
set file27=video27
set file28=video28
set file29=video29
set file30=video30
set file31=video31
set file32=video32
set file33=video33
set file34=video34
set file35=video35
set file36=video36
set file37=video37
set file38=video38
set file39=video39
set file40=video40

cd "%dir_vid_scr%"



if /i exist "%file1%.%ext%" (goto :recompress_file1) else (
echo "%file1%.%ext%" NOT EXISTS!
goto :if_after_file1_exists )

:if_after_file1_exists
if /i exist "%file2%.%ext%" (goto :recompress_file2) else (
echo "%file2%.%ext%" NOT EXISTS!
goto :if_after_file2_exists )

:if_after_file2_exists
if /i exist "%file3%.%ext%" (goto :recompress_file3) else (
echo "%file3%.%ext%" NOT EXISTS!
goto :if_after_file3_exists )

:if_after_file3_exists
if /i exist "%file4%.%ext%" (goto :recompress_file4) else (
echo "%file4%.%ext%" NOT EXISTS!
goto :if_after_file4_exists )

:if_after_file4_exists
if /i exist "%file5%.%ext%" (goto :recompress_file5) else (
echo "%file5%.%ext%" NOT EXISTS!
goto :if_after_file5_exists )

:if_after_file5_exists
if /i exist "%file6%.%ext%" (goto :recompress_file6) else (
echo "%file6%.%ext%" NOT EXISTS!
goto :if_after_file6_exists )

:if_after_file6_exists
if /i exist "%file7%.%ext%" (goto :recompress_file7) else (
echo "%file7%.%ext%" NOT EXISTS!
goto :if_after_file7_exists )

:if_after_file7_exists
if /i exist "%file8%.%ext%" (goto :recompress_file8) else (
echo "%file8%.%ext%" NOT EXISTS!

и т.д до 40
........................
.........................
.........................

:if_after_file37_exists
if /i exist "%file38%.%ext%" (goto :recompress_file38) else (
echo "%file38%.%ext%" NOT EXISTS!
goto :if_after_file38_exists )

:if_after_file38_exists
if /i exist "%file39%.%ext%" (goto :recompress_file39) else (
echo "%file39%.%ext%" NOT EXISTS!
goto :if_after_file39_exists )

:if_after_file39_exists
if /i exist "%file40%.%ext%" (goto :recompress_file40) else (
echo "%file40%.%ext%" NOT EXISTS!
goto :exit )







:recompress_file1
echo Processing file1 '%file1%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file1%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file1%_rcm.%ext%
echo Checking video duration for '%file1%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file1%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file1=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file1%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file1=%%i

echo original file duration: %dur_orig_file1% sec.
echo recompressed file duration: %dur_rcm_file1% sec.
if %dur_orig_file1% == %dur_rcm_file1% (
echo GREAT. files have same duration. Will remove original video '%file1%.%ext%'
del "%file1%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :if_after_file1_exists

:recompress_file2
echo Processing file2 '%file2%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file2%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file2%_rcm.%ext%
echo Checking video duration for '%file2%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file2%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file2=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file2%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file2=%%i

echo original file duration: %dur_orig_file2% sec.
echo recompressed file duration: %dur_rcm_file2% sec.
if %dur_orig_file2% == %dur_rcm_file2% (
echo GREAT. files have same duration. Will remove original video '%file2%.%ext%'
del "%file2%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :if_after_file2_exists

:recompress_file3
echo Processing file3 '%file3%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file3%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file3%_rcm.%ext%
echo Checking video duration for '%file3%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file3%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file3=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file3%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file3=%%i

echo original file duration: %dur_orig_file3% sec.
echo recompressed file duration: %dur_rcm_file3% sec.
if %dur_orig_file3% == %dur_rcm_file3% (
echo GREAT. files have same duration. Will remove original video '%file3%.%ext%'
del "%file3%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :if_after_file3_exists

:recompress_file4
echo Processing file4 '%file4%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file4%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file4%_rcm.%ext%
echo Checking video duration for '%file4%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file4%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file4=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file4%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file4=%%i

echo original file duration: %dur_orig_file4% sec.
echo recompressed file duration: %dur_rcm_file4% sec.
if %dur_orig_file4% == %dur_rcm_file4% (
echo GREAT. files have same duration. Will remove original video '%file4%.%ext%'
del "%file4%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :if_after_file4_exists

:recompress_file5
echo Processing file5 '%file5%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file5%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file5%_rcm.%ext%
echo Checking video duration for '%file5%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file5%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file5=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file5%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file5=%%i

echo original file duration: %dur_orig_file5% sec.
echo recompressed file duration: %dur_rcm_file5% sec.
if %dur_orig_file5% == %dur_rcm_file5% (
echo GREAT. files have same duration. Will remove original video '%file5%.%ext%'
del "%file5%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :if_after_file5_exists




и т.д до 40

--------------------------
-----------------------------
-----------------------------




:recompress_file39
echo Processing file39 '%file39%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file39%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file39%_rcm.%ext%
echo Checking video duration for '%file39%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file39%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file39=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file39%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file39=%%i

echo original file duration: %dur_orig_file39% sec.
echo recompressed file duration: %dur_rcm_file39% sec.
if %dur_orig_file39% == %dur_rcm_file39% (
echo GREAT. files have same duration. Will remove original video '%file39%.%ext%'
del "%file39%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :if_after_file39_exists

:recompress_file40
echo Processing file40 '%file40%.%ext%' in folder '%dir_vid_scr%'
echo.
ffmpeg -loglevel error -i %file40%.%ext% -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% %file40%_rcm.%ext%
echo Checking video duration for '%file40%.%ext%'
echo.

for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file40%.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_orig_file40=%%i
for /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %file40%_rcm.%ext% -of default=noprint_wrappers=1:nokey=1"') do set dur_rcm_file40=%%i

echo original file duration: %dur_orig_file40% sec.
echo recompressed file duration: %dur_rcm_file40% sec.
if %dur_orig_file40% == %dur_rcm_file40% (
echo GREAT. files have same duration. Will remove original video '%file40%.%ext%'
del "%file40%.%ext%"
echo removed ok.
)
echo ----------------------------------------------------------------
echo.
goto :exit







:exit
echo PLEASE GOOD BYE!



megaloman 29-01-2018 17:37 2794415

Я тупо переписал Ваш код, так как у меня нет Ваших программ, поэтому серьёзно не тестировалось
Код:

@Echo Off
cls

Set "ext=avi"
Set "bit_rate=133K"
Set "dir_vid_scr=Z:\Users\yura\Downloads\Video"

Set /A Nbeg=0
For /F "usebackq delims=:" %%i In (`"FINDSTR /B /L /I /N /C:":FilesSpisok" "%~dpnx0""`) DO Set /A Nbeg=%%i
If %Nbeg% EQU 0 Echo !!! No file list found &Pause &Exit /B 240

If Not Exist "%dir_vid_scr%" Echo !!! No folder found "%dir_vid_scr%" &Pause &Exit /B 240

Pushd "%dir_vid_scr%"

For /F "usebackq tokens=*" %%i In (`"more +%Nbeg% "%~dpnx0""`) DO (
        If /I "%%i"=="GoTo :Eof" GoTo :Cont
        If Exist "%%i.%ext%" (
                Call :Recompress "%%i.%ext%"
        ) else (
                Echo: &Echo !!! File "%dir_vid_scr%\%%i.%ext%" NOT EXISTS!
                Echo ----------------------------------------------------------------
        )
)
:Cont
Popd

Echo PLEASE GOOD BYE!
Pause
GoTo :Eof

:Recompress
        Echo: &Echo +++ Processing file %1 in folder "%dir_vid_scr%"

        ffmpeg -loglevel error -i %1 -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% "%~n1_rcm%~x1"

        Echo *** Checking video duration for %1
       
        For /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %1 -of default=noprint_wrappers=1:nokey=1"') Do Set "dur_orig_file=%%i"
        For /f %%i in ('"ffprobe.exe -v error -show_entries format=duration "%~n1_rcm%~x1" -of default=noprint_wrappers=1:nokey=1"') Do Set "dur_rcm_file=%%i"

        Echo *** original file duration:    %dur_orig_file% sec.
        Echo *** recompressed file duration: %dur_rcm_file% sec.
       
        If "%dur_orig_file%"=="%dur_rcm_file%" (
                Echo +++ GREAT. files have same duration. Will remove original video %1
                Del %1 &&Echo *** removed ok.
        )
        Echo ----------------------------------------------------------------

GoTo :Eof

:FilesSpisok

video1
video2
video3
video4
video5
video6

GoTo :Eof

Укажите свой путь и свой список файлов
Мне не очень нравится идея указывать список файлов - это трудоёмко. Быть может нужно обработать все файлы по маске в указанной папке?
Имена video1 ... video40 условные или реальные?

interestingyy 30-01-2018 00:50 2794470

megaloman, ОГРОМНОЕ ВАМ СПАСИБО ЗА ВАШИ ТРУДЫ!!! ПРОСТО ОЧЕНЬ БЛАГОДАРЕН!!! МАЛО КТО В НАШИ ДНИ ПРОСТО ТАК РАДИ ДОБРА И ПОМОЩИ ЧТО-ТО ДЕЛАЕТ!!!! Я БЫ САМ НЕ СМОГ БЫ НАПИСАТЬ ТАКОЙ КОМПАКТНЫЙ КОД КАК У ВАС ВЫШЕЛ!! и тот свой код и так несколько дней сам писал))

Цитата:

Цитата megaloman
Имена video1 ... video40 условные или реальные? »

Конечно же это условные имена..
реальные имена файлов могут содержать латиницу(обоих регистров, символы пробела, запятой, нижнего подчеркивания, тире и цифры 0..9). к примеру, реальные имена имеют след. вид
Скрытый текст

451P_895K_63403191.avi
fk_653_242k_46547292.avi
bolshaya-igra-2017.flv
voyna-2017.flv

вот примерно такие имена..

Цитата:

Цитата megaloman
Мне не очень нравится идея указывать список файлов - это трудоёмко. »

Абсолютно с вами согласен! это трудоемко!

Цитата:

Цитата megaloman
Быть может нужно обработать все файлы по маске в указанной папке? »

Это был бы самый идеальный вариант! если это возможно.. то хотелось бы, чтобы автоматически в указанной папке обрабатывались все файлы, имена которых заканчиваются на префикс _ready и имеют расширение, указанное в переменной %ext%

сейчас протестирую ваш код и отпишусь.. но думаю он вполне рабочий!))

interestingyy 30-01-2018 07:36 2794488

Цитата:

Цитата interestingyy
сейчас протестирую ваш код и отпишусь.. но думаю он вполне рабочий!)) »

все работает просто отменно!! БОЛЬШОЕ ВАМ СПАСИБО! ЭТО МАГИЯ!))

только можно еще счетчик к слову File добавить вот в этистроки
Код:

.......

Echo: &Echo +++ Processing file %1 in folder "%dir_vid_scr%"

...........
Echo: &Echo !!! File "%dir_vid_scr%\%%i.%ext%" NOT EXISTS!


..........

Чтобы выводилось..
Код:

Processing file 1 "file_name.ext" in folder "src_to_folder"
 Processing file 2 "file_name.ext" in folder "src_to_folder"
 Processing file 3 "file_name.ext" in folder "src_to_folder"

!!! File 1 "src_to_folder\file_name.ext" NOT EXISTS!
!!! File 2 "src_to_folder\file_name.ext" NOT EXISTS!
!!! File 3 "src_to_folder\file_name.ext" NOT EXISTS!

все супер!!!! Сенкс!!!!

megaloman 30-01-2018 11:40 2794523

можно еще счетчик
Код:

@Echo Off
cls

Set "ext=avi"
Set "bit_rate=133K"
Set "dir_vid_scr=Z:\Users\yura\Downloads\Video"

Set /A Nbeg=0
For /F "usebackq delims=:" %%i In (`"FINDSTR /B /L /I /N /C:":FilesSpisok" "%~dpnx0""`) DO Set /A Nbeg=%%i
If %Nbeg% EQU 0 Echo !!! No file list found &Pause &Exit /B 240

If Not Exist "%dir_vid_scr%" Echo !!! No folder found "%dir_vid_scr%" &Pause &Exit /B 240

Pushd "%dir_vid_scr%"

Set /A Count=0
For /F "usebackq tokens=*" %%i In (`"more +%Nbeg% "%~dpnx0""`) DO (
        If /I "%%i"=="GoTo :Eof" GoTo :Cont
        Call :Recompress "%%i.%ext%"
)
:Cont
Popd

Echo PLEASE GOOD BYE!
Pause
GoTo :Eof

:Recompress
        Set /A Count+=1
        Echo: &Echo +++ Processing %Count% file %1 in folder "%dir_vid_scr%"
        If Not Exist "%dir_vid_scr%\%~1" (
                Echo !!! File "%dir_vid_scr%\%~1" NOT EXISTS!
                Echo ----------------------------------------------------------------
                GoTo :Eof
        )

        ffmpeg -loglevel error -i %1 -c:v libx264 -c:a copy -b:v %bit_rate% -maxrate %bit_rate% -bufsize %bit_rate% "%~n1_rcm%~x1"

        Echo *** Checking video duration for %1
       
        For /f %%i in ('"ffprobe.exe -v error -show_entries format=duration %1 -of default=noprint_wrappers=1:nokey=1"') Do Set "dur_orig_file=%%i"
        For /f %%i in ('"ffprobe.exe -v error -show_entries format=duration "%~n1_rcm%~x1" -of default=noprint_wrappers=1:nokey=1"') Do Set "dur_rcm_file=%%i"

        Echo *** original file duration:    %dur_orig_file% sec.
        Echo *** recompressed file duration: %dur_rcm_file1% sec.
       
        If "%dur_orig_file%"=="%dur_rcm_file%" (
                Echo +++ GREAT. files have same duration. Will remove original video %1
                Del %1 &&Echo *** removed ok.
        )
        Echo ----------------------------------------------------------------

GoTo :Eof

:FilesSpisok

video1
video2
video3
video4
video5
video6

GoTo :Eof


interestingyy 30-01-2018 11:55 2794531

можно было и не писать весь код!) спасибо большое!

megaloman 30-01-2018 12:16 2794536

хотелось бы, чтобы автоматически в указанной папке обрабатывались все файлы, имена которых заканчиваются на префикс _ready и имеют расширение, указанное в переменной %ext%
Код:

@Echo Off
cls

Set "ext=avi"
Set "bit_rate=133K"
Set "dir_vid_scr=Z:\Users\yura\Downloads\Video"
Set "Suff=_ready"
rem Set "Suff="

If Not Exist "%dir_vid_scr%" Echo !!! No folder found "%dir_vid_scr%" &Pause &Exit /B 240

Set /A Count=0

Pushd "%dir_vid_scr%"
For /F "usebackq tokens=*" %%i In (`2^>nul Dir "*%Suff%.%ext%" /B /A:-D`) DO Call :Recompress "%%i"
Popd

Echo PLEASE GOOD BYE!
Pause
GoTo :Eof

ИМХО, использовать суффикс тоже не фэншуйно, трудоёмко переименовывать. Легче (ИМХО) забросить все готовые файлы в какую-то папочку, и там их все обработать. В этом случае укажите суффикс Set "Suff=" Естесвенно, из предыдущего кода возьмите процедуру :Recompress
Цитата:

Цитата interestingyy
можно было и не писать весь код!) »

Обратите внимание, и первая часть скрипта и процедура :Recompress по сравнению с первоначальным, был изменён, легче показать конечный результат, чем объяснить все изменения.

interestingyy 30-01-2018 12:58 2794547

megaloman, Ок! понял вас! Спасибо вам огромное за ваши огромные труды! Я бы сам точно не справился!) А вы тут магию сотворили!)
Класс! это мне значительно сократит работу!


Время: 21:23.

Время: 21:23.
© OSzone.net 2001-