Tuesday, February 7, 2017

VBScript Show Details Of a File, Copy, Delete And Move

set FSO = CreateObject("Scripting.FileSystemObject")

'Display detailed information about this file
set FileObject = FSO.GetFile("C:\tmp\heidisql.exe")

Wscript.Echo "Path = " & FileObject.Path
Wscript.Echo "Name = " & FileObject.Name
Wscript.Echo "Short Name = " & FileObject.ShortName
Wscript.Echo "Short Path = " & FileObject.ShortPath
Wscript.Echo "Drive = " & FileObject.Drive
Wscript.Echo "Size = " & FileObject.Size
Wscript.Echo "Attributes = " & FileObject.Attributes
Wscript.Echo "Parent Folder = " & FileObject.ParentFolder
Wscript.Echo "Date Created = " & FileObject.DateCreated
Wscript.Echo "Last Modified = " & FileObject.DateLastModified
Wscript.Echo "Last Accessed = " & FileObject.DateLastAccessed
'FileObject.Copy "heidisql_.exe", True 'True=Overwrite
'FileObject.Delete True 'True=Delete the read-only file
FileObject.Move "heidisql_moved.exe"

VBScript Open URL in Default Browser

Option Explicit

Dim WscriptSchell
Set WscriptSchell = CreateObject("WScript.Shell")

WscriptSchell.Run "https://google.com", 9

VBScript List And Details Of Local Drives

set FSO = CreateObject("Scripting.FileSystemObject")

'Display all drives and their properties
set ds = FSO.Drives
WScript.Echo "List of drives:"
for each d in ds
    WScript.Echo ""
    WScript.Echo "Drive Letter = " & d.DriveLetter
    WScript.Echo "Type = " & d.DriveType
    WScript.Echo "Ready = " & d.IsReady
    WScript.Echo "Path = " & d.Path
    WScript.Echo "Share Name = " & d.ShareName
    if d.IsReady then
        Details = "File System = " & d.FileSystem _
            & ", Serial = " & d.SerialNumber _
            & ", Volume = " & d.VolumeName _
            & ", Size = " & d.TotalSize _
            & ", Free = " & d.FreeSpace
        WScript.Echo Details
    end if
next

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