Solved

adding a loop for a script, help :P

Posted on 2004-08-16
3
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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