Does VBScript allow named arguments in function calls?

VBScript doesn’t support named arguments to procedures and functions. You need to change the argument list to positional: Workbooks.OpenText Filename_Argument, xlMSDOS, … VBScript also doesn’t recognize Excel constants (like xlMSDOS), so you need to look them up and replace them with their numeric values: Workbooks.OpenText Filename_Argument, 3, … And you must use explicit object references: … Read more

Download a file with VBS

You can download using XMLHTTP and leverage an ADO stream to write the binary data; dim xHttp: Set xHttp = createobject(“Microsoft.XMLHTTP”) dim bStrm: Set bStrm = createobject(“Adodb.Stream”) xHttp.Open “GET”, “http://example.com/someimage.png”, False xHttp.Send with bStrm .type = 1 ‘//binary .open .write xHttp.responseBody .savetofile “c:\temp\someimage.png”, 2 ‘//overwrite end with

Read and write into a file using VBScript

You can create a temp file, then rename it back to original file: Set objFS = CreateObject(“Scripting.FileSystemObject”) strFile = “c:\test\file.txt” strTemp = “c:\test\temp.txt” Set objFile = objFS.GetFile(strFile) Set objOutFile = objFS.CreateTextFile(strTemp,True) Set ts = objFile.OpenAsTextStream(1,-2) Do Until ts.AtEndOfStream strLine = ts.ReadLine ‘ do something with strLine objOutFile.Write(strLine) Loop objOutFile.Close ts.Close objFS.DeleteFile(strFile) objFS.MoveFile strTemp,strFile Usage is … Read more

Read and write binary file in VBscript

Based on Luc125 and Alberto answers here are the 2 reworked and simplified functions: The Read function Function readBinary(strPath) Dim oFSO: Set oFSO = CreateObject(“Scripting.FileSystemObject”) Dim oFile: Set oFile = oFSO.GetFile(strPath) If IsNull(oFile) Then MsgBox(“File not found: ” & strPath) : Exit Function With oFile.OpenAsTextStream() readBinary = .Read(oFile.Size) .Close End With End Function The Write … Read more

Getting current directory in VBScript

You can use WScript.ScriptFullName which will return the full path of the executing script. You can then use string manipulation (jscript example) : scriptdir = WScript.ScriptFullName.substring(0,WScript.ScriptFullName.lastIndexOf(WScript.ScriptName)-1) Or get help from FileSystemObject, (vbscript example) : scriptdir = CreateObject(“Scripting.FileSystemObject”).GetParentFolderName(WScript.ScriptFullName)

Can I pass an argument to a VBScript (vbs file launched with cscript)?

You can use WScript.Arguments to access the arguments passed to your script. Calling the script: cscript.exe test.vbs “C:\temp\” Inside your script: Set File = FSO.OpenTextFile(WScript.Arguments(0) &”\test.txt”, 2, True) Don’t forget to check if there actually has been an argument passed to your script. You can do so by checking the Count property: if WScript.Arguments.Count = … Read more