Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
rootx
- Posts: 1
- Joined: 09 May 2014 07:08
#1
Post
by rootx » 09 May 2014 07:17
Example file list:
easy cycle for iterating the list
Code: Select all
for /r %j in (*.jpg) do @echo %~nxj
that's the result....
How can i order the results consecutively, like below?
thanks everyone
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#3
Post
by Squashman » 09 May 2014 08:29
http://www.dostips.com/?t=Batch.SortTextWithNumbersCode: 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
)
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#4
Post
by Compo » 09 May 2014 13:07
If powershell is installed then you could use
Code: Select all
powershell -c "ls *.jpg|sort @{e={$_.BaseName -as [int]}}|%{$_.Name}"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#5
Post
by foxidrive » 10 May 2014 00:03
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.
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#6
Post
by Compo » 10 May 2014 04:57
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 suggestionsWhat was your test data? and What was the result?
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#7
Post
by Compo » 10 May 2014 06:19
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>
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#8
Post
by Squashman » 10 May 2014 08:57
I know I have some Powershell code at work that sorts Alpha Numerically. Will have to wait until Monday to post it.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 10 May 2014 09:07
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.
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#10
Post
by Aacini » 10 May 2014 11:23
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
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#11
Post
by Compo » 10 May 2014 12:05
…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.
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#12
Post
by dbenham » 13 May 2014 22:18
@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:
Dave Benham
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#13
Post
by Squashman » 14 May 2014 06:20
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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#14
Post
by foxidrive » 14 May 2014 20:42
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?