I´m reading a text file from my server as I should with the below, but I wonder how I can read a txt file from a different server? What do I need to do to get it working?
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("files.txt"), 1)
do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write("<br>")
loop
f.Close
Set f=Nothing
Set fs=Nothing
So this is working as it should, but I want to change the files.txt to http://www.somedomain.com/files.txt
Any input appreciated, thanks!
Claes , try this and let us know.
<% Option Explicit %>
<%
Const REMOTE_FILE_URL="http://www.somedomain.com/files.txt"
Call ShowRemoteFile
Sub ShowRemoteFile
Dim objXML, strContents, arrLines
Dim x
Set objXML=Server.CreateObject("Microsoft.XMLHTTP")
'read text file...
objXML.Open "GET", REMOTE_FILE_URL, False
objXML.Send
strContents=objXML.ResponseText
Set objXML=Nothing
'split into lines and read line by line...
arrLines=Split(strContents, VBCrLf)
For x=0 To UBound(arrLines)
Response.Write(arrLines(x)&"<br />")
Next
End Sub
%>
Use this function to fetch the text data (taken from here):
Function GetTextFromUrl(url)
Dim oXMLHTTP
Dim strStatusTest
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
oXMLHTTP.Open "GET", url, False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
GetTextFromUrl = oXMLHTTP.responseText
End If
End Function
Dim sResult : sResult = GetTextFromUrl("http://www.somedomain.com/files.txt")
Related
This topic is related to Loop through links and download PDF's
I am trying to convert my current VBA code into VBScript. I have already understood that I have to remove the variable types (As ... part of Dim statements) and use CreatObject to get those objects but otherwise everything should port as-is. DoEvents will also have to be replaced with something like Wscript.sleep.
I came up with some problems. Currently while running VBS file I am getting an error saying "Object required: 'MSHTML'". Pointing to line 65, where I have Set hDoc = MSHTML.HTMLDocument. I have tried to search on Google but got nothing helpful for this one.
How I should proceed with this one?
DownloadFiles("https://www.nordicwater.com/products/waste-water/")
Sub DownloadFiles(p_sURL)
Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim xHttp
Dim hDoc
Dim Anchors
Dim Anchor
Dim sPath
Dim wholeURL
Dim internet
Dim internetdata
Dim internetlink
Dim internetinnerlink
Dim arrLinks
Dim sLink
Dim iLinkCount
Dim iCounter
Dim sLinks
Set internet = CreateObject("InternetExplorer.Application")
internet.Visible = False
internet.navigate (p_sURL)
Do Until internet.ReadyState = 4
Wscript.Sleep 100
Loop
Set internetdata = internet.document
Set internetlink = internetdata.getElementsByTagName("a")
i = 1
For Each internetinnerlink In internetlink
If Left(internetinnerlink, 36) = "https://www.nordicwater.com/product/" Then
If sLinks <> "" Then sLinks = sLinks & vbCrLf
sLinks = sLinks & internetinnerlink.href
i = i + 1
Else
End If
Next
wholeURL = "https://www.nordicwater.com/"
sPath = "C:\temp\"
arrLinks = Split(sLinks, vbCrLf)
iLinkCount = UBound(arrLinks) + 1
For iCounter = 1 To iLinkCount
sLink = arrLinks(iCounter - 1)
'Get the directory listing
xHttp.Open "GET", sLink
xHttp.send
'Wait for the page to load
Do Until xHttp.ReadyState = 4
Wscript.Sleep 100
Loop
'Put the page in an HTML document
Set hDoc = MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
'Loop through the hyperlinks on the directory listing
Set Anchors = hDoc.getElementsByTagName("a")
For Each Anchor In Anchors
'test the pathname to see if it matches your pattern
If Anchor.pathname Like "*.pdf" Then
xHttp.Open "GET", wholeURL & Anchor.pathname, False
xHttp.send
With CreateObject("Adodb.Stream")
.Type = 1
.Open
.write xHttp.responseBody
.SaveToFile sPath & getName(wholeURL & Anchor.pathname), 2 '//overwrite
End With
End If
Next
Next
End Sub
Function:
Function getName(pf)
getName = Split(pf, "/")(UBound(Split(pf, "/")))
End Function
Instead of Set hDoc = MSHTML.HTMLDocument, use:
Set hDoc = CreateObject("htmlfile")
In VBA/VB6 you can specify variable and object types but not with VBScript. You have to use CreateObject (or GetObject: GetObject function) to instantiate objects like MSHTML.HTMLDocument, Microsoft.XMLHTTP, InternetExplorer.Application, etc instead of declaring those using Dim objIE As InternetExplorer.Application for example.
Another change:
If Anchor.pathname Like "*.pdf" Then
can be written using StrComp function:
If StrComp(Right(Anchor.pathname, 4), ".pdf", vbTextCompare) = 0 Then
or using InStr function:
If InStr(Anchor.pathname, ".pdf") > 0 Then
Also, at the beginning of your sub, you do the following:
Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim xHttp
You should declare your variables before assigning them values or objects. In VBScript this is very relaxed, your code will work because VBScript will create undefined variables for you but it's good practice to Dim your variables before using them.
Except for Wscript.sleep commands, your VBScript code will work in VB6/VBA so you can debug your script in VB6 or VBA apps (like Excel).
I am wondering is there any solution to download files from a website with VBscript?
I know how to download a single file from a website but how can I make it into a loop? Also how can I search a particular page for a certain file extension and download the file(s) if available?
For each pdf in website
xhr.open "GET", pdf.src, False
xhr.send
set stream = CreateObject("Adodb.Stream")
with stream
.type = 1
.Open
.Write xhr.responsebody
.SaveToFile "C:\temp\" + CStr(index) + ".pdf", 2
end with
stream.Close
set stream = nothing
index = index + 1
Next
Let's say we have a website https://website.com/productpage/ then there are links that all have the same structure https://website.com/products/xx-x-xx-x/ so all needed links start with https://website.com/products/. There seems to be 33 links of that kind according to source code.
Then after proceeding to some page there are PDF files. Sometimes one, sometimes 3 or 4. However link to the PDF file is something like https://website.com/wp-content/uploads/2016/12/xxxx.pdf where xxxx.pdf can actually be a filename.
Here is what I have managed to get for one file:
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "https://website.com/wp-content/uploads/2016/12/xxxx.pdf", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\temp\xxxx.pdf", 2 '//overwrite
end with
EDIT:
Should it go like:
Get all the needed links
Proceed to each link
Search for links that are ending with ".pdf"
Download files to C:\temp\
Structure of website:
https://website.com/productpage/
https://website.com/products/xx-x/
https://website.com/wp-content/uploads/2016/12/xx-xx.pdf
https://website.com/products/xxxxx-xsx/
https://website.com/wp-content/uploads/2018/12/x-xx-x.pdf
https://website.com/wp-content/uploads/2015/12/x-x-xx.pdf
https://website.com/wp-content/uploads/2019/12/xxx-x.pdf
https://website.com/products/x-xx-xsx/
https://website.com/wp-content/uploads/2014/12/x-xxx.pdf
https://website.com/wp-content/uploads/2013/12/x-x-x-x.pdf
https://website.com/products/xx-x-xsx/
https://website.com/wp-content/uploads/2012/12/x-xxxx.pdf
Since you have the code to save a link, you can wrap it into a sub for re-use:
Sub GetFile(p_sRemoteFile, p_sLocalFile)
Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = CreateObject("Adodb.Stream")
xHttp.open "GET", p_sRemoteFile, False
xHttp.Send
With bStrm
.Type = 1 '//binary
.open
.write xHttp.responseBody
.SaveToFile p_sLocalFile, 2 '//overwrite
End With
End Sub
Then, you can use the InternetExplorer object to get a collection of links in a page:
Sub GetPageLinks(p_sURL)
Dim objIE
Dim objLinks
Dim objLink
Dim iCounter
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate p_sURL
Do Until objIE.ReadyState = 4
Wscript.Sleep 100
Loop
Set objLinks = objIE.Document.All.Tags("a")
For iCounter = 1 To objLinks.Length
Set objLink = objLinks(iCounter - 1)
With objLink
If StrComp(Right(.href, 3), "pdf", 1) = 0 Then
' Get file
GetFile .href, "C:\temp\downloads\" & GetFileNameFromURL(.href)
Else
' Process page
GetPageLinks .href
End If
End With
Next
End Sub
Here's a function that extracts the file name from a URL:
Function GetFileNameFromURL(p_sURL)
Dim arrFields
arrFields = Split(p_sURL, "/")
GetFileNameFromURL = arrFields(UBound(arrFields))
End Function
This function will return xxxx.pdf given https://website.com/wp-content/uploads/2016/12/xxxx.pdf.
I am trying to read a zapier rss-feed with Classic ASP, and I cant get I to work. When I use the same script but with another RSS-url it works fine. Example with http://www.alle.se/category/z/feed/.
Is it the https that is giving me the problem?
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument")
xmlDOM.async = False
xmlDOM.setProperty "ServerHTTPRequest", True
xmlDOM.Load("https://zapier.com/engine/rss/1177776/insta-alle/")
Set itemList = XMLDom.SelectNodes("rss/channel/item")
For Each itemAttrib In itemList
response.Write(itemAttrib.SelectSingleNode("title").text & "<br>")
response.Write(itemAttrib.SelectSingleNode("link").text & "<br>")
response.Write(itemAttrib.SelectSingleNode("description").text & "<br>")
next
Set xmlDOM = Nothing
%>
Try Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument.6.0")
That calls the most recent version of MSXML
I have this function made to save a PDF file in the local disk.
How do I automatically close the window after the download completed with an alert/msg box stating that the window will close?
This doesn't work:
Response.Write ("<script>alert('Save Finished'); self.close();</script>")
TheCode:
Private Function SaveLocal1(ByRef cnCon, ByVal PDFFile)
Dim ObjStream
Dim ObjFileSys
Dim Size
Dim fpath
Dim bdata
'Response.Write("<script>alert('local');</script>")
fpath = "C:\1.pdf" 'path of the file in the local server
filename = "LocalSave.pdf" 'name to be saved
'Response.Write("<script>alert('file paths');</script>")
Set ObjStream = Server.CreateObject("ADODB.Stream")
ObjStream.Open
ObjStream.Type = 1
ObjStream.LoadFromFile fpath
'Response.Write("<script>alert('server.createobject');</script>")
Response.buffer = TRUE
Response.AddHeader "Content-Disposition","attachment;filename="& filename & ";"
Response.ContentType = "application/pdf"
Set ObjFileSys = CreateObject("Scripting.FileSystemObject")
Size = ObjFileSys.GetFile(fpath).Size
bdata = ObjStream.Read(Size)
Response.BinaryWrite(bdata) 'the file will be saved in the downloads folder
Response.Write("<script>alert('Save to local finish');</script>")
ObjStream.Close
Set ObjStream = Nothing
Response.Flush
Response.End
Response.Write ("<script>alert('Save Finished'); self.close();</script>")
End Function
I suppose there are could be 2 issue in your code.
2nd issue:
Response.End
Response.Write ("<script>alert('Save Finished'); self.close();</script>")
Response.End -- is last command. That is all. "Document" completed.
The End method causes the Web server to stop processing the script and
return the current result. The remaining contents of the file are not
processed.
Change order of following comand to
Response.Write ("<script>alert('Save Finished'); self.close();</script>")
Response.End
Use response.redirect and send this page to another page, use window.close on body onload
Is there an equivalent to PHP's get_included_files in classic ASP?
No, there is not.
A very ugly function for that:
<!--#include file="include/common.asp"-->
<%
Function GetIncludedFiles()
Dim Url
Dim Fso
Dim Fs
Dim Src
Dim Arr
Dim Ret
Dim i
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
ReDim Ret(-1)
Url = Request.ServerVariables("URL")
Set Fs = Fso.OpenTextFile(Server.MapPath(Url))
Src = Fs.Readall()
Fs.Close
Set Fs = Nothing
Set Fso = Nothing
Arr = Split(Src, "<" & "!--#include file=")
For i = 0 To UBound(Arr)
Arr(i) = Left(Arr(i), InStr(Arr(i), "-->"))
Arr(i) = Replace(Arr(i), "-", "")
Arr(i) = Replace(Arr(i), "'", "")
Arr(i) = Trim(Replace(Arr(i), """", ""))
If Arr(i) <> "" Then
ReDim Preserve Ret(UBound(Ret) + 1)
Ret(UBound(Ret)) = Arr(i)
End If
Next
GetIncludedFiles = Ret
End Function
Dim File
For Each File In GetIncludedFiles()
Response.Write File & "<br />"
Next
%>
The simple way is to create a main file in a specific directory (for example /include/mainfile.asp) and then include all the other files to this file. Something like:
<!#include File="[your directory here/file1.asp]"-->
<!#include File="[your directory here/file2.asp]"-->
<!#include File="[your directory here/file3.asp]"-->
Then, You can include your main file using "virtual" to the rest of your pages that you want to access those other included files.
<!#include Virtual="/include/mainfile.asp"-->
Not as such, but I vaguely remember seen a tool or two floating around that will give you the equivalent report. It might have been on Code Project or somewhere similar... its been a long time since I last ran across it.