How to make for loop in windows batch file run in name order
Asked Answered
L

4

10

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?

Lobworm answered 31/8, 2010 at 6:22 Comment(0)
C
10

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


Edit cudo's to LonelyPixel

The posted solution does not work as is on a Windows 8 machine (perhaps it did back then with Windows XP but I have no idea).

Following is a working solution using Windows 8 command prompt.

for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo %%s
Cabbala answered 31/8, 2010 at 6:57 Comment(7)
Or, for /f "usebackq" %%s in (`dir *.sql /b /ON`) do callMcfall
Patrick, your solution cuts off all files names after a space, so it's only partially useful.Grannias
Lieven, your solution fails completely. If I replace the " by a space, it finds "dir", "|" and "sort", too. So that doesn't work either.Grannias
@LonelyPixel - I have updated the answer but as I've mentioned in the edit, I have no idea if it worked back then. (OP accepted this as a correct answer so I assume it did).Cabbala
@PatrickCuff For filenames with spaces, add tokens=, like this: for /f "usebackq tokens=" %%s in (dir "my work dir\*.sql" /b /ON) do call.Rosariorosarium
What about sort in SIZE ASC?Perish
The for %%s in (dir"*.sql ^| sort) fails on wineconsole with "more?"Neonatal
D
2

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.

Dextrad answered 31/8, 2010 at 6:45 Comment(0)
S
2

I too had to deal with spaces in the filenames, so I added quotes around the input variable like so:

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

In my case it was SQLCMD, with the scripts in a subdirectory, like so:

SET "machineName=localhost"
for /f "tokens=*" %%f in ('dir /b PostDeployScripts\*.sql ^| sort') do sqlcmd -E -S%machineName% -i"PostDeployScripts\%%f"
Scanty answered 20/10, 2015 at 16:12 Comment(0)
F
0

This won't use slow SORT external function, and works fine:

@echo off
setlocal enableextensions enabledelayedexpansion & REM enhanced FOR, use RunTime !varNm! value

echo\
echo\Method1
echo\
for /f "usebackq delims=" %%a in (`dir "*.sql" /b /on`) do (
  echo %%a
  )

echo\
echo\Method2
echo\
for /f "usebackq tokens=*" %%j in (`dir "*.sql" /b /on`) do (
  echo %%j
  )
Farandole answered 31/8 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.