Novice scripting - Extract a batch file to ProgramData with VB

Discussion in 'Scripting' started by SwampFox56, Nov 26, 2014.

  1. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    Hello all!

    Like most, I experience the Windows Explorer bug, which locks up the start button, and charms bar (why MS hasn't fixed it? Who knows...) Anyways, I'm well aware that Winaero (Sergey Tkachenko), has already made a tool that restarts Windows Explorer for this exact same purpose.

    However, his tool is slow. (In my opinion) it performs unnecessary operations that takes Windows Explorer a good 10-15 seconds to restarts. My simple VBScript bypasses all this crap, and just terminates the process, then restarts it. However, the VBS relies on a batch file to kill the process.

    Is there anyway in VB to create a folder in PD (ProgramData), and then extract a file to said folder? I'd like to keep this one, simple, executable and not make it an installer if possible. Any help is appriciated :)

    Thanks a lot guys!
    SwampFox
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    #2 Compo, Nov 26, 2014
    Last edited by a moderator: Apr 20, 2017
    There should be no need to write a batch file, create a directory, extract the batch file to that directory and then run it from a visual basic script just to restart Windows Explorer.

    BTW, I would use PowerShell for this task:
    RestartExplorer.ps1
    Code:
    ForEach ($oItem In PS) { If ($oItem.ProcessName -Eq 'explorer') {
    SpPs $oItem.ID -Force } }
    $Rkey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
    If ((GP $RKey -Name AutoRestartShell | Select -ExP AutoRestartShell) -NE 1) {
    SaPs explorer.exe }
     
  3. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    Thanks for the info.

    I've been trying to switch to powershell scripting - but having used Windows since 95 came out, I know all the DOS commands by heart. I know powershell is more... well... powerful, but I don't want to relearn all the commands :/ I should...

    Anyways, I'm using VB to execute the batch script 'silently'. If you perform a 'taskkill' on any process with a batch script - the window remains open. The point of using VB was to execute the script, without leaving the CMD window open. Plus - In the end, I want it to be one executable file. Preferably in the sense that the exe creates a temp folder that extracts the batch script to it, then executes the batch file.
     
  4. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,305
    18,123
    340
    #4 s1ave77, Nov 27, 2014
    Last edited by a moderator: Apr 20, 2017
    You could write the powershell script to temp PS1, execute the PS! and delete afterwards :g:.


    Code:
    set "restart=%temp%\RestartExplorer.ps1"
    echo ForEach ^($oItem In PS^) { If ^($oItem.ProcessName -Eq 'explorer'^) {>%restart%
    echo        SpPs $oItem.ID -Force } }>>%restart%
    echo $Rkey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'>>%restart%
    echo If ^(^(GP $RKey -Name AutoRestartShell ^| Select -ExP AutoRestartShell^) -NE 1^) {>>%restart%
    echo    SaPs explorer.exe }>>%restart%
    powershell -executionpolicy bypass -file %restart%
    if exist "%restart%" del /s /q "%restart%" >nul
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    #5 SwampFox56, Nov 27, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    That's a pretty good idea actually. Only issue is, I plan to release this tool to the community... name it something like (quick shell restart). No bulls**t. No saving program states. Just kill the damn process and re-execute. Do you know of a way to wrap powershell scripts into executable files? Like I said above - to make this program simple and easy for anyone to use; I'd really like it to be just a simple .exe
     
  6. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    As I've already stated, you shouldn't need to leave the .vbs to run a .cmd.

    Have you thought about possibly providing us with your current VBScript? (Otherwise it would appear that your intention is to release someone else's work to the community).
     
  7. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    #7 SwampFox56, Nov 28, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    This did make me laugh a bit lol. If you want it, you can have it. As I said above, the point of using VB was to execute the batch file silently. Otherwise, no matter what (even if you put @echo off, or CMD /C at the beginning, or exit at the end, the Command Prompt stays open.).

    Restart Explorer.vbs
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run chr(34) & "C:\ProgramData\ShellRestart\Restart Explorer.bat" & Chr(34), 0
    Set WshShell = Nothing 
    
    Restart Explorer.bat
    Code:
    @echo off
    CMD /C
    taskkill /im explorer.exe /f
    explorer.exe
    EXIT
    
    In CMD I've tried many ways to get the command prompt to close by itself (goto, exit, cmd /c,) but the window refuses to close unless you close it yourself. For a public tool, I don't think that would be acceptable.

    Oh, and believe me. If I was copying someone elses code - it would be a little more advanced than mine. That's why I titled this thread 'NOVICE' scripting.
     
  8. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    #8 Compo, Nov 29, 2014
    Last edited by a moderator: Apr 20, 2017
    What would you expect this to do?

    script.vbs
    Code:
    Set objShell = CreateObject("WScript.Shell")
    comspec = objShell.ExpandEnvironmentStrings("%comspec%")
    objShell.Run(comspec & " /c taskkill /im explorer.exe /f"), 0, False
     
  9. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    #9 SwampFox56, Nov 30, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Already tried that. Unfortunately, that VBS won't restart explorer. It only terminates it.
     
  10. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    #10 Compo, Nov 30, 2014
    Last edited by a moderator: Apr 20, 2017
    Of course it doesn't; I didn't ask it to!
    I wasn't giving you a complete and direct solution, I was showing you that there is no need for an 'nt command script' and 'vb script' and it isn't necessary to make directories and create temporary files. I also showed that the required process can be achieved without the console window appearing in the GUI.

    [EDIT]
    Additionally, if you were to carry on with your intention of running the 'nt command script', then the fact that you "know all of the DOS commands by heart" should mean that you've tried this too!
    xplora.cmd
    Code:
    @taskkill /im explorer.exe /f
    @start explorer
    You could go one step further with that too
    Code:
    @ECHO OFF & SETLOCAL
    SET "_MP=explorer.exe"
    :LOOP
    TASKLIST /FI "IMAGENAME eq %_MP%" | FIND /I "%_MP%" >NUL && (
    TASKKILL /IM %_MP% /F >NUL & GOTO :LOOP) || (START %_MP%)
    This should open explorer if it isn't running, or otherwise restart it.
    [/EDIT]
     
  11. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    #11 SwampFox56, Nov 30, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I have to admit, I didn't try the below. If it works, I'll wrap it in an EXE and give you credit. Although I have my doubts. But what the heck! And with the VBS you posted above - I tried that before and the extra specified command (to reopen explorer.exe) but it always caused it to give me an error. Maybe I just did it wrong... who knows. I don't know VBS that well. I'll give it a go and see what happens.
     
  12. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,305
    18,123
    340
    #12 s1ave77, Nov 30, 2014
    Last edited by a moderator: Apr 20, 2017
    Found the thread where they struggled with 'graceful' Explorer Restart :D : http://forums.mydigitallife.net/thr...eylogger/page4?p=960981&viewfull=1#post960981

    some posts later there is a binary solution and soure from hb860

    EDIT:
    Noticed you already know this :doh:.

    There is a nice soultion by Mr Jinje for Context Menu Entry to Restart Explorer.

    Example for cascaded context Menu 'TOOLS' with 'Restart Explorer by Mr Jinje':


    Code:
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "MUIVerb" /t REG_SZ /d "TOOLS" /f
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "SubCommands" /t REG_SZ /d "Restart_Explorer" /f
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "Icon" /t REG_SZ /d "imageres.dll,220" /f
    reg add "HKCR\DesktopBackground\Shell\TOOLS" /v "Position" /t REG_SZ /d "top" /f
    
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Restart_Explorer" /ve /t REG_SZ /d "Restart Explorer" /f
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Restart_Explorer" /v "icon" /t REG_SZ /d "imageres.dll,-5339" /f
    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\Restart_Explorer\Command" /ve /t REG_SZ /d "cmd /c start /b /wait powershell.exe -nologo -WindowStyle Hidden -sta -command \"$PAYLOAD = ^
    'H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/In7j5Dd^
    Onq2X07aolumrvGmzuv39T9+tyqrO6/QX4+s39XX6i7+n343Nd9//mWrdbi/XZflL0mnWTufpL07R+ng2235zvcpT/vdpfl4sCwb+e35M36+bYnmRvr5u2nxxGP45PqnKMmdEmvHn+TKvi2m3yav1si0W^
    +fhs2eZ1tXqd15fFNG8O0fEyW+TNKpvmZhi/cULor9aTspim0zJrmtTgzt987/Wb4zfzOs9m38fr33talmeLVVW3Wx+tm7y+tzeeleVHo/R13j7Pmva0rqs6/Sxt63V+5/sWMPXU0o/8HSG0TAmvl22dP^
    iuWs+/S/6urraatgb/81ZwAjVEafPimaMv8Dg/he3Xeruvlo/SLrG7mWXncbH21XGTL7CKfgZrjJ1VVovP3RTbEckJQ0pdV036RNw3B3lK0599dzkbpuli26RfNxciM5uplVmcL+2fJfwrCIREuq2KWvq^
    BB32ECn74rWkPxLbTXSRSiEwH0t/HrMs9XW3s7Ozteq6dFdrEkHItpM35ZVzTLzfg1pnXro1yBjvN3+Ud45ZfYETIKYceEiTe+N3V2TYTxZuij1/O8LH9/fEHfEw3B04DqU8i8Okp33t1/sm+IMf4pYs^
    PgD0bnlxBCH/+ejBfNVU9yHj1SIuE/bkPTtn1cFlmTvjrtSeFvnLw6/X8AvD8vw6cDAAA=';^
    $data = [System.Convert]::FromBase64String($PAYLOAD);$ms = New-Object System.IO.MemoryStream;$ms.Write($data, 0, $data.Length);$ms.Seek(0,0) ^| Out-Null;$cs = New-Object System.IO.Compression.GZipStream($ms, [System.IO.Compression.CompressionMode]::Decompress);$sr = New-Object System.IO.StreamReader($cs);$t = $sr.readtoend();Invoke-Expression $t;\"" /f
    
    

    http://forums.mydigitallife.net/attachment.php?attachmentid=32401&stc=1
     

    Attached Files:

    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    #13 SwampFox56, Dec 2, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    ^^

    Personally - my context menu is cluttered enough as it is. Putting something there would honestly just be an inconvenience for me. But I finally got EXACTLY what I wanted! :D

    Using this as a reference - MSDN WMIServices I finally figured out how to get VB to restart explorer without the need for CMD commands. I used the below script to do this. Problem is, at the "Next" command, I kept getting 'Error at line 9 char 1" This was the script.

    Code:
    Dim objWMIService : Set objWMIService = GetObject ("winmgmts:")
    Dim ProcessList, Process
    
    
    Set ProcessList= objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name ='explorer.exe' ")
    
    
    For Each process In ProcessList
        process.Terminate()
    Next
    
    
    objWMIService.Get ("Win32_Process").Create "explorer.exe"
    
    So I kept digging in the MSDN information and found that every statement needs to be 'explicit' - MSDN Source. Simple solution; add "Option Explicit" at the beginning of the script. Final VBS looks like this.

    Code:
    Option Explicit
    Dim objWMIService : Set objWMIService = GetObject ("winmgmts:")
    Dim ProcessList, Process
    
    
    Set ProcessList= objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name ='explorer.exe' ")
    
    
    For Each process In ProcessList
        process.Terminate(1)
    Next
    
    
    objWMIService.Get ("Win32_Process").Create "explorer.exe"
    Formatted the code in Dreamweaver, and now it looks nice and pretty ;). Also, to keep Windows Explorer from restart automatically (and thus opening the 'My PC" explorer windows in addition to the shell) I added the the '1' in between the parentheses. This keeps windows from restarting the process. Once, I wrap this into an executable - I'll post it here for everyone :)
     
  14. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    103
    10
    #14 Compo, Dec 2, 2014
    Last edited by a moderator: Apr 20, 2017
    explicit statement isn't necessary…
    Code:
    On Error Resume Next
    strProcess = "explorer.exe"
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcess & "'")
    For Each objProcess in colProcessList
    objProcess.Terminate(1)
    Next
    Set objShell = CreateObject("Wscript.Shell") 
    objShell.Run strProcess 
    Set objShell = Nothing
    Wscript.Exit
     
  15. SwampFox56

    SwampFox56 MDL Novice

    Aug 14, 2012
    47
    15
    0
    #15 SwampFox56, Dec 3, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I remember reading something that Set objWMIService = GetObject("winmgmts:") was the minimal required specification. I also remember reading that using windows management services couldn't be used with 'Impersonate' commands. But, then again, I'm new to programming with VB.

    If you could elaborate a bit and walk me through your VB script, I'd definitely appreciate it :)
     
  16. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    12
    10
    #16 Kamrul08, Dec 3, 2014
    Last edited by a moderator: Apr 20, 2017
    Second command is working fine. will it work in xp, seven?
     
  17. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,305
    18,123
    340
    Hmm ... is pure batch code, should definitely work in Win 7, and afaik in XP likewise. As you cannot harm your system that way ... test XP to be sure ;).
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    12
    10
    Thanks. Another ask.
    In my windows 8.1 I've two user account. One is Administrator and another is standard. I operate with standard in it's my default user account. When I use Run as Administrator to run your code, Explorer closes but doesn't restart again. I'm to run manually. But woks fine when run with standard. Any solution please?
     
  19. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,305
    18,123
    340
    You're 'barking' the wrong three for that :D. Script is by Compo, could only assume here, never tested this :g:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    14,305
    18,123
    340
    When checking my memory, i remember some members (Mr Jinje and hb860) discussing how to reliably and 'gracefully' kill Explorer and then restart. The way they used in my tests worked very well, whereas the method described by Compo often fails, due to timing issues. If Explorer.exe is started too shortly after the kill, Explorer Window pops up but not the Shell.

    For that reason i prefer the nice Context menu i posted earlier in this thread.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...