Read files in directory in order of filename prefix with batch?
The Batch file below insert a leading zero in filenames that need it.
@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%f in ('dir /B *.sql') do (
set "filename=%%f"
if "!filename:~1,1!" equ "-" ren "%%f" "0%%f"
)
EDIT: New solution added
The Batch file below show the files in right order without rename them.
@echo off
setlocal EnableDelayedExpansion
rem Create an array with filenames in right order
for %%f in (*.sql) do (
for /F "delims=-" %%n in ("%%f") do (
set "number=00000%%n"
set "file[!number:~-6!]=%%f"
)
)
rem Process the filenames in right order
for /F "tokens=2 delims==" %%f in ('set file[') do (
echo %%f
)
Alternative approach!
This isn't a direct answer to your question, but may achieve same result:
FOR /L %%i IN (1,1,11) DO FOR %%f IN (%%i-*.sql) DO ECHO %%f
As far as I see there is no FOR
flag for that.
BUT here is pure PowerShell solution using some kind of weirdish Regular Expressions.
Or You can list files, sort them, put those sorted paths to the file in correct order and then read.
3th solution would be trying to build some sorting function using standart Windows Shell commands and operators.