Thursday, December 22, 2016

Reg Del( search for specific string in key)

Const HKCU = &H80000001

sStartKeyPath =  "Software\Microsoft\Office\15.0\Lync\"
sKey = "xyz.com.au"

Set oReg = GetObject _
     ("WinMgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv")

SearchAndDeleteRegistryKey HKCU, sStartKeyPath, sKey


Sub SearchAndDeleteRegistryKey(ByVal sHive, ByVal sStartKeyPath, ByVal sKey)
  Dim aSubKeys, sSubKey, iRC
  On Error Resume Next
  iRC = oReg.EnumKey(sHive, sStartKeyPath, aSubKeys)
  If iRC = 0 And IsArray(aSubKeys) Then
    For Each sSubKey In aSubKeys
      If Err.Number <> 0 Then
        Err.Clear
        Exit Sub
      End If
      SearchAndDeleteRegistryKey sHive, sStartKeyPath & "\" & sSubKey, sKey
    Next
  End If

   aTmpPath = Split(sStartKeyPath, "\")
   If InStr(1, aTmpPath(UBound(aTmpPath)), sKey, vbTextCompare) > 0 Then

    DeleteRegistryKey sHive, sStartKeyPath
  End If
End Sub


Sub DeleteRegistryKey(ByVal sHive, ByVal sKey)
  Dim aSubKeys, sSubKey, iRC
  On Error Resume Next
  iRC = oReg.EnumKey(sHive, sKey, aSubKeys)
  If iRC = 0 And IsArray(aSubKeys) Then
    For Each sSubKey In aSubKeys
      If Err.Number <> 0 Then
        Err.Clear
        Exit Sub
      End If
      DeleteRegistryKey sHive, sKey & "\" & sSubKey
    Next
  End If
  oReg.DeleteKey sHive, sKey
End Sub


Wednesday, November 23, 2016

Java path

@echo off
cls
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
set VALUE_NAME=CurrentVersion
::
:: get the current version
::
FOR /F "usebackq skip=2 tokens=3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set ValueValue=%%A
)
if defined ValueValue (
    @echo the current Java runtime is  %ValueValue%
) else (
    @echo %KEY_NAME%\%VALUE_NAME% not found.
    goto end
)
set JAVA_CURRENT="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%ValueValue%"
set JAVA_HOME=JavaHome

::
:: get the javahome
::
FOR /F "usebackq skip=2 tokens=3*" %%A IN (`REG QUERY %JAVA_CURRENT% /v %JAVA_HOME% 2^>nul`) DO (
    set JAVA_PATH=%%A %%B
)
echo the path of the current Java JVM according to the registry is
echo %JAVA_PATH%
echo.
echo now if we try it :
"%JAVA_PATH%\bin\java.exe" -version
set JAVA_HOME="%JAVA_PATH%\bin\java.exe"

:end

Shortpath Folder

Set fso = CreateObject("Scripting.FileSystemObject")
strProgramFiles = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")

Set f = fso.GetFolder(strProgramFiles)
strShortProgramFiles = f.ShortPath

session.property("ZI_SHORT_PF") = strShortProgramFiles

Process check Notification (Only twice)

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set WshShell = CreateObject("WScript.Shell")

strAppName = "CatiaV5R19"
intSecondsToWait = 10

strMessageTextENG =(vbCr & "You have been scheduled to Upgrade CatiaV5R19."&vbCrlf& "Please exit out of the following program before Upgrading:"_
&vbCrlf&"• CATIA V5")

strMessageText = strMessageTextENG & VbCR & strMessageTextDEU & VbCR & strMessageTextFRA & VbCR & strMessageTextITA & VbCR & strMessageTextESP

strProcessName = "cNext.exe"

CheckForRunningApp strMessageText, intSecondsToWait, strAppName, strProcessName

Function CheckForRunningApp (strMessageText, intSecondsToWait, strAppName, strProcessName)
    Do
        found = false
        Set colProcessList = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = '" & strProcessName & "'")
        For Each objProcess in colProcessList
            WshShell.Popup strMessageText, intSecondsToWait, strAppName, vbInformation + vbSystemmodal
            found = true
        Next
    Loop Until found = false
End Function



Dim objWMIService, objProcess, colProcess, strComputer, processName, instances,found,strMessageText,strMessageTextENG,WshShell,strAppName,intSecondsToWait
Set WshShell = CreateObject("WScript.Shell")

instances = 0
processName = "MsHta.exe"
strAppName = "CatiaV5R19"
intSecondsToWait = 10

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
If objProcess.Name = processName Then instances = instances + 1
Next

