Solved

adding a loop for a script, help :P

Posted on 2004-08-16
3
325 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
Comment Utility
<%@ 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
Comment Utility
thanks bugnuker, your gesture is appreciated.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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/…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now