help sorting list of filename by its string lenght

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
bars143
Posts: 87
Joined: 01 Sep 2013 20:47

help sorting list of filename by its string lenght

#1 Post by bars143 » 31 Oct 2013 21:05

anyone help and give me a code to sort a list of filenames by its string lenght in a text file? :

example of output should be:

Code: Select all


abc.txt
abc.mp3
a_1.exe
abcdef.txt
abcdef.mp3
abcd_2.exe
dostip-abcdef.txt
dostip-abcdef.mp3
dostip-abcd_2.exe



i will also accept output like this:

Code: Select all


dostip-abcdef.txt
dostip-abcdef.mp3
dostip-abcd_2.exe
abcdef.txt
abcdef.mp3
abcd_2.exe
abc.txt
abc.mp3
a_1.exe



all two outputs above can be easily copied or deleted if sorted that way.

i used notepad, notepad2 and notepad++ with the option of sort by a-z or 1-9 but no option for sorting list by its string length.

thats why i needs an expert in providing code by use of a batch script if possible.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help sorting list of filename by its string lenght

#2 Post by foxidrive » 31 Oct 2013 23:42

Here a method:

removed code to avoid confusion

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: help sorting list of filename by its string lenght

#3 Post by bars143 » 01 Nov 2013 02:05

foxie your code work to the given example but when i included a filenames like below:

_ab.mp3
_abcde.mp3
_dostip-abcde.mp3
1abcde.exe
1bc.exe
1dostip-abcde.exe

it is scattered.

below are output after inclusion of new filename with a "_" or "1" as first character of a filename (its my mistake foxie for late inclusion of such characters, i could have included it in the first place):

Code: Select all

_ab.mp3
_abcde.mp3
_dostip-abcde.mp3
1abcde.exe
1bc.exe
1dostip-abcde.exe
a_1.exe
abc.mp3
abc.txt
abcd_2.exe
abcdef.mp3
abcdef.txt
dostip-abcd_2.exe
dostip-abcdef.mp3
dostip-abcdef.txt


it should be like this below (grouped to same string length):

Code: Select all

_ab.mp3
1bc.exe
a_1.exe
abc.mp3
abc.txt
1abcde.exe
_abcde.mp3
abcd_2.exe
abcdef.mp3
abcdef.txt
dostip-abcd_2.exe
dostip-abcdef.mp3
dostip-abcdef.txt
1dostip-abcde.exe
_dostip-abcde.mp3


but my purpose should be there an actual testing rather than that of above:

