Help please - sort one url per line with batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: Help please - sort one url per line with batch file

#16 Post by Ben Mar » 20 Jun 2015 09:37

foxidrive wrote:
Ben Mar wrote:I don't know why the output is not working properly in the original post. Here is the new fixed:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,*" %%A in (long_url.txt) do (
   echo %%A>url.lst
   if "%%B"=="" goto :eof
   set nextURL=%%B
   :Begin
   for /f "tokens=1,*" %%F in ("!nextURL!") do (
      echo %%F>>url.lst
      if "%%F"=="" goto :eof
      set nextURL=%%G
      goto Begin
   )
)
type url.lst


To use your technique, this is all that's needed.

Code: Select all

@echo off
del url.txt 2>nul
:begin
for /f "tokens=1,*" %%A in (long_url.txt) do (
   echo %%A>>url.lst
   if not "%%B"=="" echo %%B>long_url.txt & goto :begin
)
type url.lst
pause


Thanks for the simplication method.

Ben Mar
Posts: 22
Joined: 03 May 2015 10:51

Re: Help please - sort one url per line with batch file

#17 Post by Ben Mar » 20 Jun 2015 09:38

Aacini wrote:
Ben Mar wrote:
foxidrive wrote:AFAIK you can't have a goto loop within a for construct, Ben.
I didn't test your code but unless you've found a way to do it, it's going to fall out of the for loop.


First time to hear about it. But anyway in this code I've tested so far I didn't see any wrong result yet.


When a GOTO is executed inside a ( code block ) any pending commands that comprise the code block are cancelled; this include any pending FOR iterations, any ELSE part of nested IF's, and any redirections made to the block. The execution jump to the label and continue from there like if the label were placed outside any block, so any closing parentheses found are just ignored.

In your code this point not affect the result because the GOTO is placed at the end of a FOR /F that not iterate; however, the redirection to the output file is also cancelled, so the results after the second one (after the GOTO) are not redirected to the file, but shown on the screen. Of course, the way to solve this point is appending each line to the output file, but in this case the code may also be rewritten so the :Begin label be placed outside of the first FOR.

Anyway, your code fail to fulfill the core point of this problem: a FOR command can not read a line if it is longer than 8 KB. You may test this point in an easy way: just copy the data in your test case 10 times, so the resulting file is 9900 characters long, and run your program with it...

Antonio


Thanks for the clarification. Something to learn from DOS world! :D

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

Re: Help please - sort one url per line with batch file

#18 Post by Aacini » 20 Jun 2015 09:49

philip123: Forget what I said in my previous post :!:

In the first post of this thread you said:

philip123 wrote:I have a text file containing hundreds of website url's in it, they are all separated by a single spaces.

Well, this is not true: your file have 1403 lines! That is, it have long lines with url's separated by spaces, but in 1403 cases there are two url's separated by a new line. :shock:

This is an entirely different problem...

Antonio

PS - I suggest you to carefully read this post


EDIT

The longest line in your file is 273 characters long! This means that you may solve your problem with my first solution:

Code: Select all

@echo off

(for /F "delims=" %%a in (input.txt) do (
   for %%b in (%%a) do echo %%b
)) > output.txt


:evil:

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Help please - sort one url per line with batch file

#19 Post by Compo » 30 Jun 2015 09:29

Powershell command from batch/cmd:

Code: Select all

Powershell -C "GC .\URL.txt | ForEach {$_.split()}">URLCol.txt
make sure that you are running it from the same location/along side URL.txt

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

Re: Help please - sort one url per line with batch file

#20 Post by Aacini » 30 Jun 2015 11:08

I completed a very simple test with the two last solutions and the data file provided by the OP. This is the Batch test file:

Code: Select all

@echo off

echo %time%
(for /F "delims=" %%a in (URL.txt) do (
   for %%b in (%%a) do echo %%b
)) > URLCol.txt
echo %time%

and this the PowerShell one:

Code: Select all

@echo off

echo %time%
Powershell -C "GC .\URL.txt | ForEach {$_.split()}">URLCol.txt
echo %time%

I run each one 3 times; this is the output plus some calculations:

Code: Select all

Batch:

11:45:08.89
11:45:10.75 = 1.86

11:45:20.15
11:45:21.51 = 1.36

11:45:23.66
11:45:25.08 = 1.42

Average: 1.55 seconds


PS:

11:48:35.53
11:48:51.32 = 15.79

11:48:54.43
11:49:04.30 = 9.87

11:49:06.19
11:49:15.97 = 9.78

Averge: 11.81 seconds

This means that the PS solution takes 7.62 times more time than the Batch one, or that the Batch solution run in 13% of the PS time.

Antonio

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Help please - sort one url per line with batch file

#21 Post by Compo » 30 Jun 2015 12:23

Whilst fighting your batch corner Antonio, (even though no one, especially myself rubbished it), bear two things in mind:
  1. This isn't and never will be a time sensitive operation
  2. You didn't include the time it took you to read in the longest line character count of the input file in order to determine if your solution would work!

Post Reply