Solved

adding a loop for a script, help :P

Posted on 2004-08-16
3
332 Views
Last Modified: 2012-08-14
Hello.
I am trying to create a script that will pull some data from the amazon web services site.
I have most of it done, but i ran into a problem that i have no idea how to solve.

Here is the code:


<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit
Dim aID, devToken, xmlURL, strASIN, strType, xmlHTTP, xmlDoc, arrDetails, i
Dim xmlResponse
Dim FSO
Dim myFile
set FSO = Server.CreateObject("scripting.FileSystemObject")
set myFile = fso.CreateTextFile("G:\test.txt", true)



'// Your Amazon Associates' ID
aID = "aspit-20"

'// Your Developers Token
devToken = "none"

'// The ASIN of the product
strASIN = "1590591062"

'// The type of search (lite|heavy)
strType = "lite"

'// XML URL
xmlURL = "http://xml.amazon.com/onca/xml3" & _
"?t=" & aID & _
"&dev-t=" & devToken & _
"&AsinSearch=" & strASIN & _
"&type=" & strType & _
"&f=xml"

'// Create the XMLHTTP object
Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

'// Fire off the request!
xmlHTTP.Open "GET", xmlURL, False
xmlHTTP.Send

'// Check if everything's okay
If xmlHTTP.Status = 200 Then
      xmlResponse = xmlHTTP.ResponseXML.xml

      '// Load the XML parser
      Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
      xmlDoc.Async = False
      xmlDoc.loadXML (xmlResponse)
      
      '// Check for error message
      If IsBlank(xmlDoc.selectSingleNode("//ErrorMsg")) = False Then
            Response.Write "An error occurred. Sorry"
      Else
            '// Set the Details node
            Set arrDetails = xmlDoc.selectSingleNode ("//Details")
            
            '// Show product details
                  myFile.WriteLine( arrDetails.SelectSingleNode ("Asin").Text )  & vbtab & ( arrDetails.SelectSingleNode

("ProductName").Text ) & vbtab & ( arrDetails.SelectSingleNode ("ListPrice").Text ) & vbtab & ( arrDetails.SelectSingleNode

("OurPrice").Text ) & vbtab & ( arrDetails.SelectSingleNode ("UsedPrice").Text )


                  myFile.WriteLine( arrDetails.SelectSingleNode ("Asin").Text )


            Response.Write "<b>" & arrDetails.selectSingleNode ("ProductName").Text & "</b><br>"
            Response.Write "By "
            
            '// Loop through each author
            For Each i In arrDetails.selectSingleNode ("Authors").ChildNodes
                  Response.Write i.Text & ", "
            Next
      End If
      
      '// Unload XML parser
      Set xmlDoc = Nothing
Else
      Response.Write "An error occurred. Sorry"
End If

'// Destroy the object
Set xmlHTTP = Nothing

'// Generic function to check whether something is empty or not
Function IsBlank(byref TempVar)
      Dim tmpType
      IsBlank = False

      select Case VarType(TempVar)
      
            'Empty & Null
            case 0,1
                  IsBlank = true
            
            'Strings
            case 8
                  if Len(TempVar) = 0 Then
                        IsBlank = True
                  end If
            
            'Object
            case 9
                  tmpType = TypeName(TempVar)
                  If (tmpType = "Nothing") Or (tmpType = "Empty") Then
                        IsBlank = True
                  End If
            
            'Arrays
            case 8192, 8204, 8209
                  'does it have any dimensions?
                  if UBound(TempVar) = -1 Then
                        IsBlank = True
                  end If

            'Anything else            
            case Else
                  IsBlank = False
      end Select
End Function
%>

-----------------------end code--------------------------

strAsin is the thing i am working on. I have a .txt file full of asin numbers. the goal of the script is to read that .txt file, query each number, and output the results to my text file.
The text file get written to fine, and the script as a whole works fine as it is now, but the asin is hard coded. I need to figure out a way to make it read the list, and do each number, then write the results. if anyone can help, please do :D thanks

-Shane
0
Comment
Question by:bugnuker
3 Comments
 
LVL 7

Accepted Solution

