Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textformat from Url to file...

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
NO
We're writing a script wich is going to do the following :
A) Run on startup, resumbit itself
B) When resubmit is running, connect to a url and get the content.
C) Write the content from the url to a vbs file
D) Launch the vbs from C)
E) Put result back to web

I have some format problems with the text from winhttp. If I echo it out to screen, it seems fine. However, if I open the text in ex : notepad, the file contains a lot of odd characters... and the content/format is pretty far from the original vbs, or the echo result.

I've tried a couple of things with adodb.stream, but with no good result. Anyone know about a smart way to get text in proper format?


Code:
'Name : Start.vbs
'Ver. : None yet
'Changelog = 

'Syntax : Start.vbs [/t:(n)minutes] or [/Now]
'/T = Resubmit the script to run in n minutes
'/Now = Script will run imediatly

'If script launched without options, script will resumbit itself within Const DEFAULTMIN

'Script will silently terminate if wrong argument provided, or illegal combination

'Supported OS (minimum req) : 
'----------------------------
'Windows Server 2003 family 
'Windows XP SP1
'Windows 2000 SP3, (except Datacenter Server)

'-----------
'Prefix used
'-----------
'a = Array
'b = Boolean
'c = Collection
'i = Integer
'o = Object
's = String
't = Time
's_(Subname) = Sub
'f_(FunctionName) = Function
'CAPITAL LETTER = Constant

'-----
'Rules
'-----
'Code lines split before column 125

Option Explicit

Dim oShell,oFso,oWinHttpReq
Dim sArg,sInventory,sRunPath
Dim tStart
Dim iTime
Dim bNow
Dim aNamedArg

Const DEFAULTMIN	= 15
Const FORREADING	= 1
Const FORWRITING	= 2
Const URLGET		= "[URL unfurl="true"]http://someurl/script.vbs"[/URL]
Const URLPOST		= "[URL unfurl="true"]http://someurl/"[/URL]	


'Start now flag
bNow = False

'Check/Parse arguments
If Wscript.Arguments.Named.Count = 0 Then
	iTime = DEFAULTMIN
	
ElseIf Wscript.Arguments.Named.Count = 1 Then
	
	'Bind to named arguments
	Set aNamedArg = Wscript.Arguments.Named
	
	'Parse arguments
	For Each sArg in aNamedArg
		Call s_ParseArg(sArg,aNamedArg(sArg))
	Next

ElseIf Wscript.Arguments.Named.Count >= 2 Then
	wscript.quit
End If

'Create Shell object
Set oShell = Wscript.CreateObject("Wscript.Shell")

'Resubmit
If bNow = False Then
	'Assign current time + iTime min to tStart
	tStart = DateAdd("n", iTime, FormatDateTime(Now, vbShortTime))
	oShell.Run "AT \\Localhost " & tStart & " " & "Cscript.exe" & " " & Wscript.ScriptFullName & " //B //NoLogo /Now" & Chr(34), ,1
	
	'Disassosiate oShell from Object
	Set oShell = Nothing
	wscript.quit
End If

'Path to dir where script is executed
sRunPath = Left(Wscript.ScriptFullName, InStrRev(Wscript.ScriptFullName, "\") - 1)

Set oFso = CreateObject("Scripting.FilesystemObject")

Dim sDownload
sDownload = f_GetFromUrl()

Call s_CreateScript(sRunPath & "\Test.vbs")

'oShell.Popup sDownload

Set oFso = Nothing

Set oShell = Nothing

'---------------------------------------------------------------------------------------------------------------------------
'							Function Procedures
'---------------------------------------------------------------------------------------------------------------------------
Function f_GetFromUrl()
	
	'Instantiate a WinHttp object
	Set oWinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
	
	'Initialize HTTP request
	'False = Opens the HTTP connection in synchronous mode, 
	'so a call to Send does not return until WinHTTP has completely received the response.
	oWinHttpReq.Open "GET", URLGET, false

	'Send the HTTP request
	oWinHttpReq.Send()
	
	'Put result/response into function
	f_GetFromUrl = oWinHttpReq.ResponseText
	
	'Disassosiates object
	Set oWinHttpReq = Nothing
	
End Function



'---------------------------------------------------------------------------------------------------------------------------
'							Sub Procedures
'---------------------------------------------------------------------------------------------------------------------------
Sub s_CreateScript(sFile)
	
	Dim oFile
	
	Set oFile = oFso.OpenTextFile(sFile, FORWRITING, True)

	oFile.Write sDownload
	oFile.Close
	
	Set oFile = Nothing	

End Sub	

Sub s_ParseArg(sArg,aNamedArg)
	
	On Error Resume Next
	
	'Convert time argument from string to integer
	If UCase(sArg) = "T" Then
		iTime = CInt(aNamedArg)
		
		'If conversion fail, then it's a illegal value
		If err.number <> 0 Then
			wscript.quit
		End If
	ElseIf UCase(sArg) = "NOW" Then
		bNow = True
	Else
		wscript.quit
	End If	
End Sub
 
It is screwed up how? Like it is UniCode opened in a straight ASCII viewer or something else?
 
Yes, pretty much like that. If I open it in notepad, it's no good. However, if I open it using a more advanced text editor, like Ultra Edit it's fine. I can post the result tomorrow if needed.

Maybe it's a unicode/ascii... I think the file is stored on a Apache server as I binary file, I can have that verified aswell if needed.
 
JadeKnight,

You can add this function.
Code:
fuction f_chkwidth(s)
    dim a2,w2,w3
    if len(s)<3 then	'refuse, just return ascii
        f_chkwidth=vbFalse
        exit function
    end if
    a2=asc(mid(s,2,1))
    w2=ascb(midb(s,2,1))
    w3=ascb(midb(s,3,1))
    if a2<>w3 and a2=w3 then
        f_chkwidth=vbTrue
    else
        f_chkwidth=vbFalse
    end if
end function
Then in your createscript change this.
Code:
Sub s_CreateScript(sFile)
    
    Dim oFile
    
    Set oFile = oFso.OpenTextFile(sFile, FORWRITING, [red]f_chkwidth(sDownload)[/red])

    oFile.Write sDownload
    oFile.Close
    
    Set oFile = Nothing    

End Sub
regards - tsuji
 
Sorry tsuji, that did not help. But we did find the error. It was a return problem... when the *nix guy did recieve the script, it was converted without carrier return. The script is now converted to dos format, and it's working perfectly.
 
Yes, that also can be a problem. Thanks for the feedback.
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top