Title: Batch script to read from text file and create incrementing number for filenames
Tags: command-line;ffmpeg;batch
Question: I'm making a batch script where I download videos using ffmpeg.
First it needs to read download codes from a text file (codes.txt)
Then it checks the current directory if the same file exists, if it exists it increments a new filename.
Then it starts to download, however, I wasn't able to make these bunch of codes to work together.
(The incrementing method works but without the &quot;read from txt file&quot; code, if I combine the two it results to a blank filename. I think it's because the looping code is not compatible.)
Read (loop) from txt file code:
@echo off
set prefix=title-
set resolution=720p
set extension=mkv
for /F &quot;tokens=*&quot; %%A in (code.txt) do (
title Downloading &quot;%prefix%.%extension%&quot; @ %resolution%
&quot;%~dp0ffmpeg.exe&quot; -loglevel info -stats -i &quot;https://somesite.com/%%A&amp;quality=%resolution%&quot; -i &quot;https://somesite.com&quot; -headers &quot;Origin: https://somesite.come&quot; -c copy -disposition:s:0 default -bsf:a aac_adtstoasc &quot;%prefix%.%extension%&quot;
and using the incrementing method I found from link:
@echo off
setlocal EnableDelayedExpansion
SET &quot;ffmpeg=your_path_to\ffmpeg.exe&quot;
SET &quot;output_folder=D:\rendered_files&quot;
SET &quot;extension=mov&quot;
SET &quot;prefix=Scene_&quot;
rem do not set this, just define it empty:
SET &quot;output_path=&quot;
call :get_output_path &quot;%output_folder%&quot; %prefix% %extension%
rem here goes your ffmpeg command line where %1 is input file and &quot;%output_path%&quot; is your destination
&quot;%ffmpeg%&quot; -i %1 ..... your filters end such ..... &quot;%output_path%&quot;
echo Done
endlocal &amp; echo press any key to exit ... &amp; pause&gt;nul &amp; goto :eof
:get_output_path <output folder&gt; <prefix&gt; <extension&gt;
SET &quot;i=0&quot;
SET &quot;formated_value=000&quot;
SET &quot;output_path=%~1\%~2%formated_value%.%3&quot;
if exist &quot;%output_path%&quot; call :increase&amp; goto :loop
goto :eof
:increase
SET /a &quot;i=%i%+1&quot;
SET &quot;value=000000%i%&quot;
SET &quot;formated_value=%value:~-3%&quot;
goto :eof
Combined batch script:
@echo on
setlocal EnableDelayedExpansion
set prefix=showabc-
set resolution=720p
set extension=mkv
SET &quot;output_folder=%~dp0Downloads&quot;
for /F &quot;tokens=*&quot; %%A in (code.txt) do (
SET &quot;output_path=&quot;
call :get_output_path &quot;%output_folder%&quot; %prefix% %extension%
echo File &quot;%prefix%%formated_value%.%extension%&quot; will be saved at &quot;%output_folder%&quot;.
title Downloading &quot;%prefix%.%extension%&quot; @ %resolution%
&quot;%~dp0ffmpeg.exe&quot; -loglevel info -stats -i &quot;https://somesite.com/%%A&amp;quality=%resolution%&quot; -i &quot;https://somesite.com&quot; -headers &quot;Origin: https://somesite.com&quot; -c copy -disposition:s:0 default -bsf:a aac_adtstoasc &quot;%output_path%&quot;
:get_output_path <output folder&gt; <prefix&gt; <extension&gt;
SET &quot;i=1&quot;
SET &quot;formated_value=001&quot;
SET &quot;output_path=%~1\%~2%formated_value%.%3&quot;
if exist &quot;%output_path%&quot; call :increase&amp; goto :loop
goto :eof
:increase
SET /a &quot;i=%i%+1&quot;
SET &quot;value=000000%i%&quot;
SET &quot;formated_value=%value:~-3%&quot;
goto :eof
This is the result I always get:
Screenshot
It's either &quot;.mkv&quot; or filename without incrementing numbers. :((
Sorry for the very long post. I hope someone can help me, thanks y'all! Stay safe!
Comment: You have delayed expansion enabled, but the counter appears to not use it. If I recall correctly, you will want to use `!` (the BANG char) instead of `%` (percent char) See for example: https://superuser.com/questions/1569594


Here is another answer: @echo off
cd /d &quot;%~dp0&quot; &amp;&amp; setlocal enabledelayedexpansion
for /f tokens^=* %%a in ('type .\code.txt&quot;')do call %:^) i &quot;%%~a&quot;
<con: rem./ &amp; %__AppDir__%timeout.exe /t -1 &amp; endlocal &amp; goto=:EOF
<con: set /a &quot;_%~1+=1+1000&quot;
if exist &quot;%~dp0downloads\showabc-!_%~1:~-3!.mkv&quot; &amp;&amp; goto %:^)
<con: title <nul &amp;&amp; title downloading &quot;showabc-!_i:~-3!.mkv&quot; @ 720p
echo\file &quot;showabc-!_%~1:~-3!.mkv&quot; will be saved at &quot;%~dp0downloads&quot;.
&quot;%~dp0ffmpeg.exe&quot; -i &quot;https://somesite.com/%~2&amp;quality=720p&quot; -i &quot;https://somesite.com&quot; ^
-headers &quot;origin: https://somesite.com&quot; -c copy -disposition:s:0 default -bsf:a ^
aac_adtstoasc -loglevel info -stats &quot;%~dp0downloads\showabc-!_%~1:~-3!.mkv&quot;
I'm sorry, but I removed your variables to try to understand your code and not make mistakes in applying them.
If I don't understand what you want to do, I hope you will signal me where I lost the north (plz) ...
The code below I will leave here as an attempt to help you, where in your future comments, with bigger and more accurate information, (me or other operators here), we can understand and propose a better solution for your case....
Obs.: 1 We don't know the contents of your code.txt file
Obs.: 2 I'm assuming your ffmpeg commands are correct
Obs.: 3 Goto :EOF goes straight to the End of the File, and so, immediately ending the execution, inside your :label
Obs.: 4 Use exit /b if you don't want to end the execution immediately, thus, your increment should work without premature interruptions ...

{'source': 'StackExchangeDataset', 'subset': 'superuser.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:09/30/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<command-line><ffmpeg><batch>', 'Title': 'Batch script to read from text file and create incrementing number for filenames', 'ViewCount': 428, 'id': 1589820, 'user_AboutMe': None, 'user_AccountId': 19631147.0, 'user_DisplayName': 'rubyjane1313', 'user_Reputation': 1.0, 'user_Views': 8.0, 'user_id': 1224548.0}}