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

Re: help sorting list of filename by its string lenght

#16 Post by bars143 » 02 Nov 2013 04:15

foxi,

a file.txt content i used is the same as of temp.tmp . and if it is no content - it give error.

then, i didnt ask you first of what inside the file.txt in your 3rd method and i decide myself thats content of file.txt is an input.


this is what i means that a red colored font is content is an input:

for /f "delims=" %%a in (' type "file.txt"') do call :next "%%a"

and another red colored font content below is also an input as the same as of above:

for /f "tokens=1,* delims=>" %%a in (' sort ^< temp.tmp ') do echo(%%b

its the same pattern as of my code with your second method and applying with your third method.

sorry, that is all i understand. :oops:

i could have have asked you first of what inside the file.txt? :?

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

Re: help sorting list of filename by its string lenght

#17 Post by foxidrive » 02 Nov 2013 06:37

bars143 wrote:i could have have asked you first of what inside the file.txt? :?


Yes, file.txt is the input file which I thought I mentioned. This is the content.

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

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

Re: help sorting list of filename by its string lenght

#18 Post by bars143 » 02 Nov 2013 08:05

foxi,

its really work to your original 3rd code !

finally, i find out the problem is this script:

an addition of echo in "echo del temp.tmp 2>nul"
or
deleting this script: "del temp.tmp 2>nul"


this should be added as rem (to remind me of importance):

Code: Select all

@echo off
:: sort names by number of characters

:: dont add an "echo" to "del temp.tmp 2>nul"
:: nor delete this script: "del temp.tmp 2>nul"
:: if not -- it will add blanklines equal to the number of input lines.
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 >>foxi.txt
del temp.tmp 2>nul
pause
goto :EOF

:next
...
...
...



first i remove echo to "echo del temp.tmp 2>nul"
and the result is working perfectly!
and it also work when i return back that script to my last code!!!
but until now i cant understand why it work and why its necessary? :?:

if its necessary then i will review back all codes again which i had read it codes several times in many topics before.

anyways thanks as its a necessary to my study too. :idea:

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

Re: help sorting list of filename by its string lenght

#19 Post by foxidrive » 02 Nov 2013 09:05

bars143 wrote:foxi,

finally, i find out the problem is this script:

an addition of echo in "echo del temp.tmp 2>nul"
or
deleting this script: "del temp.tmp 2>nul"

but until now i cant understand why it work and why its necessary? :?:


Thanks, I'm glad you sorted out the problem.

That line is needed to iniitalise the temporary file I used, by deleting it, which holds the strings in a format like this, which when sorted gives the list in line length order.

Code: Select all

000010>filename02
000015>filename 10 bob
000013>filenamexxx01
000013>filenamebbb53
000010>filename01


If the file remains from a previous run then it will skew the results with old data.
The input to the routine is either through the file.txt or via a DIR /b as I used earlier on.

Cheers
foxi

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

Re: help sorting list of filename by its string lenght

#20 Post by bars143 » 02 Nov 2013 11:46

foxi,

thanks again for an explanation.

its an existence of temp.tmp which shared the same folder of batch code -- is the cause all the trouble for me.

i did test afterward for other possible causes,
and i found out that without this script :

del temp.tmp 2>nul

or converted as a comment:

rem del temp.tmp 2>nul

the batch will also work if the file:

"temp.tmp" is not present in the same folder with the said batch file.

inclusion of this script:del temp.tmp 2>nul is still the best practice i ever think.

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

Re: help sorting list of filename by its string lenght

#21 Post by Aacini » 03 Nov 2013 22:35

@bars143:

The new version of my program below is simpler and run faster than the previous one. Please, test it.

Code: Select all

@echo off 
setlocal EnableDelayedExpansion

set len=10000
set "line="
for /F "tokens=1* delims=]" %%a in ('cmd /D /U /C type input.txt ^| find /N /V ""') do (
   if "%%b" neq "" (
      set /A len+=1
      if "%%b" neq "=" (set "line=!line!%%b") else set "line=!line!\"
   ) else if !len! neq 10000 (
      set name[!len:~-4!;!line!]=1
      set len=10000
      set "line="
   )
)

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


Antonio

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

Re: help sorting list of filename by its string lenght

#22 Post by foxidrive » 04 Nov 2013 03:09

It has a number of problematic characters - leading ] characters and ; and ! and ^ are problems.

