4

Example file list:

  • 1.jpg
  • 2.jpg
  • 3.jpg
  • 22.jpg

easy cycle for iterating the list

for /r %j in (*.jpg) do @echo %~nxj

The gives the following result:

1
2
22
3

How can i order the results consecutively, like below?

1
2
3
22

thanks everyone

Mark
  • 3,609
  • 1
  • 22
  • 33
gerry_76
  • 148
  • 1
  • 1
  • 8
  • possible duplicate of [Read files in directory in order of filename prefix with batch?](http://stackoverflow.com/questions/18748744/read-files-in-directory-in-order-of-filename-prefix-with-batch) – Mark May 09 '14 at 22:18

2 Answers2

0

I have just made this for you it seems that your way would of worked but there is a simpler way of doing it.

@echo off
del /q /s /f "%temp%\TEMP.tmp">nul
dir /b *.* >> %temp%\TEMP.tmp
SetLocal EnableDelayedExpansion

for /f "delims=" %%x in ('type %temp%\TEMP.tmp') do ( 
    set "Var=%%x"
    ECHO !Var!
)
pause

Put the bat file in the same dir as the pictures.

09stephenb
  • 9,358
  • 15
  • 53
  • 91
  • 1
    This code will not work as requested (increasing value) on an NTFS drive if the sort-order is altered to strict-alphabetical (the Win-2000 scheme - see https://support.microsoft.com/en-hk/help/319827/the-sort-order-for-files-and-folders-whose-names-contain-numerals-is-d ). Nor will it work for FAT-style devices (like most ramdrives, SD cards, etc.) unless the files are created in strict-numerical sequence, so if the files on an SD device are created sequentially as 22.jpg, 3.jpg, 2.jpg, 1.jpg then this routine will report them in *that* order, not in the sequence specified in the question. – Magoo Mar 31 '19 at 19:14
0
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
(
FOR /f %%a IN ('dir /b /a-d "%sourcedir%\*.jpg" ') DO (
  SET /a seq=1000000000+%%~na
  ECHO !seq!) 
)>"%temp%\tempfile"

FOR /f %%a IN ('sort "%temp%\tempfile"') DO (
 SET /a seq=%%a-1000000000
  ECHO !seq!.jpg) 
)

GOTO :EOF

This should work for you - of course, you'd need to set your own sourcedir, the temp filename is up to you, it's not cleaned up and it will only work with pure-numeric filenames without leading zeroes <1000000000. The .jpg extension is assumed.

Magoo
  • 77,302
  • 8
  • 62
  • 84