VBScript

Share interesting information or links related to portable apps here.
Message
Author
User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

VBScript

#1 Post by guinness »

I was updating the function I created called _SelfDelete the other day and a user posted that why not do this in VBScript rather than a batch file. So because I was curious I had a look at VBScript and to my surprise the syntax is very similar in certain parts to AutoIt, so for some scripts I use (Notepad2) that I've created in AutoIt I will probably migrate to VBScript. I love a challenge!

I was wondering does anyone have a good resource of examples or for learning VBScript? e.g.

>> http://ss64.com/vb/syntax.html
>> http://www.robvanderwoude.com/wshexamples.php

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#2 Post by guinness »

This VBScript function will return True/False if the OS Architecture is x64.

Code: Select all

MsgBox Is64Bit()

'~ By guinness (c) 2012.
Function Is64Bit()
	Dim oShell, sRegRead
	Set oShell = Wscript.CreateObject("Wscript.Shell")
	sRegRead = oShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
	Is64Bit = InStr(sRegRead, "64") > 0
End Function
Last edited by guinness on Sat Feb 18, 2012 2:17 pm, edited 2 times in total.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#3 Post by guinness »

Reading the resources above I came up with a different approach of hiding the cmd prompt window.

Save as 'RunHide.vbs'.

Code: Select all

Option Explicit
Private oShell, sCmdLineRaw

'~ The idea was derived from http://www.robvanderwoude.com
'~ How to run: RunHide.vbs "APP_NAME" "APP_COMMMANDARGS"
'~ By guinness (c) 2012.
If IsCmdLine Then
	sCmdLineRaw = CmdLineRaw()
	Set oShell = CreateObject("WScript.Shell")
	oShell.Run Trim(sCmdLineRaw), 0, False
	Set oShell = Nothing
	WScript.Quit 1
Else
	MsgBox "Not enough parameters were specified.", vbOKOnly, "RunHide - Error"
	WScript.Quit 0
End If

Function CmdLineRaw()
	Dim i, sCmdLine
	For i = 0 To WScript.Arguments.Count - 1
		sCmdLine = sCmdLine & " " & WScript.Arguments(i)
	Next
	CmdLineRaw = sCmdLine
End Function

Function IsCmdLine()
  IsCmdLine = WScript.Arguments.Count > 1
End Function
Please note I've never coded/scripted** in VBScript so there may be some mistakes when it comes to 'coding practice', though all examples I provide work.

** - I am Baas pointed out I should've put 'scripted' rather than 'coded'.
Last edited by guinness on Tue Feb 14, 2012 3:22 pm, edited 4 times in total.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#4 Post by guinness »

Temporarily replace Notepad.exe with Notepad2.exe. To add Notepad2 as a replacement run the following code below and to remove just run the code again. I also have an AutoIt version here >> http://www.portablefreeware.com/forums/ ... 074#p47074

Save as 'Notepad2 Takeover.vbs'.

Code: Select all

Option Explicit
Dim iError, oFileSystem, oShell, sFilePath, sRegRead, sString, sWow6432Node

'~ By guinness (c) 2012.
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
sWow6432Node = ""

If Is64Bit() Then
	sWow6432Node = "Wow6432Node\"
End If
sRegRead = "HKEY_LOCAL_MACHINE\SOFTWARE\" & sWow6432Node & "Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe\Debugger"

sRegRead = ReadRegKey(sRegRead)

If sRegRead = "[None]" Then
	iError = 0
	sFilePath = oFileSystem.GetParentFolderName(WScript.ScriptFullName) & "\Notepad2.exe"
Else
	iError = 1
	sRegRead = Replace(sRegRead, """", "")
	sFilePath = Trim(Replace(sRegRead, "/z", ""))
End If

Set oShell = Wscript.CreateObject("WScript.Shell")
If oFileSystem.FileExists(sFilePath) = False Then
	MsgBox "It appears Notepad2 is missing.", vbOKOnly + vbCritical, "Notepad2 Takeover - Error"
	WScript.Quit 1
End If
If iError = 1 Then
	oShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\" & sWow6432Node & "Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe\"
	sString = "Removed Notepad2 from the system."
Else
	oShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\" & sWow6432Node & "Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe\Debugger", """" & sFilePath & """ /z", "REG_SZ"
	sString = "Add Notepad2 to the system."
End If
MsgBox sString, vbOKOnly + vbCritical, "Notepad2 Takeover - Complete"

Function Is64Bit()
	Dim oShell, sRegRead
	Set oShell = Wscript.CreateObject("Wscript.Shell")
	sRegRead = oShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
	Is64Bit = InStr(sRegRead, "64") > 0
End Function

'~ Taken From: http://www.visualbasicscript.com/Verify-registry-key-exists-m1432.aspx
Private Function ReadRegKey(ByVal pRegKey)
	On Error Resume Next
	Dim oWsShell, sReturn

	Set oWsShell = Wscript.CreateObject("WScript.Shell")
	sReturn = oWsShell.RegRead(pRegKey)

	If Err.Number <> 0 Then
		sReturn = "[None]"
	End If
	ReadRegKey = sReturn

	Set oShell = Nothing

	On Error GoTo 0
 End Function
I'm really getting into this VBScript :mrgreen:
Last edited by guinness on Sat Feb 18, 2012 2:17 pm, edited 1 time in total.

User avatar
I am Baas
Posts: 4150
Joined: Thu Aug 07, 2008 4:51 am

Re: VBScript

#5 Post by I am Baas »

guinness wrote:Please note I've never coded in VBScript so there may be some mistakes when it comes to 'coding practice', though all examples I provide work.
Is Scripting = Coding?

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#6 Post by guinness »

I am Baas wrote:
guinness wrote:Please note I've never coded in VBScript so there may be some mistakes when it comes to 'coding practice', though all examples I provide work.
Is Scripting = Coding?
Well, no. But whatever the correct definition is, the fact remains I've created 4 VBS scripts today without prior knowledge. I did this to prove a point that with persistence you can achieve results. :)

User avatar
I am Baas
Posts: 4150
Joined: Thu Aug 07, 2008 4:51 am

Re: VBScript

#7 Post by I am Baas »

Well, I just went ahead and read a few definitions online... All I could conclude is that if you can open it in notepad and it's readable, then it's a script. If you see weird characters, it's a code. Sums it up for me :lol:

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#8 Post by guinness »

OK, thanks.

Edit_1: I'm interested does I am Baas or anyone else here know VBScript (well I know at least one who kindly sent me a PM) or PowerShell?
Edit_2: I removed some wording.
Last edited by guinness on Tue Feb 14, 2012 3:51 pm, edited 4 times in total.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#9 Post by guinness »

I will be moving these scripts to a different location in the next couple of days.

ZioZione
Posts: 40
Joined: Tue Aug 23, 2011 1:12 pm
Location: Italy

Re: VBScript

#10 Post by ZioZione »

Hi guiness,
nice to read that you are interested in VbScript. Your coding level is already very good! 8) 8)
If you are interested, you can find below a more compact version of "Notepad2 takeover" script:

