1

I am writing a batch file that finds and executes all update.bat file inside all the directories dropped onto it.

The problem here is that I expect the arguments (i.e directories' path) comes in ordered by name but it turns out they are sorted by the modified date.

Is this the default behavior of Windows (Windows 7)? Any suggestion to solve this?


Here is my batch script:

@echo off
Setlocal EnableDelayedExpansion

if [%1]==[] goto :no_update_dropped

set LOG_FILE=update_log.txt

echo You are about to run these updates:
for %%G IN (%*) do (
  if exist %%~sG\NUL echo %%G
)

pause

for %%G IN (%*) do (
  if exist %%G\NUL (
        if exist %%G\update.bat (
            call %%G\update.bat %LOG_FILE%
        ) else (
            echo No update.bat found in %%G.
            goto :no_batch_found
        )
    )
)
goto :success

:no_update_dropped
echo NO UPDATE FOLDER FOUND
echo Drag and drop one or more update folder to run.
goto :exit

:no_batch_found
echo UPDATE NOT COMPLETED!
goto exit

:success
echo all updated has been run successfully
goto :exit

:exit
pause

Best Regards.

Genzer
  • 2,921
  • 2
  • 25
  • 38
  • The order in which the folders appear is controlled by whoever (or whatever) calls your batch file. Your batch script has no bearing on the order. You could sort the parameter list within your batch if you want, but perhaps the caller should pass them in the correct order instead. – dbenham Jun 08 '12 at 04:51
  • I did test it, though. I select all folder dir1, dir2, dir3, dir4 and drop onto the batch file, the order of arguments is dir4, dir1, dir2, dir3. – Genzer Jun 08 '12 at 05:36

2 Answers2

6

You can sort your argument list right in your for loop like this:

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('(for %%i in (%*^) do @echo %%~i^)^|sort') do (
    set dirname=%%a
    set dirname=!dirname:~0,-1!
    echo use "!dirname!" without the trailing space
)

P.S. It seems like sort appends a space to the end of string,(WTF ????) so you'll have to get rid of it. I changed the code.

Finally with the help of dbenham's explanation this becomes:

for /f "delims=" %%a in ('cmd /c "for %%i in (%*) do @echo %%~i"^|sort') do (
    echo use "%%a"
)

P.P.S This should work safer with commas in names (of course, they must be quoted)

for /f "delims=" %%a in ('cmd /c ^"for %%i in ^(%*^) do @echo %%~i^"^|sort') do (
        echo use "%%a"
)
panda-34
  • 4,089
  • 20
  • 25
  • This doesn't work with folder whose name containing white spaces. Do you have any workaround? – Genzer Jun 08 '12 at 06:44
  • All this fuss with removing quotes, removing space and adding quotes was done specifically to make it work with folders whose names contain whitespaces. This _is_ the workaround. Running this batch with arguments "2nd folder" "1st folder" produces "1st folder" "2nd folder" as the results of "!dirname!" expression. As to the compatibility of the rest of your batch file with names containing spaces - there are certain doubts about that, I haven't seen a _single_ quote in it. (Actually. you need _double_ quotes, of course). – panda-34 Jun 08 '12 at 06:54
  • 1
    Thank you for keeping your original code snippet in your answer. The second one, with the "cmd /c" embedded, strips any commas in a file or directory name, whereas your loop using "sort" doesn't. – Jeffrey Fox Jan 06 '19 at 05:34
  • @JeffreyFox, it doesn't strip but replaces them with spaces even inside the names. No idea what is going on but I probably found a solution – panda-34 Jan 15 '19 at 22:02
  • This does sort the files, but the loop iterations seem to be somehow sand boxed from each other. All I am trying to do is append the files into a string, which was already working before, but now the batch file only remembers the last last iteration of this loop. It calls `set "cmd_str=%cmd_str% + %%a"` many times, but outside the loop `cmd_str` simply equals `+ last_file` – Jonathon Mar 26 '23 at 02:41
0

I would change the input set.

You can order by name by using /on and to get directories

/ad

so all directories by name =

dir /ad /on 
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216