[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

How ot use a script file to download a file ?

I want to download a file from the web
like http://www.abc.com/file.txt , use the script file.
but there seems no object support this function.
0
Sam80
Asked:
Sam80
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Sam80Author Commented:
Thanks , aelatik .
The function is just what i want.
but in vb script ,I can not use the window api function,which force me had to find out an object that can attain this requirement.

I will be greatly appreciate to your new answer.
0
 
emadatCommented:
Copy this in a module and use it.
Usage:
sString = GetHTML("http:\\www.domain_name.com")

'====================================================
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_DEFAULT_HTTP_PORT = 80
Public Const INTERNET_SERVICE_HTTP = 3
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public Const HTTP_QUERY_STATUS_CODE = 19
Public Const HTTP_ADDREQ_FLAG_ADD = &H20000000

Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" (ByVal hHttpSession As Long, ByVal sVerb As String, ByVal sObjectName As String, ByVal sVersion As String, ByVal sReferer As String, ByVal something As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
'Public Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, sOptional As Any, ByVal lOptionalLength As Long) As Long
Public Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal sOptional As String, ByVal lOptionalLength As Long) As Long
Public Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long, ByRef sBuffer As Any, ByRef lBufferLength As Long, ByRef lIndex As Long) As Long
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Declare Function HttpAddRequestHeaders Lib "wininet.dll" Alias "HttpAddRequestHeadersA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lModifiers As Long) As Integer
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumberOfBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternet&, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength&, ByVal dwFlags&, ByVal dwContext&) As Long


Public Function GetHTML(strURL As String) As String
Const BufferSize = 16384
Dim hSession&, hURL&
Dim Buffer As String * BufferSize
Dim BufferLen&
Dim sAppName$, sResult$
   sAppName = "MyApplicationName"
   hSession = InternetOpen(sAppName, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
   hURL = InternetOpenUrl(hSession, strURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
   sResult = ""
   Do
       InternetReadFile hURL, Buffer, Len(Buffer), BufferLen
       If BufferLen > 0 Then sResult = sResult & Left(Buffer, BufferLen)
   Loop Until BufferLen = 0
   GetHTML = sResult
   InternetCloseHandle hURL
   InternetCloseHandle hSession
End Function
'====================================================
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
emadatCommented:
This function will work from VBS. It relies on MS XML

However, It might need some modification in creating the object. Try the commented line if the first did not work

Function GetHTML(sURL As String) As String
Dim objXMLHTTP, xml
    Set xml = CreateObject("Microsoft.XMLHTTP")
'    Set xml = CreateObject("MSXML2.XMLHTTP")
    xml.Open "GET", sURL, False
    xml.send
    GetHTML = xml.responseText
    Set xml = Nothing
End Function

Good luck
0
 
aelatikCommented:
If its just for text and other type of pages use :

Dim IE
Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "http://www.abc.com/file.txt"
    While IE.Busy
    Wend
       GetHTMLCode = IE.Document.documentelement.innerHTML
MsgBox GetHTMLCode
0
 
Richie_SimonettiIT OperationsCommented:
hearing...
0
 
Sam80Author Commented:
thanks , guys, excellent!!!

and now i want one more function is that save the file to the specific folder , how ?
0
 
Sam80Author Commented:
how about if my request file is not a text file ?
it might be any kind such as exe,jpg and so on.
0
 
CleanupPingCommented:
Hi Sam80,
This old question (QID 20562465) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now