They may not be problems in most URLs - but don't some URLs use a semicolon?

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

Re: help sorting list of filename by its string lenght

#23 Post by bars143 » 04 Nov 2013 03:19

using my machine : w7 32bit

here are the speeds test result (in seconds) between your old and new scripts:


Code: Select all

(tested to produced four files each in one run and i used stopwatch.txt to collect time-speed)

old script speedtest status:       new script speedtest status:
----------------------------       ---------------------------

first test  = 15.74               first test  = 20.15
second test = 16.82               second test = 18.91



take note of above when i repeat again - the old script slowed down while the new script run faster a second around !?
i dont know about the differences?

here is old script with added time-speeds test script and output to four files in one go:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

echo %time% >stopwatch.txt
set maxLen=80

:top
if !count! equ 5 echo %time% >>stopwatch.txt &goto :EOF

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:\==! >>old-output!count!.txt
)
set /a "count+=1"
goto:top



: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


here is new script with added time-speeds test script and output to four files:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

echo %time% >stopwatch.txt
set len=10000
set "line="

:top
if !count! equ 5 echo %time% >>stopwatch.txt &goto :EOF
for /F "tokens=1* delims=]" %%a in ('cmd /D /U /C type input.txt ^| find /N /V ""') do (
   if "%%b" neq "" (
      set /A len+=1
      if "%%b" neq "=" (set "line=!line!%%b") else set "line=!line!\"
   ) else if !len! neq 10000 (
      set name[!len:~-4!;!line!]=1
      set len=10000
      set "line="
   )
)

for /F "tokens=2 delims=;]" %%a in ('set name[') do (
   set "file=%%a"
   echo !file:\==! >>new-output!count!.txt
)

set /a "count+=1"
goto:top



thats all and thanks for new methods and its worth for learning too.

Bars

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

Re: help sorting list of filename by its string lenght

#24 Post by Aacini » 04 Nov 2013 17:33

bars143 wrote:using my machine : w7 32bit

here are the speeds test result (in seconds) between your old and new scripts:


Code: Select all

(tested to produced four files each in one run and i used stopwatch.txt to collect time-speed)

old script speedtest status:       new script speedtest status:
----------------------------       ---------------------------

first test  = 15.74               first test  = 20.15
second test = 16.82               second test = 18.91



take note of above when i repeat again - the old script slowed down while the new script run faster a second around !?
i dont know about the differences?

. . .

Bars


I can't explain those timings... :?

Well, the next faster solution is a JScript one (like REPL.BAT). The Batch-JScript hybrid program below is a direct translation of my first solution; I included the equivalent Batch code at right side as comments.

Code: Select all

@if (@CodeSection == @Batch) @then

@echo off
CScript //nologo //E:JScript "%~F0" < input.txt
goto :EOF

@end

// JScript section

var name = new Array(), file, len, a;

while ( ! WScript.Stdin.AtEndOfStream ) {             // for /F "delims=" %%a in (input.txt) do (
   file = WScript.Stdin.ReadLine();                   //    set "file=%%a"
                                                      //    call :StrLenVar file len=
   len = "000"+file.length;                           //    set len=000!len!
   name.push(len.slice(-4)+file);                     //    set name[!len:~-4!;!file!]=1
}                                                     // )
name.sort();                                          // rem In Batch the arrays are kept always sorted by index

for ( a in name ) WScript.Echo(name[a].substr(4));    //  for /F "tokens=2 delims=;]" %%a in ('set name[') do echo %%a


This solution should run faster than REPL.BAT, because it is much smaller and written specifically for this problem.

Antonio

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

Re: help sorting list of filename by its string lenght

#25 Post by bars143 » 05 Nov 2013 02:39

Aacini,

your third code in batch file is now faster than a repl in command prompt.

one input = average seconds for repl is within 0.02 secs

yours is within 0.01 secs !!!

but can you give code for a test that count :

0.00001 seconds ?

im only using %time%
but i dont how to use ping (which has 1000 milliseconds/sec) for use in "time-speed test" ?

and thanks for the fastest third code !!! :D

Bars

Post Reply