我在一个目录中有许多sql脚本,前缀是一个数字,例如
目前,我可以使用此脚本循环遍历它们:
@ECHO OFF FOR /r . %%F IN (*.sql) DO ( ECHO File:"%%F" )
但是,由于Windows读取目录中文件的方式,在2之前读取10和11,等等:
如何按照前缀的顺序遍历目录中的每个文件?
下面的批处理文件在需要它的文件名中插入前导零.
@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" )
@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 )