views:

37

answers:

2

I have a windows batch file that does this:

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

But the for loop goes through the files randomly (not in the name order), it first run s5.01, then s4.06, then s4.08, then s4.10, then s4.07. How can I make them run in the name order?

It used to work, but now it doesn't. What may cause this problem?

+1  A: 

If memory serves, it will operate on the files in the order they're returned by the file system. As such, if you run it on a disk partition that's formatted with NTFS, the names will be returned in sorted order so they'll be processed in sorted order. If the disk partition is formatted with something like FAT or FAT32, the names will be retrieved (and processed) in a more or less random order.

Jerry Coffin
+1  A: 

Jerry's answer might very well be what is causing the problem.

You might solve it by changing

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

to

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

Lieven
Or, `for /f "usebackq" %%s in (```dir *.sql /b /ON```) do call`
Patrick Cuff