Tuesday, February 7, 2017

VBScript Execute Ping

Dim oShell, oExec, sLine
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("ping google.com")

'Reading the output of the shell command thread
Do While Not oExec.StdOut.AtEndOfStream
    sLine = oExec.StdOut.ReadLine
    WScript.StdOut.WriteLine "Output: " & sLine
    WScript.Sleep 10
Loop

'Waiting for the shell command thread to end
'In case the output ends before the command
Do While oExec.Status = 0
    WScript.Sleep 100
Loop

VBScript Append text to text file if it already exists

Option Explicit

Const ForReading = 1, ForAppending = 8

Dim InputFileName, OutputFileName, FileSystemObject, InputFile, OutputFile

Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
InputFileName  = "C:\tmp\input.txt"
OutputFileName = "C:\tmp\output.txt"

PreCheck

Set InputFile = FileSystemObject.OpenTextFile(InputFileName, ForReading)
Set OutputFile = FileSystemObject.OpenTextFile(OutputFileName, ForAppending, True)

OutputFile.WriteLine ""
Do While Not InputFile.AtEndOfStream 
    OutputFile.WriteLine InputFile.ReadLine
Loop

InputFile.Close
OutputFile.Close

Sub Echo(Str)
    Wscript.Echo Str
End Sub

Sub PreCheck()
    If Not FileSystemObject.FileExists(OutputFileName) Then
        Echo "Output File Not Found"
        WScript.Quit
    End If

    If Not FileSystemObject.FileExists(InputFileName) Then
        Echo "Input File Not Found"
        WScript.Quit
    End If

    If FileSystemObject.GetFile(InputFileName).Size < 1 Then
        Echo "Input File Has No Content"
        WScript.Quit
    End If
End Sub

Saturday, February 4, 2017

Windows CMD, Clear All Browsers Cache & Everything

::@ECHO OFF
::cscript.exe "Test.vbs"
::pause
::exit;

@echo off

rem IE
taskkill /F /IM iexplore.exe
start "" "C:\Windows\System32\rundll32.exe" InetCpl.cpl,ClearMyTracksByProcess 255

:: Parse the Local AppData sub path
call :Expand xAppData "%%LocalAppData:%UserProfile%=%%"

set "xFirefox=\mozilla\firefox\profiles"
set "xChrome=\google\chrome\user data"
set "xOpera1=\Local\Opera\Opera"
set "xOpera2=\Roaming\Opera\Opera"

:: Start at the User directory
pushd "%UserProfile%\.."

taskkill /F /IM firefox.exe
taskkill /F /IM chrome.exe
taskkill /F /IM opera.exe

:: Loop through the Users
for /D %%D in (*) do if exist "%%~fD%xAppData%" (
    rem Check for Firefox
    if exist "%%~fD%xAppData%%xFirefox%" (
        rd /s /q "%%~fD%xAppData%%xFirefox%"
    )

    rem Check for Chrome
    if exist "%%~fD%xAppData%%xChrome%" (
        rd /s /q "%%~fD%xAppData%%xChrome%"
    )
 
 rem Check for Opera
    if exist "%%~fD%xAppData%%xOpera1%" (
        rd /s /q "%%~fD%xAppData%%xOpera1%"
    )
 if exist "%%~fD%xAppData%%xOpera2%" (
        rd /s /q "%%~fD%xAppData%%xOpera2%"
    )
)
popd
goto End


::::::::::::::::::::::::::::::
:Expand <Variable> <Value>
if not "%~1"=="" set "%~1=%~2"
goto :eof


:End
endlocal

VBScript Clear Google Chrome Cache, History & Everything

Option Explicit
Dim FileSystemObject, RootFolder, UserName, File
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

UserName = CreateObject("WScript.Network").UserName
RootFolder = "C:\Users\" & UserName & "\AppData\Local\Google\Chrome\User Data\"
 
ProcessTerminate "chrome.exe"
DeleteFileFromFolder RootFolder

Sub DeleteFileFromFolder(Path)
    For Each File In FileSystemObject.GetFolder(Path).Files
        DeleteAllow File
    Next
    For Each File In FileSystemObject.GetFolder(Path).SubFolders
        DeleteFileFromFolder File.Path
    Next
End Sub

Sub DeleteAllow(File)
    'Bookmarks remain same
    If NOT(StrComp(Left(File.Name, 9), "Bookmarks", vbTextCompare)) = 0 Then
        Wscript.Echo File.Path + " Deleted"    
        On Error Resume Next
        FileSystemObject.DeleteFile File.Path, True
    End If
End Sub

Sub ProcessTerminate(Process)
    Const StrComputer = "." 
    Dim WshShell, ObjWMIService, ProcessList, ObjProcess
    Set WshShell = CreateObject("WScript.Shell")
    Set ObjWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & StrComputer & "\root\cimv2")

    Set ProcessList = ObjWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & Process & "'")
    For Each ObjProcess in ProcessList 
        On Error Resume Next
        ObjProcess.Terminate
        Wscript.Echo "Process Terminated, ProcessId=" & ObjProcess.ProcessId & " (" & ObjProcess.Name & ")" 
    Next
End Sub

Friday, February 3, 2017

VBScript MySql connection

You have to install mysql odbc connector first.

Dim Connection, ConnectString

ConnectString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;PORT=3306;Database=database;User=root;Password="

Set Connection = CreateObject("ADODB.Connection")

Connection.Open ConnectString

Sql = "SELECT id,created_at FROM table ORDER BY id DESC LIMIT 0, 10"
Set Result = Connection.Execute(Sql)

Do Until Result.EOF
    Wscript.Echo "Id=" + CStr(Result(0)) + ",Create_Date=" & FormatDateTime(Result(1), 1)
    Result.MoveNext
Loop

Connection.Close

Install and Create MySQL ODBC Connector on Windows 7

MySQL ODBC Connector help you to connect to MySQL via different applications. It supports Windows, Linux, Mac & Unix Platforms. You can more details from here. Please follow the following steps to download and install MySQL ODBC Connector to your machine.


  • Download and install from here or from here
  • Double click on the file "mysql-connector-odbc-3.51.30-winx64.msi" that you just downloaded to run the setup process.
  • Click Next and accept the license agreement.
  • Select Custom to see what we will have after the installation

VBScript Get a List of the ODBC Drivers

Const HKEY_LOCAL_MACHINE = &H80000002
StrComputer = "."
Set ObjRegistry = GetObject("winmgmts:\\" & StrComputer & "\root\default:StdRegProv")
StrKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
ObjRegistry.EnumValues HKEY_LOCAL_MACHINE, StrKeyPath, arrValueNames, arrValueTypes

For i = 0 to UBound(arrValueNames)
    StrValueName = arrValueNames(i)
    ObjRegistry.GetStringValue HKEY_LOCAL_MACHINE,StrKeyPath,StrValueName,StrValue
    Wscript.Echo StrValueName + " - " + StrValue
Next