Feeds:
Posts
Comments

Posts Tagged ‘VBScript’

Deleting a file is fairly simple in VBScript. First you’ll want to declare a File System Object so that you can interact with the file system. I’ve usually got a line near the top of my scripts

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Then you can delete a file easily using DeleteFile:

objFso.DeleteFile “C:\Temp\temp.file”

Read Full Post »

How to run VBScript

There are a few ways to run VBscript. I will attempt to outline some methods briefly.
Firstly, VBScript is generally saved into a file with extension .vbs (eg MyScript.vbs), some other file types are .vbe, .wsf, .wsc, but I primarily deal with .vbs. VBScript does not need to be compiled, so these files can be human read.

My primary method of running or executing VBScript is to open a command prompt, then to type cscript then the name of the script. If the script file is in a different folder (eg c:\Temp) you would need to type cscript C:\Temp\MyScript.vbs

You can use wscript rather than cscript in a command prompt, but the main difference is that if there are any dialogue boxes or output, cscript will write this into the command window, but wscript will create a dialogue box which you probably need to click for the script to continue. I generally write scripts that shouldn’t need any interaction, so cscript is better for me.


Another option is to double click on the script in explorer. This will run wscript and use that directory as the working directory. This is great for scripts where you want interaction, but it means you can’t easily do something tricky like provide parameters.

Hope this brief overview helps!

Read Full Post »

VBScript edit line in file

If you want to edit a specific line in a file through VBScript you can use something like below. This reads the original file line by line and writes it out to a temporary file. If the original line meets the condition you want, make the changes required. At the end, close both files, delete the original and move the temp file.

Const ForReading=1
Const ForWriting=2
Set objFSO = CreateObject("Scripting.FileSystemObject")
folder = "C:\Program Files\Vendor\"
filePath = folder & "file.txt"
Set myFile = objFSO.OpenTextFile(filePath, ForReading, True)
Set myTemp= objFSO.OpenTextFile(filePath & ".tmp", ForWriting, True)
Do While Not myFile.AtEndofStream
 myLine = myFile.ReadLine
 If InStr(myLine, "Variable=") Then
  myLine = "Variable="&whatever
 End If

 myTemp.WriteLine myLine
Loop
myFile.Close
myTemp.Close
objFSO.DeleteFile(filePath)
objFSO.MoveFile filePath&".tmp", filePath

Read Full Post »

In looking for a way to retrieve the ProductCode from an MSI I came across this. Two important points: One, this uses the windows installer object and can be queried using SQL effectively, Two, the different parameters for opening the database can be found in the MSDN, but I found 2 worked better than 0.

 

Function GetMSIProductCode(msi) ' Return the Product Code from a given msi
 On Error Resume Next
 GetMSIProductCode=""
 If msi = "" Then Exit Function End If
 
 Dim FS, TS, WI, DB, View, Rec
 Set WI = CreateObject("WindowsInstaller.Installer")
 Set DB = WI.OpenDatabase(msi,2)
 If Err.number Then Exit Function End If
 
 Set View = DB.OpenView("Select `Value` From Property WHERE `Property` ='ProductCode'")
 View.Execute
 Set Rec = View.Fetch
 If Not Rec Is Nothing Then
  GetMSIProductCode=Rec.StringData(1)
 End If
End Function

Read Full Post »

VBScript – get file version

I was looking for a way to find a file version with vbscript and found this, which should work for .dlls and .exes:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
file = “C:\windows\file.exe”
Wscript.Echo objFSO.GetFileVersion(file)

Easy!

Read Full Post »

Something neat I did was working with command line parameters in VBScript. You can access the command line arguments array through

Wscript.Arguments(i)

where i goes from 0 upwards. So, if you’ve got one argument option you could handle it like this

If Wscript.Arguments.Count = 0 Then
    ‘ Do Proceed as Normal
Else
    If Wscript.Arguments(0) = “/something” Then
         RunFunctionA
  End If
End If

If you want to have multiple parameters (the script I did had /nolog & /reset), then you’ll want to process all the arguments before you proceed. So, I went through the array and turned on flags so I could act afterwards. If you don’t do this, you might end running something you don’t want or in a way you don’t want. You could process your arguments like this:

If Wscript.Arguments.Count = 0 Then
    ‘ Do Proceed as Normal
Else
    For i = 0 to Wscript.Arguments.Count – 1
        If Wscript.Arguments(i) = “/reset” Then
         resetflag=1 ‘Turn on the reset flag 
       ElseIf Wscript.Arguments(i) = “/nolog” Then
         loggingflag = 0 ‘Turn off the logging flag 
  End If
    Next
End If

If loggingFlag =0 Then
  ‘Don’t log
Else
 LogMyStuff ‘ Call the function you want
End If

‘==========================================================================
‘If reset command line parameter is detected, run the reset function only, then quit
‘==========================================================================
If resetFlag Then
 Reset
 WScript.Quit
End If

Read Full Post »