Archive for the 'Scripting [VBscript,WSH]' Category

VB Script:Add an entry in Hosts file

The hosts file is one of several system facilities to assist in addressing network nodes in a computer network.(As per the definition in Wikipedia!)
The hosts file contains lines of text consisting of an IP address in the first text field followed by one or more hostnames.

This file will be present in %SystemRoot%\system32\drivers\etc\

 

The below script is an example for adding an entry in to the hosts file.

This will append the existing hosts file. The same script can be used for updating the services file by changing the filename in the script.

No shy to say this is a copied script !!. I have modified some lines as per our requirement.

 

On Error Resume Next

Dim wshshell

Set wshshell = createobject("wscript.shell")

Line1="0.0.0.0  TEST"     ‘Enter the required entry here
Call AddValues(Line1)

Set wshShell = Nothing

 

Sub AddValues(Line)
    Dim varFile         ‘Variable to store the values of the file
    Dim varFileLines    ‘Variable to store the current line read from the file
    Dim WshShell        ‘Object pointing to the WScript
   
    ‘Initializing the WScript object
    Set WshShell = CreateObject("WScript.shell")
    ‘System root
    SysRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%")
    ‘File Path
    sFileName = SysRoot & "\system32\drivers\etc\Hosts"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(sFileName,1)

    Flag=0
   
    ‘looping through the hosts file
    Do Until objFile.AtEndOfStream
        ‘Reading a line from the hosts file
        varFileLines = objFile.ReadLine
        ‘Checking for the search string
        If Trim(varFileLines) <> Line Then
            ‘Building the string without the search string
            If Trim(varFile) = "" Then
                varFile = Trim(varFileLines)
            Else
                varFile = varFile & vbCrLf & Trim(varFileLines)
            End If
    Else
        Flag=1
        End If
    Loop
    ‘varFile = varFile & vbCrLf & Trim(varFileLines)
     ‘Closing the file object
        objFile.Close
    if Flag=0 then
        ‘varFile = varFile & vbCrLf & Trim(varFileLines) & vbCrLf & Line
        varFile = varFile & vbCrLf & Line
        ‘Creating the file with the search string
        Set objFile = objFSO.CreateTextFile(sFileName, True, False)
        ‘Writing the lines into the file
        objFile.WriteLine varFile
    ‘Closing the file object
    objFile.Close
    end if
    Set objFile = Nothing
End Sub

%~z1

%~z1 – expands %1 to size of file

To get the size of a file in bytes using batch script is very easy.

Open note pad and type in the below commands.

echo %~z1

pause

save the file as test.bat

If you want to get the size of a file ,say “Testmysize.mdb” ,Open cmd.exe and run the below command.

test.bat Testmysize.mdb

It will display the size of Test my size.mdb file in bytes.

VB Script :Write a binary value in registry

Here I share with a you a sample script to write a binary value in registry.Its not  easy as you write REG_DWORD ,etc .

const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\test"
strComputer = "."
iValues = Array(&H01) ‘<-Give the value here .

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
BinaryValueName = "FailureActions"
oReg.SetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath, BinaryValueName,iValues

 

Note: For testing this script make sure ‘HKLM\Software\test’ key present already.

%~dp0

To get the path of a batch file from which directory it is executing ,you can use %~dp0.

image

Get Current Directory (VBscript)

The below script shows two methods to find the current directory.

‘===Get Current Directory
‘Method 1:
set wshell = createobject(“wscript.shell”)
wscript.echo wshell.currentdirectory

‘Method 2:

Set wshell= CreateObject(“WScript.Shell”)
Set objfso = CreateObject(“Scripting.FileSystemObject”)

wshell.CurrentDirectory = objfso.GetParentFolderName(Wscript.ScriptFullName)

wscript.echo wshell.CurrentDirectory

Batch script : Special Parameters

 

Parameter Description
%1 The normal parameter.
%~f1 Expands %1 to a fully qualified pathname. If you passed only a filename from the current directory, this parameter would also expand to the drive or directory.
%~d1 Extracts the drive letter from %1.
%~p1 Extracts the path from %1.
%~n1 Extracts the filename from %1, without the extension.
%~x1 Extracts the file extension from %1.
%~s1 Changes the n and x options’ meanings to reference the short name. You would therefore use %~sn1 for the short filename and %~sx1 for the short extension.

How to execute a batch file with a parameter from VBS

The below script is an example to show how to pass a parameter to a batch file from VBS.

StrComputer=”MYCOMPUTER”
Set oWsh = WScript.CreateObject(“WScript.Shell”)
sfcmd = chr(34) & oWsh.CurrentDirectory &”\install.bat”&chr(34)&” ” &StrComputer
oWsh.Run sfcmd,0,True       ‘ Hidden,  wait

Note: In the batchfile set the StrComputer=%1 to get the StrComputer value mentioned in VBS file.