I am trying to rename all the files present in a Windows directory using FOR command as follows at the command prompt:
for %1 in (*.*) do ren %1 test%1
E.g. This renames a file enc1.ctl to testenc1.ctl enc2.ctl to testenc2.ctl
Thats not what i want. What i want is
enc1.ctl renamed to test1.ctl enc2.ctl renamed to test2.ctl
How do i do that?
#Akelunuk:
Thanks, that w kind of works but i have files names as
h263_enc_random_pixels_1.ctl , h263_enc_random_pixels_2.ctl which i want to rename to
test1.ctl and test2.ctl respectively
Then how?
I've got it!
for %1 in (.) do ren %1 t%1
and then:
ren tenc*.* test*.*
If you know the number of files, (say 10), you can use
for /L %1 in (1,1,10) do ren enc%1.ctl test%1.ctl
I am not sure if it is possible in batch, but then again I never mastered this primitive language... :-P
If CMD isn't mandatory, but you can't use a good file renamer, you can do that with WSH:
var path= "E:/tmp";
var fso = WScript.CreateObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(path);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext())
{
var file = files.item();
var fileName = file.Name;
var p = /^enc(\d+)\.ctl$/.exec(fileName);
if (p != null)
{
var newFileName = "test" + p[1] + ".ctl";
// Optional feedback
WScript.echo(fileName + " -----> " + newFileName);
file.Move(newFileName);
}
}
Of course, put that in a file.js
I actually tested with file.Copy(file.ParentFolder + "/SO/" + newFileName); to avoid loosing files...
HTH.
This renames all files in directory for filter file types with PREFIX and today's date and time
#echo ON
cls
for %%a in (*.pdf) do (set myfiledate=%%~ta echo !myfiledate!)
echo Date format = %myfiledate%
echo dd = %myfiledate:~0,2%
echo mm = %myfiledate:~3,2%
echo yyyy = %myfiledate:~6,4%
echo.
echo Time format = %myfiledate%
echo hh = %myfiledate:~11,2%
echo mm = %myfiledate:~14,2%
echo AM = %myfiledate:~17,2%
echo.
echo Timestamp = %myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%
ECHO "TEST..." > "test-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%.txt"
PAUSE
This echos successfuly the date modified and time as a postfix but doesnt parse the info into the rename. I cant figure out why, but it is very close. Maybe someone cant tweak to suit your purpose.
#echo ON
setlocal
cls
for %%a in (*.pdf) do (set myfiledate=%%~ta echo !myfiledate!)
:DATETIME
echo Date format = %myfiledate%
echo dd = %myfiledate:~0,2%
echo mm = %myfiledate:~3,2%
echo yyyy = %myfiledate:~6,4%
echo Time format = %myfiledate%
echo hh = %myfiledate:~11,2%
echo mm = %myfiledate:~14,2%
echo AM = %myfiledate:~17,2%
= %myfiledate:~17,2%
echo.
echo Timestamp = %myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%
ECHO "TEST..." > "test-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%.txt"
for /f "delims=" %%a in ('dir *.pdf /t:a /a:-d /b /s') do call :RENAME "%%a"
:RENAME
REM for /f "tokens=1-6 delims=/ " %%a in ('dir %%a /t:w^|find "/"') do (
ren %%a "3DC-test-OFF-ELE-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%~x1")
PAUSE
GOTO :EOF
Related
I usually like to try to work these out myself, but at the moment I have to admit I don't know where to start with this one. Hoping someone could kindly steer me in the right direction at least.
I have a folder with a number of .txt files
Text1.txt
Text2.txt
Text3.txt
In my windows bat file I need to list the contents of said folder and set them as options to be set as variables.
example:
cls
echo[
echo[ Please select an option
echo[
echo (1) Text1
echo (2) Text2
echo (3) Text3
echo[
set /p option=Type your selection (1-3) and press ENTER=
if !option!==1 set var=Text1
if !option!==2 set var=Text2
if !option!==3 set var=Text3
Any advice is greatly appreciated, this forum has been great.
*Edit
here is something I tried
cls
echo[
echo[ Please select an option
echo[
dir /b "*.txt"
echo[
set /p option=Type your selection (1-3) and press ENTER=
if !option!==1 set var=text1
if !option!==2 set var=text2
if !option!==3 set var=text3
it works, but does not add the numbers (1) before the options, and it also has them aligned left not centred.
Please select an option
text1.txt
text2.txt
text3.txt
Type your selection (1-3) and press ENTER=
There are different solutions you can build, here are 2 examples.
If you have only a few files you can utilize choice:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=1,*delims=[]" %%i in ('dir /b /a:-d *.txt ^| find /v /n ""') do (
echo %%i. %%j
set "cnt=!cnt!%%i"
set "fchoice%%i=%%~j"
)
choice /c %cnt% /m "Choose"
echo you chose !fchoice%errorlevel%!
if you have many files though, choice might not be a viable option, then revert to set /p:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=1,*delims=[]" %%i in ('dir /b /a:-d *.txt ^| find /v /n ""') do (
echo %%i. %%j
set "cnt=!cnt!%%i"
set "fchoice%%i=%%~j"
set "fin=%%i"
)
set /p "chosen=Select a file by number: "
for /l %%i in (1,1,%fin%) do if "%chosen%" == "%%i" set "check=1"
if not defined check echo Incorrect choice selected & goto :EOF
echo you chose !fchoice%chosen%!
Well, if this were any other language what would you do? Probably populate an array with your directory listing, right? Then use that array to pair menu option with file choice? Well, don't let the fact that the Batch language doesn't have arrays stop you. They're easy enough to simulate.
#echo off & setlocal
rem // init array index
set file.length=0
rem // for each .txt file in the current directory
for %%I in (*.txt) do (
rem // It's good practice only to enable delayed expansion when needed, as
rem // otherwise it can mangle values containing exclamation marks
setlocal enabledelayedexpansion
rem // use a "for /f" command to endlocal while reading the value of
rem // !file.length!. This preserves exclamation marks in file names.
for /f %%# in ("!file.length!") do endlocal & set "file[%%~#]=%%~I"
rem // set /a doesn't require delayed expansion. It just works.
set /a file.ubound = file.length, file.length += 1
)
rem // Display the collection of variables named file...something.
set file
There you go. The last line should show you a list of all the variables beginning with file, including the simulated .length and .ubound properties. From there, just use a for /L %%I in (0, 1, %file.ubound%) to display your menu.
Here's a bit more. Firstly, I read your comment explaining that the number of txt files could exceed 50. By default, the Windows cmd console is 24 lines. Wouldn't it be nice to columnify the output so the user doesn't have to scroll? I wrote a utility script that will take a flat list and columnify it based on the number of rows in the current cmd console. Save this as...
columnify.bat:
#if (#CodeSection == #Batch) #then
#echo off & setlocal
if "%~1"=="test" (
rem // this errors if there's redirected input buffer waiting
timeout /t 0 /nobreak >NUL 2>NUL && (
echo Usage: command list output ^| %~nx0
echo or %~nx0 ^< txtfile containing a list
echo;
echo Hit Ctrl-C to exit.
)
exit
)
for /f "usebackq tokens=1,2 delims=," %%I in (
`powershell "(Get-Host).UI.RawUI.WindowSize.toString()"`
) do (
rem // detect whether input buffer has content waiting
start /b cmd /c "%~f0" test
cscript /nologo /e:JScript "%~f0" %%I %%J
)
goto :EOF
#end // end Batch / begin JScript hybrid code
var stdin = WSH.CreateObject('Scripting.FileSystemObject').GetStandardStream(0).ReadAll();
cols = WSH.Arguments(0) * 1 - 1,
rows = WSH.Arguments(1) * 1 - 4;
var out = stdin.split(/\r?\n/), buffer = [], maxlen, col = 0;
if (rows > out.length) { rows = out.length; }
while (out.length) {
buffer[col] = [];
maxlen = 0;
while (buffer[col].length < rows) {
val = out.length ? out.shift() : '';
buffer[col].push(val);
if (maxlen < val.length) maxlen = val.length;
}
for (var i = buffer[col].length; i--;) {
while (buffer[col][i].length < maxlen) { buffer[col][i] += ' '; }
}
col++;
}
for (var i=0; i < buffer[0].length; i++) {
var line = [];
for (var j=0; j < col; j++) {
line.push(buffer[j][i]);
}
WSH.Echo(line.join(' ').substr(0, cols));
}
Now your main script can make use of columnify.bat by piping output through it.
#echo off & setlocal
rem // All the same stuff as above, but without the comments.
set file.length=0
for %%I in (*.txt) do (
setlocal enabledelayedexpansion
for /f %%# in ("!file.length!") do endlocal & set "file[%%~#]=%%~I"
set /a file.ubound = file.length, file.length += 1
)
cls
:displaymenu
echo Always press your luck. Which file do you choose?
echo;
( for /L %%I in (0,1,%file.ubound%) do #(
call echo %%I: %%file[%%~I]%%
) ) | columnify
echo;
set /P "choice=Enter a number: "
if %choice% GEQ 0 if %choice% LEQ %file.ubound% (
setlocal enabledelayedexpansion
echo You chose !file[%choice%]!. Neat.
endlocal
) else (
cls
powershell "write-host -f red 'Invalid response. Enter a number between 0 and %file.ubound%.'"
goto displaymenu
)
I want to read the file name with the command and automatically move it to the folder with the same name. What should I do?
example:
before processing
after processing
If I have files and folders with the same name, I want to move them to a folder with the same name.
What should I do? Do I have a cmd command?
I will provide both a solution to this exact question, and an alternative that I would suggest to handle this situation.
==========
~ Solution ~
==========
Copy/Paste the code below into an empty file, save, and execute.
#echo off
for /f "tokens=1,* delims=_" %%a in ('dir /b *.xlsx') do (
if not "%%a_%%b"=="%~nx0" (
if not exist %%a mkdir %%a
move "%%a_%%b" "%%a\"
)
)
You can change the "*.xlsx" to any other file extension, or change to "*.*" to work with ANY file extension.
Keep in mind though, this ONLY works with filenames that are formatted like the way you mentioned in your question.
===================
~ Alternative Solution~
===================
I would suggest the Shell Extension "Files 2 Folder" as an alternative. I came across a situation where I needed something similar to what you're asking a couple years ago, and this ended up working out great.
https://www.dcmembers.com/skwire/download/files-2-folder/
Here is another approach using regex in vbscript with a batch file :
#echo off & color 0A
Title Extract Title using Regex in vbscript
SetLocal EnableDelayedExpansion
#for /f "delims=" %%a in ('dir /b *.xlsx') do (
Call :Extract_Title "%%a" Title
If Defined Title (
If Not Exist "!Title!\" MkDir "!Title!\"
Move /-Y "%%a" "!Title!\"
)
)
Pause & Exit
::----------------------------------------------------------------------------------------
:Extract_Title <InputFile> <Title to be Set>
>"%tmp%\%~n0.vbs" (
echo WScript.StdOut.WriteLine Extract_Title(Data^)
echo Function Extract_Title(Data^)
echo Data = Wscript.Arguments(0^)
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "(\S+|\S.+)_"
echo For Each Match in re.Execute(Data^)
echo Title = Match.SubMatches(0^)
echo Next
echo Extract_Title = Title
echo End Function
)
#for /f "delims=" %%A in ('cscript /nologo "%tmp%\%~n0.vbs" "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------------
I'm trying to loop through all .lss files in a folder, and grab a string that exists between two tags, save that value and rename the file using that string.
Example:
42982934829.lss -> contains string:
<surveyls_title><![CDATA[J.3200-1118 - Project Title]]></surveyls_title>
Rename to `J.3200-1118 - Project Title.lss`
Here is what I have so far, but I fear my syntax is badly incorrect..
#Echo off
Set Folder=X:\RenameTest
Set Files=*.lss
PushD %Folder%
For %%A in (%Files%) Do For /f %%B IN (
'findstr "<surveyls_title>.*</surveyls_title>" "%ProjectTitle%"'
) Do Call :Rename ..
PopD
Goto :Eof
:Rename
Echo Ren %1 "%ProjectTitle%"
I suppose this is what you want.
#echo off
pushd "X:\RenameTest"
for %%a in (*.lss) do for /f "tokens=3*delims=[]" %%i in ('type "%%a" ^| find "</surveyls_title>"') do echo ren "%%~a" "%%~i%%~xa"
popd
Just remove echo from the line once you are happy with the printed results.
You can also extract the title using Regex : Demo Here
#echo off & color 0A
Title Extract Title using Regex
Set "InputFile=42982934829.lss"
Call :Extract_Title "%InputFile%" Title
Echo %Title%
Pause & Exit
::----------------------------------------------------------------------------------------
:Extract_Title <InputFile> <Title to be Set>
(
echo WScript.StdOut.WriteLine Extract_Title(Data^)
echo Function Extract_Title(Data^)
echo Data = WScript.StdIn.ReadAll
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "\[CDATA\[(.*?)\]\]"
echo For Each Match in re.Execute(Data^)
echo Title = Match.SubMatches(0^)
echo Next
echo Extract_Title = Title
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%A in ('cscript /nologo "%tmp%\%~n0.vbs" ^< "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------------
Trying to make a script to unlock all locked files inside a folder, by using Windows' handle.exe. But when I split the output the filename value is .... weird (all other values are Ok).
The sample output of the handle.exe is this:
REM perl.exe pid: 12532 type: File PCNAME\UserName 144: C:\dev\massunlocker\Eula.txt
REM a perl.exe
REM b pid:
REM c 12532
REM d type:
REM e File
REM f PCNAME\UserName
REM g 144:
REM h C:\dev\massunlocker\Eula.txt
So, from this I need c, g, and h.
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,2,3,4,5,6,7,8 delims= " %%a in ( 'handle64.exe C:\dev\massunlocker\sample-dir -u -nobanner' ) do (
REM echo a = "%%a"
REM echo b = "%%b"
REM echo c = "%%c"
REM echo d = "%%d"
REM echo e = "%%e"
REM echo f = "%%f"
REM echo g = "%%g"
REM echo h = "%%h"
echo [%%h]
)
:end
setlocal DisableDelayedExpansion
First 2 are fine, but the %%h is hmm weird (edited)?
]C:\dev\massunlocker\sample-dir\ae\pdf
]C:\dev\massunlocker\sample-dir
]C:\dev\massunlocker\sample-dir\ae
Why its not this? :
[C:\dev\massunlocker\sample-dir\ae\pdf]
[C:\dev\massunlocker\sample-dir]
[C:\dev\massunlocker\sample-dir\ae]
And I can't test it for being dir or a file, it's always comes as true for not exist, for example.
Edit: here's an output example with lines uncommented:
a = "cmd.exe"
b = "pid:"
c = "1624"
d = "type:"
e = "File"
f = "PCNAME\UserName"
g = "1FC:"
" = "C:\dev\massunlocker\sample-dir
note the last line...
P.S. Handle tool
The output you're experiencing is the same as we get from the output of wmic.exe within a For loop. I'll assume therefore that handle64.exe also outputs an extra <CR>.
If that's the case, you should run the result through an additional For loop, in the same way as if it were WMIC. Take a look at some of the WMIC examples on this site for examples of it in use. Alternatively take a look at this topic over on dostips.com.
Here's an example of it in use:
For /F "Tokens=*" %%A In ('
handle64.exe C:\dev\massunlocker\sample-dir -u -nobanner
')Do For /F "Tokens=1-8" %%B In ("%%A")Do (Rem Echo B = "%%B"
Rem Echo C = "%%C"
Rem Echo D = "%%D"
Rem Echo E = "%%E"
Rem Echo F = "%%F"
Rem Echo G = "%%G"
Rem Echo H = "%%H"
Rem Echo I = "%%I"
Echo [%%I])
Pause
The following code snippet could help.
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
for /f "tokens=1-7,* delims= " %%a in ('
handle64.exe C:\Windows\System32\en-US\Kernel -u -nobanner
' ) do (
REM echo a = "%%a"
REM echo b = "%%b"
REM echo c = "%%c"
REM echo d = "%%d"
REM echo e = "%%e"
REM echo f = "%%f"
REM echo g = "%%g"
REM echo h = "%%h"
for /F "tokens=*" %%H in ("%%~h") do echo h="%%H" a="%%a"
)
Here the for loops are
%%a to retrieve the handle64.exe output values;
%%H to remove the ending carriage return in the %%h value returned at a line end.
wmic (and handle64 as well) behaviour: each output line ends with 0x0D0D0A (<CR><CR><LF>) instead of common 0x0D0A (<CR><LF>).
See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem
Output (truncated):
==> D:\bat\SO\55065841.bat
h="C:\Windows\System32\en-US\KernelBase.dll.mui" a="GoldenDict.exe"
h="C:\Windows\System32\en-US\KernelBase.dll.mui" a="chrome.exe"
h="C:\Windows\System32\en-US\kernel32.dll.mui" a="chrome.exe"
h="C:\Windows\System32\en-US\KernelBase.dll.mui" a="AppleChromeDAV.exe"
==>
I don't mean this as an answer.. I just needed the formatted text.
As Compo points out.. there is indeed 0D 0D 0A at the end of the output but not in the middle where the parser should care about it.
Their parser should be better than that.
As a workaround (as Compo mentioned), take the output of the call and run it through another for loop, it works just fine. I did the one below with a function call.
#echo off
Set THE_DIR=%TEMP%
for /f "delims=" %%a in ( 'handle64.exe %THE_DIR% -u -nobanner' ) do call :Process_Line "%%a"
goto :EOF
:Process_Line
for /f "tokens=1,2,3,4,5,6,7,8 delims= " %%a in ( 'echo %*' ) do (
echo a = "%%a"
echo b = "%%b"
echo c = "%%c"
echo d = "%%d"
echo e = "%%e"
echo f = "%%f"
echo g = "%%g"
echo h = "%%h"
)
goto :EOF
I start with this example: I have a file called concatlist.txt that contain a list of real .mxf files, for example
CONCATLIST.TXT
c:\myfolder\C0060.MXF
c:\myfolder\C0061.MXF
c:\myfolder\C0062.MXF
c:\myfolder\C0063.MXF
c:\myfolder\C0064.MXF
c:\myfolder\C0065.MXF
c:\myfolder\C0066.MXF
c:\myfolder\C0067.MXF
c:\myfolder\C0068.MXF
c:\myfolder\C0069.MXF
c:\myfolder\C0070.MXF
c:\myfolder\C0071.MXF
c:\myfolder\C0072.MXF
c:\myfolder\C0060.MXF
c:\myfolder\C0061.MXF
c:\myfolder\C0062.MXF
c:\myfolder\C0063.MXF
c:\myfolder\C0064.MXF
c:\myfolder\C0065.MXF
c:\myfolder\C0066.MXF
c:\myfolder\C0067.MXF
c:\myfolder\C0068.MXF
c:\myfolder\C0069.MXF
c:\myfolder\C0070.MXF
c:\myfolder\C0071.MXF
c:\myfolder\C0072.MXF
I would like create a batch script that from the concatlist.txt create a target.txt in wich each filename_and_pathfile is putted like this .txt file:
SetMemoryMax(16)
LoadPlugin("v:\automazioneclip\avisynth\plugins\LSMASHSource.dll")
videofile0 = LWLibavVideoSource("c:\myfolder\C0060.MXF")
audiofile0 = LWLibavAudioSource("c:\myfolder\C0060.MXF")
file0 = audiodub(videofile0,audiofile0)
videofile1 = LWLibavVideoSource("c:\myfolder\C0061.MXF")
audiofile1 = LWLibavAudioSource("c:\myfolder\C0061.MXF")
file1 = audiodub(videofile1,audiofile1)
videofile2 = LWLibavVideoSource("c:\myfolder\C0062.MXF")
audiofile2 = LWLibavAudioSource("c:\myfolder\C0062.MXF")
file2 = audiodub(videofile2,audiofile2)
videofile3 = LWLibavVideoSource("c:\myfolder\C0063.MXF")
audiofile3 = LWLibavAudioSource("c:\myfolder\C0063.MXF")
file3 = audiodub(videofile3,audiofile3)
videofile4 = LWLibavVideoSource("c:\myfolder\C0064.MXF")
audiofile4 = LWLibavAudioSource("c:\myfolder\C0064.MXF")
file4 = audiodub(videofile4,audiofile4)
videofile5 = LWLibavVideoSource("c:\myfolder\C0065.MXF")
audiofile5 = LWLibavAudioSource("c:\myfolder\C0065.MXF")
file5 = audiodub(videofile5,audiofile5)
videofile6 = LWLibavVideoSource("c:\myfolder\C0066.MXF")
audiofile6 = LWLibavAudioSource("c:\myfolder\C0066.MXF")
file6 = audiodub(videofile6,audiofile6)
videofile7 = LWLibavVideoSource("c:\myfolder\C0067.MXF")
audiofile7 = LWLibavAudioSource("c:\myfolder\C0067.MXF")
file7 = audiodub(videofile7,audiofile7)
videofile8 = LWLibavVideoSource("c:\myfolder\C0068.MXF")
audiofile8 = LWLibavAudioSource("c:\myfolder\C0068.MXF")
file8 = audiodub(videofile8,audiofile8)
videofile9 = LWLibavVideoSource("c:\myfolder\C0069.MXF")
audiofile9 = LWLibavAudioSource("c:\myfolder\C0069.MXF")
file9 = audiodub(videofile9,audiofile9)
videofile10 = LWLibavVideoSource("c:\myfolder\C0070.MXF")
audiofile10 = LWLibavAudioSource("c:\myfolder\C0070.MXF")
file10 = audiodub(videofile10,audiofile10)
videofile11 = LWLibavVideoSource("c:\myfolder\C0071.MXF")
audiofile11 = LWLibavAudioSource("c:\myfolder\C0071.MXF")
file11 = audiodub(videofile11,audiofile11)
videofile12 = LWLibavVideoSource("c:\myfolder\C0072.MXF")
audiofile12 = LWLibavAudioSource("c:\myfolder\C0072.MXF")
file12 = audiodub(videofile12,audiofile12)
file0++file1++file2++file3++file4++file5++file6++file7++file8++file9++file10++file11++file12
Another example:
assumed the concatList.txt is this
c:\cats\catsVideoA.MXF
c:\dogs\dogsVideoB.MXF
the batch should generate this target.txt:
SetMemoryMax(16)
LoadPlugin("v:\automazioneclip\avisynth\plugins\LSMASHSource.dll")
videofile0 = LWLibavVideoSource("c:\cats\catsVideoA.MXF")
audiofile0 = LWLibavAudioSource("c:\cats\catsVideoA.MXF")
file0 = audiodub(videofile0,audiofile0)
videofile1 = LWLibavVideoSource("c:\dogs\dogsVideoB.MXF")
audiofile1 = LWLibavAudioSource("c:\dogs\dogsVideoB.MXF")
file1 = audiodub(videofile1,audiofile1)
file0++file1
Test this code:
#echo off
setlocal enabledelayedexpansion
set num=0
(
echo SetMemoryMax(16^)
echo LoadPlugin("v:\automazioneclip\avisynth\plugins\LSMASHSource.dll"^)
for /f "usebackq delims=" %%a in ("CONCATLIST.TXT") do (
echo(
set line=!line!++file!num!
echo videofile!num! = LWLibavVideoSource("%%a"^)
echo audiofile!num! = LWLibavAudioSource("%%a"^)
echo file!num! = audiodub(videofile!num!,audiofile!num!^)
set /a num+=1
)
echo !line:~2!
)>"target.txt"
Here is a variant that can support an "unlimited" concatlist size. (not really unlimited, but a much bigger number than will ever be practical)
I also avoid delayed expansion so ! does not cause any problems in file paths.
#echo off
setlocal disableDelayedExpansion
call :createFile >target.txt
exit /b
:createFile
echo SetMemoryMax(16)
echo LoadPlugin("v:\automazioneclip\avisynth\plugins\LSMASHSource.dll")
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" "concatlist.txt"') do (
echo(
echo videofile%%A = LWLibavVideoSource("%%B"^)
echo audiofile%%A = LWLibavAudioSource("%%B"^)
echo file%%A = audiodub(videofile%%A,audiofile%%A^)
set "cnt=%%A"
)
set /a cnt2=cnt-1
for /l %%N in (1 1 %cnt2%) do <nul set /p "=file%%N++"
echo file%cnt%
exit /b
EDIT
If you really insist that the numbering begin with 0 instead of 1, then it is best to revert back to using SET /A to keep track of the count.
#echo off
setlocal disableDelayedExpansion
call :createFile >target.txt
type target.txt
exit /b
:createFile
echo SetMemoryMax(16)
echo LoadPlugin("v:\automazioneclip\avisynth\plugins\LSMASHSource.dll")
set /a cnt=0
for /f "usebackq eol=: delims=" %%A in ("concatlist.txt") do (
echo(
setlocal enableDelayedExpansion
for %%N in (!cnt!) do (
endlocal
echo videofile%%N = LWLibavVideoSource("%%A"^)
echo audiofile%%N = LWLibavAudioSource("%%A"^)
echo file%%N = audiodub(videofile%%N,audiofile%%N^)
)
set /a cnt+=1
)
<nul set /p "=file0"
for /l %%N in (1 1 %cnt%) do <nul set /p "=++file%%N"
echo(
exit /b