I\'m trying to process a list of file names but don\'t know how to \"escape\" the command line to produce consistent output regardless of whether the name contains plain tex
try this:
@echo off
setlocal EnableDelayedExpansion
set /a COUNT=0
for /f "tokens=*" %%g in ('dir /b *.txt') do (
set /a COUNT=!COUNT! + 1
<nul set /p=!count!
setlocal disableDelayedExpansion & (
(echo( %%g)
)
setlocal enableDelayedExpansion
)
You can turn on/off the delayed expansion within the for loop for proper echoing the special chars. <nul set /p=%%g
prints a string without new line at the end - which is needed for proper echoing of the count which should be in delayed expansion part.And it will work faster without subroutine.
Edited to print the count first.
I think this will work as well.
@echo off
set /a COUNT=0
for /f "tokens=*" %%g in ('dir /b *.txt') do (
set /a COUNT+=1
set "file=%%g"
call :LIST file COUNT
)
pause
exit
:LIST
setlocal enabledelayedexpansion
echo !%2! !%1!
endlocal
goto :EOF
Output
1 01 A Test.txt
2 02 & Test.txt
3 03 ! test.txt
4 04 % test.txt
Press any key to continue . . .