Code: Select all

Option Explicit
Dim oFileSystem, oShell, sFilePath, sRegRead, sString, sWow6432Node

'~ By guinness (c) 2012.
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set oShell = Wscript.CreateObject("WScript.Shell")
sWow6432Node = ""
   
If Is64Bit() Then
   sWow6432Node = "Wow6432Node\"
End If
sRegRead = "HKEY_LOCAL_MACHINE\SOFTWARE\" & sWow6432Node & "Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe\Debugger"

sRegRead = ReadRegKey(sRegRead)

If sRegRead = "[None]" Then
   sFilePath = oFileSystem.GetParentFolderName(WScript.ScriptFullName) & "\Notepad2.exe"

   If oFileSystem.FileExists(sFilePath) = False Then
      MsgBox "It appears Notepad2 is missing.", vbOKOnly + vbCritical, "Notepad2 Takeover - Error"
      WScript.Quit 1
   End If
   
   oShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\" & sWow6432Node & "Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe\Debugger", """" & sFilePath & """ /z", "REG_SZ"
   sString = "Add Notepad2 to the system."
Else
   sRegRead = Replace(sRegRead, """", "")
   sFilePath = Trim(Replace(sRegRead, "/z", ""))
   oShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\" & sWow6432Node & "Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe\"
   sString = "Removed Notepad2 from the system."
End If
MsgBox sString, vbOKOnly + vbCritical, "Notepad2 Takeover - Complete"

Function Is64Bit()
   sRegRead = oShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
   Is64Bit = InStr(sRegRead, "64") > 0
End Function

'~ Taken From: http://www.visualbasicscript.com/Verify-registry-key-exists-m1432.aspx
Private Function ReadRegKey(ByVal pRegKey)
   On Error Resume Next
   Dim sReturn

   sReturn = oShell.RegRead(pRegKey)

   If Err.Number <> 0 Then
      sReturn = "[None]"
   End If
   ReadRegKey = sReturn
End Function
This code has been written in two minutes and I don't tried it myself, so forgive all errors... :mrgreen:
Best Regards
ZioZione

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#11 Post by guinness »

ZioZione,

I can see now where it could've been improved, thanks for the optimisation tips. That's something that will come overtime :D

User avatar
SYSTEM
Posts: 2043
Joined: Sat Jul 31, 2010 1:19 am
Location: Helsinki, Finland

Re: VBScript

#12 Post by SYSTEM »

guinness wrote:This VBScript function will return True/False if the OS Architecture is x64.

Code: Select all

MsgBox Is64Bit()

'~ By guinness (c) 2012.
Function Is64Bit()
	Dim oShell, sIs64Bit
	Set oShell = Wscript.CreateObject("Wscript.Shell")
	sRegRead = oShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
	Is64Bit = InStr(sRegRead, "64") > 0
End Function
Eh...

The registry key HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment contains environment variables.

Here you are querying the value of %PROCESSOR_ARCHITECTURE% via the registry.
My YouTube channel | Release date of my 13th playlist: August 24, 2020

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#13 Post by guinness »

Correct, but the registry value gave me better results when I tried it.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#14 Post by guinness »

OK, so I was reading this section wrong >> http://ss64.com/vb/envexpand.html, the reason it didn't work before is PROCESSOR_ARCHITECTURE is not a PROCESS environment variable but instead SYSTEM. So here is the new code. Like I said this is still fresh to me so I'm bound to make obvious mistakes. Thanks SYSTEM.

Code: Select all

MsgBox Is64Bit()

Function Is64Bit()
   Dim oShell
   Set oShell = Wscript.CreateObject("Wscript.Shell")
   Is64Bit = InStr(oShell.Environment("SYSTEM").Item("PROCESSOR_ARCHITECTURE"), "64") > 0
End Function
Note: I amended the code above as I was declaring a variable not being used.

User avatar
guinness
Posts: 4118
Joined: Mon Aug 27, 2007 2:00 am
Contact:

Re: VBScript

#15 Post by guinness »

I was looking at the CCleaner documentation and came across a tip in the documentation for adding custom VBScripts that CCleaner executes when cleaning.

Though with the number of additions in winapp2 as well as the power of CCleaner, I can't think of anything else that would really need to be added.

Source: http://www.piriform.com/docs/ccleaner/a ... ng-process

Post Reply