by:
j2nku earned 500 total points
ID: 11815574
<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit
Dim aID, devToken, xmlURL, strASIN, strType, xmlHTTP, xmlDoc, arrDetails, i
Dim xmlResponse
Dim FSO
Dim myFile, asinfile
set FSO = Server.CreateObject("scripting.FileSystemObject")
set myFile = fso.CreateTextFile("G:\test.txt", true)
set asinfile = fso.OpenTextFile("G:\somedir\myASINs.txt") ' you wrote you have a txt file full of numbers - write the path to it here

' == You didn't write how the ASIN numbers are written in the file.
' == I'll write 2 examples:

' // Ex. 1 - if the numbers are separated by space, comma, etc - no linebreaks

asins = Split(asinfile.readall," ") ' == Here i assume that numbers are separated with one blank space
For Each asinnumber in asins
  if isnumeric(trim(asinnumber)) then GetProdInfo trim(asinnumber)
next

' // Ex. 2 - if each line has one number

Do While Not asinfile.atEndOfStream
  asinnumber = trim(asinfile.readline)
  if isnumeric(asinnumber) Then GetProdInfo asinnumber
loop

Set asinfile = nothing

Function GetProdInfo(ASIN)

'// Your Amazon Associates' ID
aID = "aspit-20"

'// Your Developers Token
devToken = "none"

'// The ASIN of the product
strASIN = ASIN

'// The type of search (lite|heavy)
strType = "lite"

'// XML URL
xmlURL = "http://xml.amazon.com/onca/xml3" & _
"?t=" & aID & _
"&dev-t=" & devToken & _
"&AsinSearch=" & strASIN & _
"&type=" & strType & _
"&f=xml"

'// Create the XMLHTTP object
Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

'// Fire off the request!
xmlHTTP.Open "GET", xmlURL, False
xmlHTTP.Send

'// Check if everything's okay
If xmlHTTP.Status = 200 Then
     xmlResponse = xmlHTTP.ResponseXML.xml

     '// Load the XML parser
     Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
     xmlDoc.Async = False
     xmlDoc.loadXML (xmlResponse)
     
     '// Check for error message
     If IsBlank(xmlDoc.selectSingleNode("//ErrorMsg")) = False Then
          Response.Write "An error occurred. Sorry"
     Else
          '// Set the Details node
          Set arrDetails = xmlDoc.selectSingleNode ("//Details")
         
          '// Show product details
                  myFile.WriteLine( arrDetails.SelectSingleNode ("Asin").Text )  & vbtab & ( arrDetails.SelectSingleNode

("ProductName").Text ) & vbtab & ( arrDetails.SelectSingleNode ("ListPrice").Text ) & vbtab & ( arrDetails.SelectSingleNode

("OurPrice").Text ) & vbtab & ( arrDetails.SelectSingleNode ("UsedPrice").Text )


                  myFile.WriteLine( arrDetails.SelectSingleNode ("Asin").Text )


          Response.Write "<b>" & arrDetails.selectSingleNode ("ProductName").Text & "</b><br>"
          Response.Write "By "
         
          '// Loop through each author
          For Each i In arrDetails.selectSingleNode ("Authors").ChildNodes
               Response.Write i.Text & ", "
          Next
     End If
     
     '// Unload XML parser
     Set xmlDoc = Nothing
Else
     Response.Write "An error occurred. Sorry"
End If

'// Destroy the object
Set xmlHTTP = Nothing

End Function

'// Generic function to check whether something is empty or not
Function IsBlank(byref TempVar)
     Dim tmpType
     IsBlank = False

     select Case VarType(TempVar)
     
          'Empty & Null
          case 0,1
               IsBlank = true
         
          'Strings
          case 8
               if Len(TempVar) = 0 Then
                    IsBlank = True
               end If
         
          'Object
          case 9
               tmpType = TypeName(TempVar)
               If (tmpType = "Nothing") Or (tmpType = "Empty") Then
                    IsBlank = True
               End If
         
          'Arrays
          case 8192, 8204, 8209
               'does it have any dimensions?
               if UBound(TempVar) = -1 Then
                    IsBlank = True
               end If

          'Anything else          
          case Else
               IsBlank = False
     end Select
End Function
%>

'---------------------- good luck!
0
 
LVL 8

Expert Comment

by:fozylet
ID: 12723551
thanks bugnuker, your gesture is appreciated.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question