Page 1 of 1

Help with the FOR command, get current iteration number

Posted: 17 Jun 2010 21:55
by landuchi
Hello!

I am trying to write a little batch file to process video files with ffmpeg.

I would like to be able to know the progress of the process, ie "Processing file 2 of 23". Below is the code I have so far, but all I get in the window tittle is... "Processing file 0 of 23". I can't get the value of %currentfile% to change while the for is running.

Any suggestions?
Thanks in advance

Code: Select all

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

Set extensionList= *.mpg *.mov *.wmv *.avi
Set ffmpegParameters= -vcodec flv -b 250k -t 30

SET currentfile=0
for /f "tokens=1* delims=." %%i in ('dir /b %extensionList%') do (
SET /A "currentfile+=1"
CALL:setTitle "Processing file %currentfile% of %filecount%"
ffmpeg.exe -i  "%%i.%%j" %ffmpegParameters% -y "%%i_sm.flv"
)

:countFiles -- Counts the number of files that match the extension list
SET filecount=0
for /f "tokens=* delims=" %%i in ('dir /b %extensionList%') do (
   SET /A "filecount+=1"
)
EXIT /b

:setTitle title -- Changes window title
::   -- title [string]   - Title to show
TITLE %~1
EXIT /b

Re: Help with the FOR command, get current iteration number

Posted: 17 Jun 2010 22:18
by landuchi
Searching the forums, I found the subroutine trick, updated the batch and it's working now :D.

Anyway is there a simpler way of knowing the current iteration number other than using SET /A variable+=1 ?

Corrected code below in case someone finds it useful.

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

REM.-- Extensions to procesar
Set extensionList= *.mpg *.mov *.wmv *.avi

REM.-- Conversion parameters
Set ffmpegParameters= -vcodec flv -b 250k -t 30

CALL:countFiles
SET currentfile=0

for /f "tokens=1* delims=." %%i in ('dir /b %extensionList%') do (
CALL:doProgress
ffmpeg.exe -i  "%%i.%%j" %ffmpegParameters% -y "%%i_sm.flv"
)
GOTO:EOF

:: FUNCTIONS ::::::::::::::::::::::::::::::::::::::::

:countFiles -- Counts the number of files to process
SET filecount=0
for /f "tokens=* delims=" %%i in ('dir /b %extensionList%') do (
   SET /A "filecount+=1"
)
EXIT /b

:doProgress -- Shows process progress
SET /A "currentfile+=1"
CALL:setTitle "Processing file %currentfile% of %filecount%"
EXIT /b

:setTitle title -- Changes window title
::   -- title [string]   - title to show
TITLE %~1
EXIT /b

Re: Help with the FOR command, get current iteration number

Posted: 18 Jun 2010 11:54
by avery_larry
You can actually use the delayedexpansion that you have enabled:

CALL:setTitle "Processing file !currentfile! of %filecount%"

Re: Help with the FOR command, get current iteration number

Posted: 18 Jun 2010 17:52
by landuchi
I didn't know what delayed expansion was for... I just graved it from the script examples. After reading about it, I realize the solution was in the very first line of my script.

Thank you for the reply.

Updated code below:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

REM.-- Extensions to process
Set extensionList= *.mpg *.mov *.wmv *.avi

REM.-- Conversion parameters
Set ffmpegParameters= -vcodec flv -b 250k -t 30

CALL:countFiles
SET currentfile=0

for /f "tokens=1* delims=." %%i in ('dir /b %extensionList%') do (
SET /A "currentfile+=1"
CALL:setTitle "Processing file !currentfile! of %filecount%"
ffmpeg.exe -i  "%%i.%%j" %ffmpegParameters% -y "%%i_sm.flv"
)
GOTO:EOF

:: FUNCTIONS ::::::::::::::::::::::::::::::::::::::::

:countFiles -- Counts the number of files to process
SET filecount=0
for /f "tokens=* delims=" %%i in ('dir /b %extensionList%') do (
   SET /A "filecount+=1"
)
EXIT /b

:setTitle title -- Changes window title
::   -- title [string]   - title to show
TITLE %~1
EXIT /b