Solved

adding a loop for a script, help :P

Posted on 2004-08-16
3
328 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

17 Experts available now in Live!

Get 1:1 Help Now