Do

found=false
Set colProcess = objWMIService.ExecQuery ("SELECT * FROM Win32_Process WHERE Name = '" & processName & "'")
strMessageTextENG =(vbCr & "You have been scheduled to Upgrade CatiaV5R19."&vbCrlf& "Please exit out of the following program before upgrading:"_
&vbCrlf&"• Catiav5 / 3d Com Launcher")

strMessageText = strMessageTextENG
For Each objProcess in colProcess

              
WshShell.Popup strMessageText, intSecondsToWait, strAppName, vbInformation + vbSystemmodal
if colProcess.Count = instances then
found=true
Else
WScript.Quit

End If
 Next
Loop until Found=false

User reg Del ( All available users Machine)

On Error Resume Next

  
Dim WshShell, RegRoot, objFSO
Set WshShell = CreateObject("WScript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const HKEY_CURRENT_USER     = &H80000001
Const HKEY_LOCAL_MACHINE    = &H80000002
Const HKEY_USERS            = &H80000003
Const HKEY_CURRENT_CONFIG   = &H80000005
'
Const DAT_NTUSER             = &H70000000
Const DAT_USRCLASS             = &H70000001
 
    
'==============================================
' SCRIPT BEGINS HERE
'==============================================

RegRoot = "HKLM\TEMPHIVE" 

'== Loads each user's "HKCU" registry hive
Call Load_Registry_For_Each_User(DAT_NTUSER)     

'== Loads each user's "HKCR" registry hive
Call Load_Registry_For_Each_User(DAT_USRCLASS) 

WScript.Quit(0)
'                                                                   |
'                                                                   |
'====================================================================
 
Sub KeysToModify(sRegistryRootToUse, DAT_FILE)
    '==============================================
    ' Change variables here, or add additional keys
    '==============================================
    '
    On Error Resume Next
   

    If DAT_FILE = DAT_NTUSER Then 'This is for updating HKCU keys
        Dim strRegPathParent01
        Dim strRegPathParent02
        Dim strRegPathParent03
       

strRegPathParent01 = "Software\Microsoft\Active Setup\Installed Components\ETS-ENG-OneDrive-17.3.6390.0509-GBL-R1\"
strRegPathParent02 = "Software\Wow6432Node\Microsoft\Active Setup\Installed Components\ETS-ENG-OneDrive-17.3.6390.0509-GBL-R1\"
strRegPathParent03 = "Software\Microsoft\Active Setup\Installed Components\{8A69D345-D564-463c-AFF1-A69D9E530F96}\"
   

        WshShell.regdelete sRegistryRootToUse & "\" & strRegPathParent01  
        WshShell.regdelete sRegistryRootToUse & "\" & strRegPathParent02
        WshShell.regdelete sRegistryRootToUse & "\" & strRegPathParent03 

    End If
End Sub



Function GetDefaultUserPath
    On Error Resume Next
   
    Dim objRegistry
    Dim strKeyPath
    Dim strDefaultUser
    Dim strDefaultPath
    Dim strResult
 
    Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
 
    objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,"DefaultUserProfile",strDefaultUser
    objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,"ProfilesDirectory",strDefaultPath
         
    If Len(strDefaultUser) < 1 or IsEmpty(strDefaultUser) or IsNull(strDefaultUser) Then
        'must be on Vista or newer
        objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,"Default",strDefaultPath
        strResult =  strDefaultPath
    Else
        'must be on XP
        strResult =  strDefaultPath & "\" & strDefaultUser
    End If
     
    GetDefaultUserPath = strResult
End Function
 
Function RetrieveUsernameFromPath(sTheProfilePath)
    On Error Resume Next
   
    Dim lstPath
    Dim sTmp
    Dim sUsername
    
    lstPath = Split(sTheProfilePath,"\")
    For each sTmp in lstPath
        sUsername = sTmp
        'last split is our username
    Next
    
    RetrieveUsernameFromPath = sUsername
End Function
 
Sub LoadProfileHive(sProfileDatFilePath, sCurrentUser, DAT_FILE)
    On Error Resume Next
   
    Dim intResultLoad, intResultUnload, sUserSID

    'Load user's HKCU into temp area under HKLM
    intResultLoad = WshShell.Run("reg.exe load " & RegRoot & " " & chr(34) & sProfileDatFilePath & chr(34), 0, True)
    If intResultLoad <> 0 Then
        ' This profile appears to already be loaded...lets update it under the HKEY_USERS hive
        Dim objRegistry2, objSubKey2
        Dim strKeyPath2, strValueName2, strValue2
        Dim strSubPath2, arrSubKeys2
 
        Set objRegistry2 = GetObject("winmgmts:\\.\root\default:StdRegProv")
        strKeyPath2 = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
        objRegistry2.EnumKey HKEY_LOCAL_MACHINE, strKeyPath2, arrSubkeys2
        sUserSID = ""
 
        For Each objSubkey2 In arrSubkeys2
            strValueName2 = "ProfileImagePath"
            strSubPath2 = strKeyPath2 & "\" & objSubkey2
            objRegistry2.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath2,strValueName2,strValue2
            If Right(UCase(strValue2),Len(sCurrentUser)+1) = "\" & UCase(sCurrentUser) Then
                'this is the one we want
                sUserSID = objSubkey2
            End If
        Next
 
        If Len(sUserSID) > 1 Then
            'WScript.Echo "  Updating another logged-on user: " & sCurrentUser & vbCrLf

            If DAT_FILE = DAT_NTUSER Then
                Call KeysToModify("HKEY_USERS\" & sUserSID, DAT_FILE)
            ElseIf DAT_FILE = DAT_USRCLASS Then
                Call KeysToModify("HKEY_USERS\" & sUserSID & "_Classes", DAT_FILE)
            End If       
        Else
            'WScript.Echo("  *** An error occurred while loading HKCU for this user: " & sCurrentUser)
        End If
    Else
        'WScript.Echo("  HKCU loaded for this user: " & sCurrentUser)
    End If
 
    ''
    If sUserSID = "" then 'check to see if we just updated this user b/c they are already logged on
        Call KeysToModify(RegRoot, DAT_FILE) ' update registry settings for this selected user
    End If
    ''
 
    If sUserSID = "" then 'check to see if we just updated this user b/c they are already logged on
        intResultUnload = WshShell.Run("reg.exe unload " & RegRoot,0, True) 'Unload HKCU from HKLM
        If intResultUnload <> 0 Then
            'WScript.Echo("  *** An error occurred while unloading HKCU for this user: " & sCurrentUser & vbCrLf)
        Else
            'WScript.Echo("  HKCU UN-loaded for this user: " & sCurrentUser & vbCrLf)
        End If
    End If
End Sub
 
Function GetUserRunningScript()
    On Error Resume Next
    Dim sUserRunningScript, sComputerName
    sUserRunningScript = WshShell.ExpandEnvironmentStrings("%USERNAME%") 'Holds name of current logged on user running this script
    sComputerName = UCase(WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%"))
    
    If sUserRunningScript = "%USERNAME%" or sUserRunningScript = sComputerName & "$"  Then
        ' This script might be run by the SYSTEM account or a service account
        Dim sTheProfilePath
        sTheProfilePath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") 'Holds name of current logged on user running this script
   
        sUserRunningScript = RetrieveUsernameFromPath(sTheProfilePath)
    End If

    GetUserRunningScript = sUserRunningScript
End Function

Function RemoveTrailingPathDelimiter(sPath)
    On Error Resume Next

    Dim sUpdatedPath
    sUpdatedPath = sPath

    If Right(sUpdatedPath,1) = "\" Then
        sUpdatedPath = Left(sUpdatedPath,Len(sUpdatedPath)-1)
    End If

    RemoveTrailingPathDelimiter = sUpdatedPath
End Function

Function GetPathToDatFileToUpdate(sProfilePath, DAT_FILE)
    On Error Resume Next

    Dim sDatFile, sPathToDat, sTrimmedProfilePath
    Dim bFoundDatFile
    sPathToDat = "" 'default

    sTrimmedProfilePath = RemoveTrailingPathDelimiter(sProfilePath)

    If DAT_FILE = DAT_NTUSER Then
        sDatFile = "NTUSER.DAT"

        If objFSO.FileExists(sTrimmedProfilePath & "\" & sDatFile) or objFSO.FileExists(chr(34) & sTrimmedProfilePath & "\" & sDatFile & chr(34)) Then
            sPathToDat = sTrimmedProfilePath & "\" & sDatFile       
        End If
    ElseIf DAT_FILE = DAT_USRCLASS Then
        sDatFile = "USRCLASS.DAT"

        If objFSO.FileExists(sTrimmedProfilePath & "\AppData\Local\Microsoft\Windows\" & sDatFile) OR _
            objFSO.FileExists(chr(34) & sTrimmedProfilePath & "\AppData\Local\Microsoft\Windows\" & sDatFile & chr(34)) Then
            sPathToDat = sTrimmedProfilePath & "\AppData\Local\Microsoft\Windows\" & sDatFile
        ElseIf objFSO.FileExists(sTrimmedProfilePath & "\Local Settings\Application Data\Microsoft\Windows\" & sDatFile) OR _
            objFSO.FileExists(chr(34) & sTrimmedProfilePath & "\Local Settings\Application Data\Microsoft\Windows\" & sDatFile & chr(34)) Then
            sPathToDat = sTrimmedProfilePath & "\Local Settings\Application Data\Microsoft\Windows\" & sDatFile
        End If
    End If

    GetPathToDatFileToUpdate = sPathToDat
End Function

Sub Load_Registry_For_Each_User(DAT_FILE)
    On Error Resume Next
        
    Dim sUserRunningScript
    Dim objRegistry, objSubkey
    Dim strKeyPath, strValueName, strValue, strSubPath, arrSubKeys
    Dim sCurrentUser, sProfilePath, sNewUserProfile
    Dim sPathToDatFile

    sUserRunningScript = GetUserRunningScript       
    'WScript.Echo "Updating the logged-on user: " & sUserRunningScript & vbCrLf
    ''

    If DAT_FILE = DAT_NTUSER Then
        Call KeysToModify("HKCU", DAT_FILE) 'Update registry settings for the user running the script
    ElseIf DAT_FILE = DAT_USRCLASS Then
        Call KeysToModify("HKCR", DAT_FILE) 'Update registry settings for the user running the script
    End If
    ''     
    sNewUserProfile = GetDefaultUserPath

    sPathToDatFile = GetPathToDatFileToUpdate(sNewUserProfile, DAT_FILE)

    If Len(sPathToDatFile) > 0 Then
        'WScript.Echo "Updating the DEFAULT user profile which affects newly created profiles." & vbCrLf
        Call LoadProfileHive(sPathToDatFile, "Default User Profile", DAT_FILE)
   
    End If
        
    Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
    objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
    
    For Each objSubkey In arrSubkeys
        strValueName = "ProfileImagePath"
        strSubPath = strKeyPath & "\" & objSubkey
        objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
        sProfilePath = strValue
        sCurrentUser = RetrieveUsernameFromPath(strValue)
    
        If ((UCase(sCurrentUser) <> "ALL USERS") and _
            (UCase(sCurrentUser) <> UCase(sUserRunningScript)) and _
            (UCase(sCurrentUser) <> "LOCALSERVICE") and _
            (UCase(sCurrentUser) <> "SYSTEMPROFILE") and _
            (UCase(sCurrentUser) <> "NETWORKSERVICE")) then
            
            sPathToDatFile = GetPathToDatFileToUpdate(sProfilePath, DAT_FILE)

            If Len(sPathToDatFile) > 0 Then
                'WScript.Echo "Preparing to update the user: " & sCurrentUser
                Call LoadProfileHive(sPathToDatFile, sCurrentUser, DAT_FILE)
            End If
        End If
    Next
End Sub

Logged on user

On Error Resume Next

Const ForReading = 1
Dim objWshShell : Set objWshShell = CreateObject("WScript.Shell")
Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
Dim str1, Windir
Set objFSO = CreateObject("Scripting.FileSystemObject")
Windir = WshShell.ExpandEnvironmentStrings("%windir%")
Set objFile = objFSO.OpenTextFile(windir & "\Temp\ansi_Lguser.txt", ForReading)

Do Until objFile.AtEndOfStream
    strNextLine = objFile.ReadLine
    If Len(strNextLine) > 0 Then
        strLine=strNextLine
    End If
Loop

objFile.Close
'Wscript.Echo strLine
str1 = "HKEY_LOCAL_MACHINE\SOFTWARE\CSC\Current_UName\Username"
WshShell.Regwrite str1,strLine,"REG_SZ"   

Empty Folder Del

on error resume next

Dim wshShell, objFSO, ProgramFiles

Set wshShell =CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
ProgramFiles=wshShell.ExpandEnvironmentStrings("%ProgramFiles%")

If FolderEmpty(ProgramFiles & "\MATLAB") Then
objFSO.DeleteFolder ProgramFiles & "\MATLAB"
End If



Function FolderEmpty(strFolder)
Dim objFSO, objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
 
    If objFolder.Files.Count = 0 And objFolder.SubFolders.Count = 0 Then
        FolderEmpty=True
    Else
        FolderEmpty=False

    End If
End If

Set objFSO = Nothing
End Function


Set wshShell = Nothing
Set objFSO = Nothing
WScript.Quit