below are actual copied links taken from website(dostips.com page#84)
as you see below - it does not sort according to the numbers of characters of file's name which included characters like / ? . = : & ,etc. :

Code: Select all

http://www.divisioncore.com/
http://www.dostips.com/
http://www.dostips.com/forum/faq.php
http://www.dostips.com/forum/index.php
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=14
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=15
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=17
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=18
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=19
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=21
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=23
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=28
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=29
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=32
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=4919
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5119
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9
http://www.dostips.com/forum/posting.php?mode=post&f=3
http://www.dostips.com/forum/search.php
http://www.dostips.com/forum/search.php?search_id=active_topics
http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/ucp.php?mode=login
http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewforum.php?f=3
http://www.dostips.com/forum/viewforum.php?f=3&start=3950
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewforum.php?f=3&start=4050
http://www.dostips.com/forum/viewforum.php?f=3&start=4100
http://www.dostips.com/forum/viewforum.php?f=3&start=4150
http://www.dostips.com/forum/viewforum.php?f=3&start=4150#
http://www.dostips.com/forum/viewforum.php?f=4
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=11&p=21#p21
http://www.dostips.com/forum/viewtopic.php?f=3&t=12
http://www.dostips.com/forum/viewtopic.php?f=3&t=12&p=26#p26
http://www.dostips.com/forum/viewtopic.php?f=3&t=14
http://www.dostips.com/forum/viewtopic.php?f=3&t=14&p=30#p30
http://www.dostips.com/forum/viewtopic.php?f=3&t=15
http://www.dostips.com/forum/viewtopic.php?f=3&t=15&p=39#p39
http://www.dostips.com/forum/viewtopic.php?f=3&t=16
http://www.dostips.com/forum/viewtopic.php?f=3&t=16&p=36#p36
http://www.dostips.com/forum/viewtopic.php?f=3&t=20
http://www.dostips.com/forum/viewtopic.php?f=3&t=20&p=46#p46
http://www.dostips.com/forum/viewtopic.php?f=3&t=21
http://www.dostips.com/forum/viewtopic.php?f=3&t=21&p=50#p50
http://www.dostips.com/forum/viewtopic.php?f=3&t=24
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56
http://www.dostips.com/forum/viewtopic.php?f=3&t=27
http://www.dostips.com/forum/viewtopic.php?f=3&t=27&p=58#p58
http://www.dostips.com/forum/viewtopic.php?f=3&t=28
http://www.dostips.com/forum/viewtopic.php?f=3&t=28&p=63#p63
http://www.dostips.com/forum/viewtopic.php?f=3&t=30
http://www.dostips.com/forum/viewtopic.php?f=3&t=30&p=73#p73
http://www.dostips.com/forum/viewtopic.php?f=3&t=31
http://www.dostips.com/forum/viewtopic.php?f=3&t=31&p=128#p128
http://www.dostips.com/forum/viewtopic.php?f=3&t=32
http://www.dostips.com/forum/viewtopic.php?f=3&t=32&p=92#p92
http://www.dostips.com/forum/viewtopic.php?f=3&t=33
http://www.dostips.com/forum/viewtopic.php?f=3&t=33&p=79#p79
http://www.dostips.com/forum/viewtopic.php?f=3&t=35
http://www.dostips.com/forum/viewtopic.php?f=3&t=35&p=78#p78
http://www.dostips.com/forum/viewtopic.php?f=3&t=36
http://www.dostips.com/forum/viewtopic.php?f=3&t=36&p=80#p80
http://www.dostips.com/forum/viewtopic.php?f=3&t=44
http://www.dostips.com/forum/viewtopic.php?f=3&t=44&p=98#p98
http://www.dostips.com/forum/viewtopic.php?f=3&t=45
http://www.dostips.com/forum/viewtopic.php?f=3&t=45&p=113#p113
http://www.dostips.com/forum/viewtopic.php?f=3&t=47
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111
http://www.dostips.com/forum/viewtopic.php?f=3&t=48
http://www.dostips.com/forum/viewtopic.php?f=3&t=48&p=133#p133
http://www.dostips.com/forum/viewtopic.php?f=3&t=49
http://www.dostips.com/forum/viewtopic.php?f=3&t=49&p=132#p132
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=9
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17
http://www.freeforums.org/
http://www.phpbb.com/
http://www.stsoftware.biz/index.html


below is what i need (but i inserted blankline to separate different group per filename's string length).

Code: Select all

http://www.phpbb.com/

http://www.dostips.com/

http://www.freeforums.org/

http://www.divisioncore.com/

http://www.dostips.com/forum/faq.php
http://www.stsoftware.biz/index.html

http://www.dostips.com/forum/index.php

http://www.dostips.com/forum/search.php

http://www.dostips.com/forum/viewforum.php?f=3
http://www.dostips.com/forum/viewforum.php?f=4

http://www.dostips.com/forum/ucp.php?mode=login

http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewtopic.php?f=3&t=9

http://www.dostips.com/forum/viewtopic.php?f=3&t=31
http://www.dostips.com/forum/viewtopic.php?f=3&t=12
http://www.dostips.com/forum/viewtopic.php?f=3&t=14
http://www.dostips.com/forum/viewtopic.php?f=3&t=45
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=44
http://www.dostips.com/forum/viewtopic.php?f=3&t=35
http://www.dostips.com/forum/viewtopic.php?f=3&t=30
http://www.dostips.com/forum/viewtopic.php?f=3&t=47
http://www.dostips.com/forum/viewtopic.php?f=3&t=49
http://www.dostips.com/forum/viewtopic.php?f=3&t=21
http://www.dostips.com/forum/viewtopic.php?f=3&t=24
http://www.dostips.com/forum/viewtopic.php?f=3&t=20
http://www.dostips.com/forum/viewtopic.php?f=3&t=27
http://www.dostips.com/forum/viewtopic.php?f=3&t=28
http://www.dostips.com/forum/viewtopic.php?f=3&t=33
http://www.dostips.com/forum/viewtopic.php?f=3&t=36
http://www.dostips.com/forum/viewtopic.php?f=3&t=15
http://www.dostips.com/forum/viewtopic.php?f=3&t=32
http://www.dostips.com/forum/viewtopic.php?f=3&t=48
http://www.dostips.com/forum/viewtopic.php?f=3&t=16

http://www.dostips.com/forum/viewtopic.php?f=3&t=4980
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982

http://www.dostips.com/forum/posting.php?mode=post&f=3

http://www.dostips.com/forum/viewforum.php?f=3&start=3950
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewforum.php?f=3&start=4050
http://www.dostips.com/forum/viewforum.php?f=3&start=4100
http://www.dostips.com/forum/viewforum.php?f=3&start=4150

http://www.dostips.com/forum/viewforum.php?f=3&start=4150#

http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17

http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/viewtopic.php?f=3&t=12&p=26#p26
http://www.dostips.com/forum/viewtopic.php?f=3&t=32&p=92#p92
http://www.dostips.com/forum/viewtopic.php?f=3&t=20&p=46#p46
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56
http://www.dostips.com/forum/viewtopic.php?f=3&t=30&p=73#p73
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=11&p=21#p21
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=36&p=80#p80
http://www.dostips.com/forum/viewtopic.php?f=3&t=15&p=39#p39
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=16&p=36#p36
http://www.dostips.com/forum/viewtopic.php?f=3&t=28&p=63#p63
http://www.dostips.com/forum/viewtopic.php?f=3&t=21&p=50#p50
http://www.dostips.com/forum/viewtopic.php?f=3&t=35&p=78#p78
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/viewtopic.php?f=3&t=27&p=58#p58
http://www.dostips.com/forum/viewtopic.php?f=3&t=44&p=98#p98
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=33&p=79#p79
http://www.dostips.com/forum/viewtopic.php?f=3&t=14&p=30#p30

http://www.dostips.com/forum/viewtopic.php?f=3&t=31&p=128#p128
http://www.dostips.com/forum/viewtopic.php?f=3&t=49&p=132#p132
http://www.dostips.com/forum/viewtopic.php?f=3&t=48&p=133#p133
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111
http://www.dostips.com/forum/viewtopic.php?f=3&t=45&p=113#p113
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111

http://www.dostips.com/forum/search.php?search_id=active_topics

http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9

http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=14
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=15
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=17
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=18
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=19
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=21
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=23
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=28
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=29
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=32

http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=4919
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5119



here are group of links of above of what i needed to be saved for further study in batch scripting.

Code: Select all

http://www.dostips.com/forum/viewtopic.php?f=3&t=31
http://www.dostips.com/forum/viewtopic.php?f=3&t=12
http://www.dostips.com/forum/viewtopic.php?f=3&t=14
http://www.dostips.com/forum/viewtopic.php?f=3&t=45
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=44
http://www.dostips.com/forum/viewtopic.php?f=3&t=35
http://www.dostips.com/forum/viewtopic.php?f=3&t=30
http://www.dostips.com/forum/viewtopic.php?f=3&t=47
http://www.dostips.com/forum/viewtopic.php?f=3&t=49
http://www.dostips.com/forum/viewtopic.php?f=3&t=21
http://www.dostips.com/forum/viewtopic.php?f=3&t=24
http://www.dostips.com/forum/viewtopic.php?f=3&t=20
http://www.dostips.com/forum/viewtopic.php?f=3&t=27
http://www.dostips.com/forum/viewtopic.php?f=3&t=28
http://www.dostips.com/forum/viewtopic.php?f=3&t=33
http://www.dostips.com/forum/viewtopic.php?f=3&t=36
http://www.dostips.com/forum/viewtopic.php?f=3&t=15
http://www.dostips.com/forum/viewtopic.php?f=3&t=32
http://www.dostips.com/forum/viewtopic.php?f=3&t=48
http://www.dostips.com/forum/viewtopic.php?f=3&t=16


im very very sorry for some incompleteness of request of help. :oops:

Bars

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help sorting list of filename by its string lenght

#4 Post by foxidrive » 01 Nov 2013 02:34

Actually I confused myself into thinking it worked - the samples just came out that way. :)

I'll have a look further.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help sorting list of filename by its string lenght

#5 Post by foxidrive » 01 Nov 2013 03:11

This works here - it should support most characters.

Code: Select all

@echo off
:: sort names by number of characters

type nul>_ab.mp3
type nul>_abcde.mp3
type nul>_dostip-abcde.mp3
type nul>1abcde.exe
type nul>1bc.exe
type nul>1dostip-abcde.exe
type nul>a_1.exe
type nul>abc.mp3
type nul>abc.txt
type nul>abcd_2.exe
type nul>abcdef.mp3
type nul>abcdef.txt
type nul>dostip-abcd_2.exe
type nul>dostip-abcdef.mp3
type nul>dostip-abcdef.txt

del temp.tmp 2>nul
for /f "delims=" %%a in (' dir /b /a-d /on') do call :next "%%a"
for /f "tokens=1,* delims=>" %%a in (' sort ^< temp.tmp ') do echo(%%b
del temp.tmp 2>nul
pause
goto :EOF

:next
set "c=0"
set "var=%~1"
setlocal enabledelayedexpansion
:loop
if not "!var:~%c%,1!"=="" (
   set /a c+=1
   goto :loop
)
set "num=000000%c%"
for %%b in ("%num:~-5%>!var!") do >>temp.tmp echo(%%~b
endlocal

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: help sorting list of filename by its string lenght

#6 Post by dbenham » 01 Nov 2013 06:32

Assuming your list is in a file named test.txt, then here is a very efficient solution using REPL.BAT

Code: Select all

type test.txt | repl .* "$&??$&" | repl \?\?$ "" | repl .(?=.*\?\?) x|sort|repl .*\?\? ""

The above assumes the string "??" (quotes not included) never appears within the list.

REPL.BAT seems to have a bug in the first call that repeats the "??" at the end, thus the necessity of the 2nd REPL call to remove the terminating "??".


Dave Benham

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: help sorting list of filename by its string lenght

#7 Post by bars143 » 01 Nov 2013 08:44

foxi, your second script worked with "_" and "1".
and i edited your second script to work with dostips.com's links as specified above.
but i did homeworks at many hours and nothing solved as i struggle of what causes that output like "echo off" and a "?" symbols problems.

here are my edited code with your method:

Code: Select all

@echo off
:: filenames are listed in a file named: temp.tmp

del sorted_output.txt 2>nul

:: first convert "?" to "X" to avoid blank output
setlocal enabledelayedexpansion
for /f "delims=" %%a in (temp.tmp) do (
SET "m=%%a"
SET "m=!m:?=X!
ECHO !m! >>temp2.tmp
)
PAUSE

:: sort names by number of characters
for /f "delims=" %%a in (temp2.tmp) do call :next "%%a"
for /f "tokens=1,* delims=>" %%a in (' sort ^< temp2.tmp ') do echo(%%b >>sorted_output.txt
del temp2.tmp 2>nul
pause

:: rename back "X" to "?"
setlocal enabledelayedexpansion
for /f "delims=" %%a in (sorted_output.txt) do (
SET "n=%%a"
SET "n=!n:X=?!
ECHO !n! >>final_output.txt
)

goto :EOF

:next
set "c=0"
set "var=%~1"
setlocal enabledelayedexpansion
:loop
if not "!var:~%c%,1!"=="" (
   set /a c+=1
   goto :loop
)
set "num=000000%c%"
for %%b in ("%num:~-5%>!var!") do >>temp2.tmp echo(%%~b
endlocal


here are the content of temp.tmp file

Code: Select all

http://www.divisioncore.com/
http://www.dostips.com/
http://www.dostips.com/forum/faq.php
http://www.dostips.com/forum/index.php
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=14
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=15
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=17
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=18
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=19
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=21
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=23
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=28
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=29
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=32
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=4919
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5119
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9
http://www.dostips.com/forum/posting.php?mode=post&f=3
http://www.dostips.com/forum/search.php
http://www.dostips.com/forum/search.php?search_id=active_topics
http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/ucp.php?mode=login
http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewforum.php?f=3
http://www.dostips.com/forum/viewforum.php?f=3&start=3950
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewforum.php?f=3&start=4050
http://www.dostips.com/forum/viewforum.php?f=3&start=4100
http://www.dostips.com/forum/viewforum.php?f=3&start=4150
http://www.dostips.com/forum/viewforum.php?f=3&start=4150#
http://www.dostips.com/forum/viewforum.php?f=4
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=11&p=21#p21
http://www.dostips.com/forum/viewtopic.php?f=3&t=12
http://www.dostips.com/forum/viewtopic.php?f=3&t=12&p=26#p26
http://www.dostips.com/forum/viewtopic.php?f=3&t=14
http://www.dostips.com/forum/viewtopic.php?f=3&t=14&p=30#p30
http://www.dostips.com/forum/viewtopic.php?f=3&t=15
http://www.dostips.com/forum/viewtopic.php?f=3&t=15&p=39#p39
http://www.dostips.com/forum/viewtopic.php?f=3&t=16
http://www.dostips.com/forum/viewtopic.php?f=3&t=16&p=36#p36
http://www.dostips.com/forum/viewtopic.php?f=3&t=20
http://www.dostips.com/forum/viewtopic.php?f=3&t=20&p=46#p46
http://www.dostips.com/forum/viewtopic.php?f=3&t=21
http://www.dostips.com/forum/viewtopic.php?f=3&t=21&p=50#p50
http://www.dostips.com/forum/viewtopic.php?f=3&t=24
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56
http://www.dostips.com/forum/viewtopic.php?f=3&t=27
http://www.dostips.com/forum/viewtopic.php?f=3&t=27&p=58#p58
http://www.dostips.com/forum/viewtopic.php?f=3&t=28
http://www.dostips.com/forum/viewtopic.php?f=3&t=28&p=63#p63
http://www.dostips.com/forum/viewtopic.php?f=3&t=30
http://www.dostips.com/forum/viewtopic.php?f=3&t=30&p=73#p73
http://www.dostips.com/forum/viewtopic.php?f=3&t=31
http://www.dostips.com/forum/viewtopic.php?f=3&t=31&p=128#p128
http://www.dostips.com/forum/viewtopic.php?f=3&t=32
http://www.dostips.com/forum/viewtopic.php?f=3&t=32&p=92#p92
http://www.dostips.com/forum/viewtopic.php?f=3&t=33
http://www.dostips.com/forum/viewtopic.php?f=3&t=33&p=79#p79
http://www.dostips.com/forum/viewtopic.php?f=3&t=35
http://www.dostips.com/forum/viewtopic.php?f=3&t=35&p=78#p78
http://www.dostips.com/forum/viewtopic.php?f=3&t=36
http://www.dostips.com/forum/viewtopic.php?f=3&t=36&p=80#p80
http://www.dostips.com/forum/viewtopic.php?f=3&t=44
http://www.dostips.com/forum/viewtopic.php?f=3&t=44&p=98#p98
http://www.dostips.com/forum/viewtopic.php?f=3&t=45
http://www.dostips.com/forum/viewtopic.php?f=3&t=45&p=113#p113
http://www.dostips.com/forum/viewtopic.php?f=3&t=47
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111
http://www.dostips.com/forum/viewtopic.php?f=3&t=48
http://www.dostips.com/forum/viewtopic.php?f=3&t=48&p=133#p133
http://www.dostips.com/forum/viewtopic.php?f=3&t=49
http://www.dostips.com/forum/viewtopic.php?f=3&t=49&p=132#p132
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=9
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17
http://www.freeforums.org/
http://www.phpbb.com/
http://www.stsoftware.biz/index.html


here are content of final_output.txt
after renaming back "X" to "?"

Code: Select all

http://www.phpbb.com/   
http://www.dostips.com/   
http://www.freeforums.org/   
http://www.divisioncore.com/   
http://www.dostips.com/forum/faq.php   
http://www.stsoftware.biz/inde?.html   
http://www.dostips.com/forum/inde?.php   
http://www.dostips.com/forum/search.php   
http://www.dostips.com/forum/viewforum.php?f=3   
http://www.dostips.com/forum/viewforum.php?f=4   
http://www.dostips.com/forum/ucp.php?mode=login   
http://www.dostips.com/forum/ucp.php?mode=register   
http://www.dostips.com/forum/viewtopic.php?f=3&t=9   
http://www.dostips.com/forum/viewtopic.php?f=3&t=10   
http://www.dostips.com/forum/viewtopic.php?f=3&t=11   
http://www.dostips.com/forum/viewtopic.php?f=3&t=12   
http://www.dostips.com/forum/viewtopic.php?f=3&t=14   
http://www.dostips.com/forum/viewtopic.php?f=3&t=15   
http://www.dostips.com/forum/viewtopic.php?f=3&t=16   
http://www.dostips.com/forum/viewtopic.php?f=3&t=20   
http://www.dostips.com/forum/viewtopic.php?f=3&t=21   
http://www.dostips.com/forum/viewtopic.php?f=3&t=24   
http://www.dostips.com/forum/viewtopic.php?f=3&t=27   
http://www.dostips.com/forum/viewtopic.php?f=3&t=28   
http://www.dostips.com/forum/viewtopic.php?f=3&t=30   
http://www.dostips.com/forum/viewtopic.php?f=3&t=31   
http://www.dostips.com/forum/viewtopic.php?f=3&t=32   
http://www.dostips.com/forum/viewtopic.php?f=3&t=33   
http://www.dostips.com/forum/viewtopic.php?f=3&t=35   
http://www.dostips.com/forum/viewtopic.php?f=3&t=36   
http://www.dostips.com/forum/viewtopic.php?f=3&t=44   
http://www.dostips.com/forum/viewtopic.php?f=3&t=45   
http://www.dostips.com/forum/viewtopic.php?f=3&t=47   
http://www.dostips.com/forum/viewtopic.php?f=3&t=48   
http://www.dostips.com/forum/viewtopic.php?f=3&t=49   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983   
http://www.dostips.com/forum/posting.php?mode=post&f=3   
http://www.dostips.com/forum/viewforum.php?f=3&start=3950   
http://www.dostips.com/forum/viewforum.php?f=3&start=4000   
http://www.dostips.com/forum/viewforum.php?f=3&start=4050   
http://www.dostips.com/forum/viewforum.php?f=3&start=4100   
http://www.dostips.com/forum/viewforum.php?f=3&start=4150   
http://www.dostips.com/forum/viewforum.php?f=3&start=4150#   
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17   
http://www.dostips.com/forum/search.php?search_id=unanswered   
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19   
http://www.dostips.com/forum/viewtopic.php?f=3&t=11&p=21#p21   
http://www.dostips.com/forum/viewtopic.php?f=3&t=12&p=26#p26   
http://www.dostips.com/forum/viewtopic.php?f=3&t=14&p=30#p30   
http://www.dostips.com/forum/viewtopic.php?f=3&t=15&p=39#p39   
http://www.dostips.com/forum/viewtopic.php?f=3&t=16&p=36#p36   
http://www.dostips.com/forum/viewtopic.php?f=3&t=20&p=46#p46   
http://www.dostips.com/forum/viewtopic.php?f=3&t=21&p=50#p50   
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56   
http://www.dostips.com/forum/viewtopic.php?f=3&t=27&p=58#p58   
http://www.dostips.com/forum/viewtopic.php?f=3&t=28&p=63#p63   
http://www.dostips.com/forum/viewtopic.php?f=3&t=30&p=73#p73   
http://www.dostips.com/forum/viewtopic.php?f=3&t=32&p=92#p92   
http://www.dostips.com/forum/viewtopic.php?f=3&t=33&p=79#p79   
http://www.dostips.com/forum/viewtopic.php?f=3&t=35&p=78#p78   
http://www.dostips.com/forum/viewtopic.php?f=3&t=36&p=80#p80   
http://www.dostips.com/forum/viewtopic.php?f=3&t=44&p=98#p98   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980&p=0#p0   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981&p=0#p0   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982&p=0#p0   
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983&p=0#p0   
http://www.dostips.com/forum/viewtopic.php?f=3&t=31&p=128#p128   
http://www.dostips.com/forum/viewtopic.php?f=3&t=45&p=113#p113   
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111   
http://www.dostips.com/forum/viewtopic.php?f=3&t=48&p=133#p133   
http://www.dostips.com/forum/viewtopic.php?f=3&t=49&p=132#p132   
http://www.dostips.com/forum/search.php?search_id=active_topics   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=14   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=15   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=17   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=18   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=19   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=21   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=23   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=28   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=29   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=32   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=4919   
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5119   
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.


above sorting are good but have two faulty output remain to be corrected,
as you see and read above - the following problems needs correction:

1.) from six to seven lines , the word "index" become "inde?" as "x" is converted to "?" as i had no ideas of what to do with symbol "?".

2.) its create another 92 blank lines which converted to "echo off" and i dont know where they come from?


anyway a newbie like me needs some expert help to solved above problems.

Bars.

===========================================

dbenham wrote:Assuming your list is in a file named test.txt, then here is a very efficient solution using REPL.BAT

Code: Select all

type test.txt | repl .* "$&??$&" | repl \?\?$ "" | repl .(?=.*\?\?) x|sort|repl .*\?\? ""

The above assumes the string "??" (quotes not included) never appears within the list.

REPL.BAT seems to have a bug in the first call that repeats the "??" at the end, thus the necessity of the 2nd REPL call to remove the terminating "??".

Dave Benham


dbenham,

thanks for another method and i will test and use your suggestion soon.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: help sorting list of filename by its string lenght

#8 Post by Aacini » 01 Nov 2013 08:46

The Batch file below do what you want:

Code: Select all

@echo off 
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (input.txt) do (
   set "file=%%a"
   call :StrLenVar file len=
   set len=000!len!
   set name[!len:~-4!;!file!]=1
)

for /F "tokens=2 delims=;]" %%a in ('set name[') do echo %%a
goto :EOF


:StrLenVar varString len=
set "string=0!%1!"
set "len=0"
for /L %%a in (8,-1,0) do (
    set /A "len|=1<<%%a"
    for %%b in (!len!) do if "!string:~%%b,1!" equ "" set /A "len&=~1<<%%a"
)
set %2=%len%
exit /b

input.txt:

Code: Select all

http://www.divisioncore.com/
http://www.dostips.com/
http://www.dostips.com/forum/faq.php
http://www.dostips.com/forum/index.php
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9
http://www.dostips.com/forum/posting.php?mode=post&f=3
http://www.dostips.com/forum/search.php
http://www.dostips.com/forum/search.php?search_id=active_topics
http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/ucp.php?mode=login
http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewforum.php?f=3
http://www.dostips.com/forum/viewforum.php?f=3&start=3950
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewforum.php?f=4
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=9
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17
http://www.freeforums.org/
http://www.phpbb.com/
http://www.stsoftware.biz/index.html

output:

Code: Select all

http://www.phpbb.com/
http://www.dostips.com/
http://www.freeforums.org/
http://www.divisioncore.com/
http://www.dostips.com/forum/faq.php
http://www.stsoftware.biz/index.html
http://www.dostips.com/forum/index.php
http://www.dostips.com/forum/search.php
http://www.dostips.com/forum/viewforum.php?f=4
http://www.dostips.com/forum/ucp.php?mode=login
http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewtopic.php?f=3&t=9
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/posting.php?mode=post&f=3
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17
http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/search.php?search_id=active_topics
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13


Antonio

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: help sorting list of filename by its string lenght

#9 Post by bars143 » 01 Nov 2013 09:50

Dbenham,

your REPL.BAT is working great too with only one-liner code and i added code for a result to a file:

Code: Select all

type test.txt | repl .* "$&??$&" | repl \?\?$ "" | repl .(?=.*\?\?) x|sort|repl .*\?\? "" >>dbenham.txt


=================================================

Aacini,

i want the output like this below:

Code: Select all

http://www.phpbb.com/
http://www.dostips.com/
http://www.freeforums.org/
http://www.divisioncore.com/
http://www.dostips.com/forum/faq.php
http://www.stsoftware.biz/index.html
http://www.dostips.com/forum/index.php
http://www.dostips.com/forum/search.php
http://www.dostips.com/forum/viewforum.php?f=3
http://www.dostips.com/forum/viewforum.php?f=4
http://www.dostips.com/forum/ucp.php?mode=login
http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewtopic.php?f=3&t=9
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=12
http://www.dostips.com/forum/viewtopic.php?f=3&t=14
http://www.dostips.com/forum/viewtopic.php?f=3&t=15
http://www.dostips.com/forum/viewtopic.php?f=3&t=16
http://www.dostips.com/forum/viewtopic.php?f=3&t=20
http://www.dostips.com/forum/viewtopic.php?f=3&t=21
http://www.dostips.com/forum/viewtopic.php?f=3&t=24
http://www.dostips.com/forum/viewtopic.php?f=3&t=27
http://www.dostips.com/forum/viewtopic.php?f=3&t=28
http://www.dostips.com/forum/viewtopic.php?f=3&t=30
http://www.dostips.com/forum/viewtopic.php?f=3&t=31
http://www.dostips.com/forum/viewtopic.php?f=3&t=32
http://www.dostips.com/forum/viewtopic.php?f=3&t=33
http://www.dostips.com/forum/viewtopic.php?f=3&t=35
http://www.dostips.com/forum/viewtopic.php?f=3&t=36
http://www.dostips.com/forum/viewtopic.php?f=3&t=44
http://www.dostips.com/forum/viewtopic.php?f=3&t=45
http://www.dostips.com/forum/viewtopic.php?f=3&t=47
http://www.dostips.com/forum/viewtopic.php?f=3&t=48
http://www.dostips.com/forum/viewtopic.php?f=3&t=49
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983
http://www.dostips.com/forum/posting.php?mode=post&f=3
http://www.dostips.com/forum/viewforum.php?f=3&start=3950
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewforum.php?f=3&start=4050
http://www.dostips.com/forum/viewforum.php?f=3&start=4100
http://www.dostips.com/forum/viewforum.php?f=3&start=4150
http://www.dostips.com/forum/viewforum.php?f=3&start=4150#
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17
http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/viewtopic.php?f=3&t=11&p=21#p21
http://www.dostips.com/forum/viewtopic.php?f=3&t=12&p=26#p26
http://www.dostips.com/forum/viewtopic.php?f=3&t=14&p=30#p30
http://www.dostips.com/forum/viewtopic.php?f=3&t=15&p=39#p39
http://www.dostips.com/forum/viewtopic.php?f=3&t=16&p=36#p36
http://www.dostips.com/forum/viewtopic.php?f=3&t=20&p=46#p46
http://www.dostips.com/forum/viewtopic.php?f=3&t=21&p=50#p50
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56
http://www.dostips.com/forum/viewtopic.php?f=3&t=27&p=58#p58
http://www.dostips.com/forum/viewtopic.php?f=3&t=28&p=63#p63
http://www.dostips.com/forum/viewtopic.php?f=3&t=30&p=73#p73
http://www.dostips.com/forum/viewtopic.php?f=3&t=32&p=92#p92
http://www.dostips.com/forum/viewtopic.php?f=3&t=33&p=79#p79
http://www.dostips.com/forum/viewtopic.php?f=3&t=35&p=78#p78
http://www.dostips.com/forum/viewtopic.php?f=3&t=36&p=80#p80
http://www.dostips.com/forum/viewtopic.php?f=3&t=44&p=98#p98
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=31&p=128#p128
http://www.dostips.com/forum/viewtopic.php?f=3&t=45&p=113#p113
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111
http://www.dostips.com/forum/viewtopic.php?f=3&t=48&p=133#p133
http://www.dostips.com/forum/viewtopic.php?f=3&t=49&p=132#p132
http://www.dostips.com/forum/search.php?search_id=active_topics
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=14
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=15
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=17
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=18
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=19
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=21
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=23
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=28
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=29
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=32
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=4919
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5119


above output is a 92 lines which is also a 92 lines from input
but your code seems up to 25 lines only ? maybe something wrong with my copy and paste from your code?

===========================================================

foxie,

please help me to correct my code with the use of your method so that i learn more of how to solve usage of "?" symbols with other faulty "echo off" appearance? thanks.


also thanks again to all who help me.

Bars

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: help sorting list of filename by its string lenght

#10 Post by Aacini » 01 Nov 2013 10:53

Oops! I forgot to process the equal signs! :oops: The code below is correct:

Code: Select all

@echo off 
setlocal EnableDelayedExpansion

set maxLen=80

for /F "delims=" %%a in (input.txt) do (
   set "file=%%a"
   call :procFile len=
   set len=000!len!
   set name[!len:~-4!;!file!]=1
)

for /F "tokens=2 delims=;]" %%a in ('set name[') do (
   set "file=%%a"
   echo !file:\==!
)
goto :EOF


:procFile len=
for /L %%i in (0,1,%maxLen%) do if "!file:~%%i,1!" neq "" (
   set len=%%i
   if "!file:~%%i,1!" equ "=" (
      set /A j=%%i+1
      for %%j in (!j!) do set "file=!file:~0,%%i!\!file:~%%j!"
   )
)
exit /b


You must change the maxLen variable if the names to process are longer.

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help sorting list of filename by its string lenght

#11 Post by foxidrive » 01 Nov 2013 18:16

That is a good method, using repl.bat

The bug isn't present when you use this syntax, dave.

Code: Select all

type test.txt | repl "^.*$" "$&??$&" | repl .(?=.*\?\?) x|sort|repl .*\?\? ""

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help sorting list of filename by its string lenght

#12 Post by foxidrive » 01 Nov 2013 18:29

I fixed my earlier code, which also had the equals issue, and it now processes file.txt here.

It's pretty slow compared to repl.bat


Code: Select all

@echo off
:: sort names by number of characters

del temp.tmp 2>nul
for /f "delims=" %%a in (' type "file.txt"') do call :next "%%a"
for /f "tokens=1,* delims=>" %%a in (' sort ^< temp.tmp ') do echo(%%b
del temp.tmp 2>nul
pause
goto :EOF

:next
set "c=0"
set "var=%~1"
setlocal enabledelayedexpansion
:loop
if not "!var:~%c%,1!"=="" (
   set /a c+=1
   goto :loop
)
set "num=000000%c%"
for /f "delims=" %%b in ("%num:~-5%>!var!") do >>temp.tmp echo(%%~b
endlocal

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: help sorting list of filename by its string lenght

#13 Post by dbenham » 01 Nov 2013 20:03

foxidrive wrote:That is a good method, using repl.bat

The bug isn't present when you use this syntax, dave.

Code: Select all

type test.txt | repl "^.*$" "$&??$&" | repl .(?=.*\?\?) x|sort|repl .*\?\? ""

Thanks foxidrive. Nice solution. The $ is not needed at the end, only the leading ^ is needed.

I figured out why my version appended the ?? at the end: .* first matches the entire string and the replace is made, then .* matches the empty string at the end of the line, and performs a 2nd replace. So if it is a bug, it is a bug with the regex engine itself.

Addition of the leading caret prevents the search from matching the empty string at the end of the line.

An alternate approach is to force the regex to match at least one character:

Code: Select all

type test.txt | repl ".{1,}" "$&??$&" | repl .(?=.*\?\?) x|sort|repl .*\?\? ""


Dave Benham
Last edited by dbenham on 01 Nov 2013 22:54, edited 1 time in total.

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: help sorting list of filename by its string lenght

#14 Post by bars143 » 01 Nov 2013 22:47

foxi,
your third code cant be used due to the first line of input is transfered
to last line(#183) but also has a blanklines from #91 to 182.

those i selected back your second code as working
even though i added another two "for /f" scripts to stabilized "?" symbols (if not removed or renamed, it will output blankline)
and one "findstr" to eliminated unwanted blank lines(i dont know again where they come from).

here is my last modified code to your 2nd method:

Code: Select all

@echo off
:: filenames are listed in a file named: temp.tmp

:: first convert "?" to "®" to avoid blank output
:: before - i used "X" but not knowing that there are other "x" in the word "index"- which become "inde?" when renaming back to "?".

setlocal enabledelayedexpansion
for /f "delims=" %%a in (temp.tmp) do (
SET "m=%%a"
SET "m=!m:?=®!
ECHO !m! >>temp2.tmp
)

:: foxi sort method
:: added output file named "sorted_output.txt"
for /f "delims=" %%a in (temp2.tmp) do call :next "%%a"
for /f "tokens=1,* delims=>" %%a in (' sort ^< temp2.tmp ') do echo(%%b >>sorted_output.txt
del temp2.tmp 2>nul


:: rename back "®" to "?"
:: added output file after renaming back to "?"
setlocal enabledelayedexpansion
for /f "delims=" %%a in (sorted_output.txt) do (
SET "n=%%a"
SET "n=!n:®=?!
ECHO !n! >>renamed_output.txt
)

:: added code to remove unwanted blanklines
findstr "h" "renamed_output.txt" >>final_output.txt
goto :EOF

:next
set "c=0"
set "var=%~1"
setlocal enabledelayedexpansion
:loop
if not "!var:~%c%,1!"=="" (
   set /a c+=1
   goto :loop
)
set "num=000000%c%"
for %%b in ("%num:~-5%>!var!") do >>temp2.tmp echo(%%~b
endlocal


=====================================================

Aacini,
your second code working now and faster and yet REPL.BAT is fastest.
your code with different method got attention again. :idea:

foxi,
your code using REPL.BAT working too.
thanks to your methods which got my brains think more, which i made one above. :idea:

Dbenham,
your REPL.BAT really needs some background in JScript as worthy as of batch. :idea:


big thanks to all !!! :D

Bars
.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help sorting list of filename by its string lenght

#15 Post by foxidrive » 02 Nov 2013 00:52

bars143 wrote:foxi,
your third code cant be used due to the first line of input is transfered
to last line(#183) but also has a blanklines from #91 to 182.


Just on this point, the last code I provided gives me this - no blank lines etc here.

Code: Select all

http://www.phpbb.com/
http://www.dostips.com/
http://www.freeforums.org/
http://www.divisioncore.com/
http://www.dostips.com/forum/faq.php
http://www.stsoftware.biz/index.html
http://www.dostips.com/forum/index.php
http://www.dostips.com/forum/search.php
http://www.dostips.com/forum/viewforum.php?f=3
http://www.dostips.com/forum/viewforum.php?f=4
http://www.dostips.com/forum/ucp.php?mode=login
http://www.dostips.com/forum/ucp.php?mode=register
http://www.dostips.com/forum/viewtopic.php?f=3&t=9
http://www.dostips.com/forum/viewtopic.php?f=3&t=10
http://www.dostips.com/forum/viewtopic.php?f=3&t=11
http://www.dostips.com/forum/viewtopic.php?f=3&t=12
http://www.dostips.com/forum/viewtopic.php?f=3&t=14
http://www.dostips.com/forum/viewtopic.php?f=3&t=15
http://www.dostips.com/forum/viewtopic.php?f=3&t=16
http://www.dostips.com/forum/viewtopic.php?f=3&t=20
http://www.dostips.com/forum/viewtopic.php?f=3&t=21
http://www.dostips.com/forum/viewtopic.php?f=3&t=24
http://www.dostips.com/forum/viewtopic.php?f=3&t=27
http://www.dostips.com/forum/viewtopic.php?f=3&t=28
http://www.dostips.com/forum/viewtopic.php?f=3&t=30
http://www.dostips.com/forum/viewtopic.php?f=3&t=31
http://www.dostips.com/forum/viewtopic.php?f=3&t=32
http://www.dostips.com/forum/viewtopic.php?f=3&t=33
http://www.dostips.com/forum/viewtopic.php?f=3&t=35
http://www.dostips.com/forum/viewtopic.php?f=3&t=36
http://www.dostips.com/forum/viewtopic.php?f=3&t=44
http://www.dostips.com/forum/viewtopic.php?f=3&t=45
http://www.dostips.com/forum/viewtopic.php?f=3&t=47
http://www.dostips.com/forum/viewtopic.php?f=3&t=48
http://www.dostips.com/forum/viewtopic.php?f=3&t=49
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983
http://www.dostips.com/forum/posting.php?mode=post&f=3
http://www.dostips.com/forum/viewforum.php?f=3&start=3950
http://www.dostips.com/forum/viewforum.php?f=3&start=4000
http://www.dostips.com/forum/viewforum.php?f=3&start=4050
http://www.dostips.com/forum/viewforum.php?f=3&start=4100
http://www.dostips.com/forum/viewforum.php?f=3&start=4150
http://www.dostips.com/forum/viewforum.php?f=3&start=4150#
http://www.dostips.com/forum/viewtopic.php?f=3&t=9&p=17#p17
http://www.dostips.com/forum/search.php?search_id=unanswered
http://www.dostips.com/forum/viewtopic.php?f=3&t=10&p=19#p19
http://www.dostips.com/forum/viewtopic.php?f=3&t=11&p=21#p21
http://www.dostips.com/forum/viewtopic.php?f=3&t=12&p=26#p26
http://www.dostips.com/forum/viewtopic.php?f=3&t=14&p=30#p30
http://www.dostips.com/forum/viewtopic.php?f=3&t=15&p=39#p39
http://www.dostips.com/forum/viewtopic.php?f=3&t=16&p=36#p36
http://www.dostips.com/forum/viewtopic.php?f=3&t=20&p=46#p46
http://www.dostips.com/forum/viewtopic.php?f=3&t=21&p=50#p50
http://www.dostips.com/forum/viewtopic.php?f=3&t=24&p=56#p56
http://www.dostips.com/forum/viewtopic.php?f=3&t=27&p=58#p58
http://www.dostips.com/forum/viewtopic.php?f=3&t=28&p=63#p63
http://www.dostips.com/forum/viewtopic.php?f=3&t=30&p=73#p73
http://www.dostips.com/forum/viewtopic.php?f=3&t=32&p=92#p92
http://www.dostips.com/forum/viewtopic.php?f=3&t=33&p=79#p79
http://www.dostips.com/forum/viewtopic.php?f=3&t=35&p=78#p78
http://www.dostips.com/forum/viewtopic.php?f=3&t=36&p=80#p80
http://www.dostips.com/forum/viewtopic.php?f=3&t=44&p=98#p98
http://www.dostips.com/forum/viewtopic.php?f=3&t=4980&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4981&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4982&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=4983&p=0#p0
http://www.dostips.com/forum/viewtopic.php?f=3&t=31&p=128#p128
http://www.dostips.com/forum/viewtopic.php?f=3&t=45&p=113#p113
http://www.dostips.com/forum/viewtopic.php?f=3&t=47&p=111#p111
http://www.dostips.com/forum/viewtopic.php?f=3&t=48&p=133#p133
http://www.dostips.com/forum/viewtopic.php?f=3&t=49&p=132#p132
http://www.dostips.com/forum/search.php?search_id=active_topics
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=9
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=11
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=12
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=13
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=14
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=15
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=17
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=18
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=19
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=21
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=23
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=28
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=29
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=32
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=4919
http://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=5119

Post Reply