How to Count a list of URLs stored in a Variable

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
clickfast
Pro Scripter
Posts: 58
Joined: Wed May 23, 2007 12:04 am

How to Count a list of URLs stored in a Variable

Post by clickfast » Thu Jun 14, 2007 4:54 am

Hi did due diligence combing thru forums and found no referrences to solving this.

I have a variable that contains anywhere from 1 up to 5 URLs separated by ';' (using the SEPARATE command)..

What I want to do is count the number of urls and execute IF/Else statements based on the number of URLs found?

I was thinking about using a Read Line function but I'm so new to this I don't know if that would be barking up the wrong tree...

Ideas?

Thanks!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jun 14, 2007 1:31 pm

The Help is your friend :wink: this is the example code. Separate reads the file names into an array called file_names and file_names_count will automagically contain the number of items.

GetFileList>c:\temp\*.*,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count
Label>end

clickfast
Pro Scripter
Posts: 58
Joined: Wed May 23, 2007 12:04 am

Post by clickfast » Thu Jun 14, 2007 7:29 pm

Ahhhh... thanks! Will check help first next time! :wink:
Me_again wrote:The Help is your friend :wink: this is the example code. Separate reads the file names into an array called file_names and file_names_count will automagically contain the number of items.

GetFileList>c:\temp\*.*,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count
Label>end

clickfast
Pro Scripter
Posts: 58
Joined: Wed May 23, 2007 12:04 am

Post by clickfast » Sat Jun 16, 2007 9:08 am

Hello Me_Again, so I tried to use this code right out the box. Substituted c:\temp for the correct path of my file. Tried both the GetFileList and ReadFile functions and both will capture contents into variable. The SEPARATE command delimits just fine.

But, the MessageModal line returns a value of 1 and I know that there are 5 items in the list.

Any ideas?

What I'm trying to do is read a text file with anywhere from 1 to 5 URLs and based on the number of URLs, execute a serious of IF statements.

e.g.
IF>URLcount=1
do something
If>URLcount=2
do something else
etc..etc...

Me_again wrote:The Help is your friend :wink: this is the example code. Separate reads the file names into an array called file_names and file_names_count will automagically contain the number of items.

GetFileList>c:\temp\*.*,files
Separate>files,;,file_names
MessageModal>Num Files: %file_names_count%
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
Message>file_names_%k%
Until>k,file_names_count
Label>end

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sat Jun 16, 2007 6:19 pm

Hi clickfast,

If you are reading the contents of a single file you'll need to modify the example slightly. Instead of using GetFileList>, which reads a directory and creates a list containing the names of all the files located in that directory, try using Readfile> which will read the contents of a particular file.

Code: Select all

ReadFile>C:\Path\URL_List_Filename,;,URL_List
MessageModal>Num URLs: %URL_List_count%
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
Message>URL_List_%k%
Until>k,URL_List_count
Label>end

clickfast
Pro Scripter
Posts: 58
Joined: Wed May 23, 2007 12:04 am

Post by clickfast » Sat Jun 16, 2007 7:06 pm

Sorry Guys, I just can't get any of these to work. So I'm pasting the source of my test file.

Copy and past the following dummy URLs into a .txt file and name it urls.txt and save it to c:\

Then try to use the following script based on recommendations in this thread.

Code: Select all

ReadFile>C:\urls.txt,;,URL_List
MessageModal>Num URLs: %URL_List_count%
If>file_names_count=0,end
Let>k=0
Repeat>k
Let>k=k+1
Message>URL_List_%k%
Until>k,URL_List_count
Label>end
[/quote]


I can't get this to work using the above .txt file and the script above.

JRL wrote:Hi clickfast,

If you are reading the contents of a single file you'll need to modify the example slightly. Instead of using GetFileList>, which reads a directory and creates a list containing the names of all the files located in that directory, try using Readfile> which will read the contents of a particular file.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Jun 16, 2007 9:07 pm

You seem to have an extra semicolon:

ReadFile>C:\urls.txt,;,URL_List

Syntax is ReadFile>SourceFile,DestinationVariable
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Sat Jun 16, 2007 11:19 pm

You aren't using GetFileList> which is a special case because it produces a ";" delimited output. With ReadFile>, even with Bob's fix, all you have done is read the whole list of URL's into a variable. To display them separately you'll need to separate them and you can do that with the CRLF that's between the lines of the original file:

ReadFile>C:\urlstest.txt,URL_Text
MDL>URL_Text
Separate>URL_Text,CRLF,URL_List
MDL>%URL_List_count%
Let>k=0
Repeat>k
Let>k=k+1
MDL>URL_List_%k%
Until>k,URL_List_count
Label>end

clickfast
Pro Scripter
Posts: 58
Joined: Wed May 23, 2007 12:04 am

Post by clickfast » Sun Jun 17, 2007 6:28 pm

The question below is answered in this post http://www.mjtnet.com/usergroup/viewtopic.php?t=3922
So now I'd like to perform a few IF statements based on the number of URLs.

Example
IF the URL Counts =1 then do something
If the URL Counts = 2 then do something
If the URL Counts = 3 then do something
etc...

What is the best way to structure this? Also, how do I pass the %URL_List_count% variable to the IF statements?

Thanks!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts