Page 1 of 1

How can i order the results consecutively in a batch?

Posted: 09 May 2014 07:17
by rootx
Example file list:

Code: Select all

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


easy cycle for iterating the list

Code: Select all

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


that's the result....

Code: Select all

1
2
22
3


How can i order the results consecutively, like below?

Code: Select all

1
2
3
22


thanks everyone

Re: How can i order the results consecutively in a batch?

Posted: 09 May 2014 07:31
by foxidrive
What tools can you, or can't you use?

Re: How can i order the results consecutively in a batch?

Posted: 09 May 2014 08:29
by Squashman
http://www.dostips.com/?t=Batch.SortTextWithNumbers

Code: Select all

@ECHO OFF
if "%~1"=="/?" (
    echo.Sorts text by handling first number in line as number not text
    echo.
    echo.%~n0 [n]
    echo.
    echo.  n     Specifies the character number, n, to
    echo.        begin each comparison.  3 indicates that
    echo.        each comparison should begin at the 3rd
    echo.        character in each line.  Lines with fewer
    echo.        than n characters collate before other lines.
    echo.        By default comparisons start at the first
    echo.        character in each line.
    echo.
    echo.Description:
    echo.        'abc10def3' is bigger than 'abc9def4' because
    echo.        first number in first string is 10
    echo.        first number in second string is 9
    echo.        whereas normal text compare returns
    echo.        'abc10def3' smaller than 'abc9def4'
    echo.
    echo.Example:
    echo.        To sort a directory pipe the output of the dir
    echo.        command into %~n0 like this:
    echo.           dir /b^|%~n0
    echo.
    echo.Source: http://www.dostips.com
    goto:EOF
)
if "%~1" NEQ "~" (
    for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
    goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
    set f=,%%B
    (
        set f0=!f:~0,%n%!
        set f0=!f0:~1!
        rem call call set f=,%%%%f:*%%f0%%=%%%%   
        set f=,!f:~%n%!
    )
    for /f "delims=1234567890" %%b in ("!f!") do (
        set f1=%%b
        set f1=!f1:~1!
        call set f=0%%f:*%%b=%%
    )
    for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`@#$*_-+=:;',.?/\ " %%b in ("!f!") do (
        set f2=00000000000000000000%%b
        set f2=!f2:~-20!
        call set f=%%f:*%%b=%%
    )
    echo.!f1!!f2!!f!,%%B
    rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)

Re: How can i order the results consecutively in a batch?

Posted: 09 May 2014 13:07
by Compo
If powershell is installed then you could use

Code: Select all

powershell -c "ls *.jpg|sort @{e={$_.BaseName -as [int]}}|%{$_.Name}"

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 00:03
by foxidrive
Compo wrote:If powershell is installed then you could use

Code: Select all

powershell -c "ls *.jpg|sort @{e={$_.BaseName -as [int]}}|%{$_.Name}"


It doesn't sort correctly in my test here.

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 04:57
by Compo
foxidrive wrote:It doesn't sort correctly in my test here.

Quite odd...

...not that it may not work, just that your response is completely out of character!
think about your response if that was a reply to one of your suggestions

What was your test data? and What was the result?

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 06:19
by Compo
foxidrive, as i guessed, your test data is not correct for the code I provided. The code I provided was in answer to the originating question only.

The BaseName is intended to be an integer, my code was to sort 'number data' numerically, your data contains other than numbers.

<EDIT>
Just for onlookers wondering why this post looks out of place, I'm not telepathic, foxidrive posted their data, and I responded prior to them removing it.
</EDIT>

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 08:57
by Squashman
I know I have some Powershell code at work that sorts Alpha Numerically. Will have to wait until Monday to post it.

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 09:07
by foxidrive
JPsoft's TCC/LE also has natural sorting that can be used in a batch command.

Compo, I didn't assume that the OP's data was as simple as it was presented - few posters give you any actual details these days.

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 11:23
by Aacini
Another possible solution for the original OP request:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /r %%j in (*.jpg) do (
   set j=0000000%%~nj
   set name[!j:~-8!]=~nxj
)
for "tokens=2 delims==" %%j in ('set name[') do echo %%j


Previous code show just one instance of duplicated names, if there are any in different folders; in the original example there is no way to differentiate duplicated names. This point may be fixed, if needed; for example, inserting the path to differentiate duplicated names.

Antonio

Re: How can i order the results consecutively in a batch?

Posted: 10 May 2014 12:05
by Compo
…and to make mine recursive too, (sorry I missed it):

Code: Select all

powershell -c "ls *.jpg -recurse|sort @{e={$_.BaseName -as [int]}}|%{$_.Name}"
Change .Name to .FullName if you want the path including too.

Re: How can i order the results consecutively in a batch?

Posted: 13 May 2014 22:18
by dbenham
@Squashman - Thanks for the SORTN.BAT link. I had not seen that before. It works well, but it can become quite slow with even a moderate number of lines.

I decided to write a hybrid JScript/batch JSORT.BAT solution that does much the same thing, except it also supports case sensitive sorts.

Sorts are strictly text based and case sensitive by default. Options are available to treat numbers as numbers, and to ignore case.

Using JSORT.BAT, the solution becomes:

Code: Select all

dir *.jpg | jsort /i /n


Dave Benham

Re: How can i order the results consecutively in a batch?

Posted: 14 May 2014 06:20
by Squashman
I try to remember to search the DT Batch Library first. It was dumb luck that my Google Search found it for me.

I can see a need for Case Sensitive sorting but it drives me nuts in a piece of software I use at work. I use a program called Watch Directory and it sorts the tasks in the Control Center case sensitive. When you are trying to find a task in a list of 189 of them it is sometimes a pain when it sorts them case sensitive. I think I am just going to go back and rename all the tasks UPPER CASE. My predecessor liked using mixed case for readability which is nice but when they are not in a true alphabetical order and you can't find the darn task it gets frustrating.

I think your code will come in handy for a lot of people.

Re: How can i order the results consecutively in a batch?

Posted: 14 May 2014 20:42
by foxidrive
Squashman wrote:My predecessor liked using mixed case for readability which is nice but when they are not in a true alphabetical order and you can't find the darn task it gets frustrating.

I think your code will come in handy for a lot of people.


I agree, the sortN script you posted and now Dave's sortJ script are nice tools to have.

I know what you meant about case sensitive sorted lists and trying to locate something - it's just not intuitive, but it makes me wonder: does the Watch Directory tool have a switch to control it?