Help with the FOR command, get current iteration number

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
landuchi
Posts: 4
Joined: 26 Jun 2008 15:14

Help with the FOR command, get current iteration number

#1 Post by landuchi » 17 Jun 2010 21:55

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

landuchi
Posts: 4
Joined: 26 Jun 2008 15:14

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

#2 Post by landuchi » 17 Jun 2010 22:18

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

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

#3 Post by avery_larry » 18 Jun 2010 11:54

You can actually use the delayedexpansion that you have enabled:

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

landuchi
Posts: 4
Joined: 26 Jun 2008 15:14

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

#4 Post by landuchi » 18 Jun 2010 17:52

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

Post Reply