Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: MkObject() - Create COM Object with Error Checking[Next]: SortArray() - Sort single dimension array
\->VBSCRIPT Files->RestartCscriptIn32BitModeIfRequired() - if in 64 bit mode

RestartCscriptIn32BitModeIfRequired() [if in 64 bit mode].vbs

See the "64 & 32 Bit Windows Operating System" section for an introduction to some of the differences and issues.

[anchor]

The Code for: "RestartCscriptIn32BitModeIfRequired() [if in 64 bit mode].vbs"

This is the example, a shortcut was installed to this code :

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[vbs].GetEnv() [Get Environment Variables].vbs.txt.pvcs   1.0   11 Jul 2014 19:31:06   USER "Dennis"  $
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

dim oShell : set oShell = CreateObject("WScript.Shell")
    TEST_SHOW_MODE("Script Starting")
         RestartCscriptIn32BitModeIfRequired()
    TEST_SHOW_MODE("Script Initialized/Finishing")

set oShell = Nothing
wscript.quit 777

'============================================================================
sub RestartCscriptIn32BitModeIfRequired()
' Based on FIXED code: http://stackoverflow.com/questions/2806584/how-do-i-run-a-vbscript-in-32-bit-mode-on-a-64-bit-machine
'============================================================================
    Dim r32wShell, r32env1, r32env2, r32iCount
    Dim r32fso
    SET r32fso    = CreateObject("Scripting.FileSystemObject")
    Set r32wShell = CreateObject("WScript.Shell")
    r32env1       = r32wShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
    If  r32env1 <> "x86" Then
        '--- not running in x86 mode (prevent infinite loops) ---------------
        For r32iCount = 0 To WScript.Arguments.Count - 1
            r32env2 = r32env2 & WScript.Arguments(r32iCount) & VbCrLf
        Next
        If  InStr(r32env2, "restart32") <> 0 Then
            '--- Already tried to restart -----------------------------------
            MsgBox "Cannot find 32bit version of cscript.exe or unknown OS type " & r32env1
        else
            '--- Ok this is the first time so BUILD THE COMMAND WITHOUT SCRIPT PARAMETERS ---
            dim strCMD, iCount
            strCMD = r32wShell.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\SysWOW64\cscript.exe"
            if NOT r32fso.FileExists(strCMD) then
                '--- This may not work if we can't find the SysWOW64 Version ---
                strCMD = "cscript.exe"
            end if
            strCMD = strCMD & " //NoLogo """ & Wscript.ScriptFullName & """"

            '--- Rebuild the arguments on the command ----------------------------
            If  Wscript.Arguments.Count > 0 Then
                For iCount = 0 To WScript.Arguments.Count - 1
                     if Instr(Wscript.Arguments(iCount), " ") = 0 Then ' add unspaced args
                        strCMD = strCMD & " " & Wscript.Arguments(iCount) & " "
                     else
                          If Instr("/-\", Left(Wscript.Arguments(iCount), 1)) > 0 Then ' quote spaced args
                             If InStr(WScript.Arguments(iCount),"=") > 0 Then
                                strCMD = strCMD & " " & Left(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), "=") ) & """" & Mid(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), "=") + 1) & """ "
                             ElseIf Instr(WScript.Arguments(iCount),":") > 0 Then
                                strCMD = strCMD & " " & Left(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), ":") ) & """" & Mid(Wscript.Arguments(iCount), Instr(Wscript.Arguments(iCount), ":") + 1) & """ "
                             Else
                                strCMD = strCMD & " """ & Wscript.Arguments(iCount) & """ "
                             End If
                          Else
                           strCMD = strCMD & " """ & Wscript.Arguments(iCount) & """ "
                          End If
                     End If
                Next
            End If

            '--- Start the command and flag that we restarted ---------------
            wscript.echo "Restarting, you will not see stdout after this)..."
            dim ScriptRc : ScriptRc = 9876
            ScriptRc = r32wShell.Run(strCMD & " restart32", 0, true)
        end if
        Set r32wShell = Nothing
        WScript.Quit( ScriptRc )
    End If
    Set r32wShell = Nothing
    Set r32fso = Nothing
end sub







'============================================================================
sub TEST_SHOW_MODE(Doing)
'============================================================================
    say       "Mode: " & GetEnv("PROCESSOR_ARCHITECTURE") & "  (" & Doing & ")"
    MsgBox    "Mode: " & GetEnv("PROCESSOR_ARCHITECTURE"),,         Doing
end sub

'============================================================================
function GetEnv(EnvName)
'============================================================================
   dim Try
   Try    = "%" & EnvName & "%"
   GetEnv = oShell.ExpandEnvironmentStrings(Try)
   if  GetEnv = Try then
       GetEnv = ""
   end if
end function


'============================================================================
sub Say(What)
'============================================================================
    wscript.echo What
end sub


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: MkObject() - Create COM Object with Error Checking[Next]: SortArray() - Sort single dimension array


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.