如何在Windows批处理文件中按名称顺序运行循环

new*_*guy 9 sql windows loops batch-file

我有一个Windows批处理文件,它执行此操作:

for %%s in (*.sql) do call

It loops through all the sql script in a folder.

In the folder the file names are like:
s4.06.01.sql
s4.07.01.sql
s4.08.01.sql
s4.10.01.sql
s5.01.sql
Run Code Online (Sandbox Code Playgroud)

但是for循环随机地遍历文件(不是按照名称顺序),首先运行s5.01,然后是s4.06,然后是s4.08,然后是s4.10,然后是s4.07.如何让它们按名称顺序运行?

它曾经工作过,但现在却没有.什么可能导致这个问题?

Lie*_*ers 8

杰里的答案很可能是引起问题的原因。

您可以通过更改来解决

for %%s in (*.sql) do call

for %%s in (dir"*.sql ^| sort) do call


编辑 cudo到LonelyPixel

发布的解决方案无法像在Windows 8机器上那样工作(也许可以在Windows XP上使用,但我不知道)

以下是使用Windows 8命令提示符的可行解决方案。

for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo %%s
Run Code Online (Sandbox Code Playgroud)

  • 或者,对于(\ dir * .sql / b / ON \`)中的/ f“ usebackq” %% s进行调用` (2认同)