Got a .bat file. It works well but......

Discussion in 'Scripting' started by tnx, Jan 28, 2021.

  1. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    Been messing about making myself a SHA1 and SHA2556 checksum calculator and verifier.
    It's coming along nicely but I need a bit of help with a .bat script I am using to get file names and run my .exe

    I have set up a right click on the Context Menu to get the full path and name of and image, a .iso or a .svf image file.

    It gets the image paths, writes a .text file and then runs my .exe which reads the text file and carries on working.

    What I don't like though is the command window pops up for a fraction of a second just before my .exe runs and I want this not to happen.

    I was hoping somebody in the know could help me out. I would like to write it in .vbs but I am unsure.

    Anyway here is my script, a .bat file

    Code:
    @echo OFF
    
    set File="%1"
    
    for /F "delims=" %%i in (%File%) do set dirname=%%~dpi 
    for /F "delims=" %%i in (%File%) do set filename=%%~nxi
    for /F "delims=" %%i in (%File%) do set basename=%%~ni
    
    for /F "delims=" %%i in (%File%) do set FullPathNoQuotes=%%~i
    
    ::echo |set /p=%File%|clip
    
    echo %FullPathNoQuotes%  > C:\VeriHash\Image.txt
    echo %filename% >> C:\VeriHash\Image.txt
    
    
    Start C:\VeriHash\VeriHash.exe
    
    exit
    As you can see I was going to use the Clipboard at first before I decided to get both the full path and image name but left that bit of code there for any future reference.

    It works just by dropping an image onto the .bat too.

    So is there any way to have the text file written to without any command window showing what so ever ?
     
  2. Flipp3r

    Flipp3r MDL Expert

    Joined:
    Feb 11, 2009
    Messages:
    1,743
    Likes Received:
    772
    Trophy Points:
    60
    What if you create a shortcut to your batch file and change the properties to run minimized instead of normal.
    I'm not too sure but maybe change it from .bat to .cmd...
     
  3. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    i tried using a .vbs to call the .bat but that didn’t pick up the file path, name info I wanted.
     
  4. Flipp3r

    Flipp3r MDL Expert

    Joined:
    Feb 11, 2009
    Messages:
    1,743
    Likes Received:
    772
    Trophy Points:
    60
    Did you try making the shortcut? You can even set it to run as Admin...
     
  5. adric

    adric MDL Addicted

    Joined:
    Jul 30, 2009
    Messages:
    846
    Likes Received:
    580
    Trophy Points:
    30
    I usually use the following scheme to hide the console window:
    Code:
    Set WshShell = createobject("wscript.shell")
    WshShell.Run "Your Batch.cmd",0,false
    WScript.Quit
    
    The "0" will hide it and false doesn't wait for the batch to end.
     
  6. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    Yep, tried that but it doesn't carry the info of the file into the .bat.
     
  7. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    I have this .vbs script

    Code:
    Set objFS=CreateObject("Scripting.FileSystemObject")
    Set objArgs = WScript.Arguments
    strFile= objArgs(0)
    Set objFile = objFS.OpenTextFile(strFile)
    Set objFile = objFS.GetFile(strFile)
    WScript.Echo objFile.Path
    If i drop a file onto this .vbs file I get the full path, which is good.

    If I add this to the registry
    Code:
    Windows Registry Editor Version 5.00
    
    :: vbs get path
    [HKEY_CLASSES_ROOT\*\shell\VBS Get Path]
    "icon"="C:\\VeriHash\\Hash.ico"
    
    
    [HKEY_CLASSES_ROOT\*\shell\VBS Get Path\command]
    @="WScript C:\\VeriHash\\vbsGetPath.vbs"
    The through the context menu, right click to get the path I get an error, "subscript out of rang"

    any ideas guys ?
     
  8. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    right...
    This .vbs script works on the right click

    Code:
    Function GetSelectedFiles() 'Returns paths as array of strings
        Dim FileList, Window, SelectedItem
        'avoid duplicates by storing paths in dictionary keys
        Set FileList = CreateObject("Scripting.Dictionary")
    
        With CreateObject("Shell.Application")
            For Each Window In .Windows
                'skip IE Windows
                If InStr(1, Window.FullName, "iexplore.exe", vbTextCompare) = 0 Then
                    For Each SelectedItem In Window.Document.SelectedItems
                        FileList(SelectedItem.Path) = Null
                    Next
                End If
            Next
        End With
    
        GetSelectedFiles = FileList.Keys 'array of paths
    End Function
    
    MsgBox  "Click OK after selecting the items",  _
            vbOKOnly Or vbInformation, "Select a few items"
    
    Dim SelectedFiles
        SelectedFiles =  GetSelectedFiles
    
    MsgBox  "You selected: " & vbNewLine  & vbNewLine & _
             Join(SelectedFiles, vbNewLine), vbOKOnly Or vbInformation, "Selected Items"
    
    'loop through array
    'Dim FileItem
    'For Each FileItem In SelectedFiles
    '   WScript.Echo FileItem
    'Next
    Does too much, will have to just right the full path to a text file, or even to the clipboard
     
  9. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    Still having problem with seeing the command window.

    I have this REG code
    Code:
    :: Copy Path On The Context Menu
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\windows.copyaspath]
    "CanonicalName"="{707C7BC6-685A-4A4D-A275-3966A5A3EFAA}"
    "CommandStateHandler"="{3B1599F9-E00A-4BBF-AD3E-B3F99FA87779}"
    "CommandStateSync"=""
    "Description"="@shell32.dll,-30336"
    "Icon"="imageres.dll,-5302"
    "InvokeCommandOnSelection"=dword:00000001
    "MUIVerb"="@shell32.dll,-30329"
    "VerbHandler"="{f3d06e7c-1e45-4a26-847e-f9fcdee59be0}"
    "VerbName"="copyaspath"
    
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shellex\ContextMenuHandlers\CopyAsPathMenu]
    @="{f3d06e7c-1e45-4a26-847e-f9fcdee59be0}"
    Now that copies the file path to the clipboard without seeing any sign of the command window.
    Is it possible to add to that script so it runs a .exe at the end ?

    anybody with any ideas on this
     
  10. haz367

    haz367 MDL Senior Member

    Joined:
    Jan 11, 2020
    Messages:
    325
    Likes Received:
    415
    Trophy Points:
    10
    This is a short version

    tested on Windows 7
    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Copy Path\command]
    @="mshta vbscript:CreateObject(\"wscript.shell\").Run(\"cmd /c chcp 65001 && echo | set /p x=\"\"%1\"\" | clip\",0)(window.close)"
    
    clip /r copyclipboard.txt

    no clue really, bump for you.....
     
  11. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    cheers for that. Will look at it later on, one I get up.
     
  12. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60

    Just tested that and it works pretty nice, cheers.

    Just wondering, can "Start .exe" be added on the end some how ?

    So it grabs the file path and runs my app.
     
  13. haz367

    haz367 MDL Senior Member

    Joined:
    Jan 11, 2020
    Messages:
    325
    Likes Received:
    415
    Trophy Points:
    10
    Hey @tnx

    It took me 5 hrs of my life :p

    Renamed "copy as path" to "verify checksums" and using NirCmd to hide the output, and clip to copy the clipboard and output the path. Then it runs a batch to run MultiHasher.exe.

    No cmd popup and it runs smooth.... :)

    https://a.tmp.ninja/gFsKGIPeabG6.7z

    [​IMG]


    Code:
    
    C:\Users\haz3\Desktop\vmwaretools.iso
    
    ===================================
    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Verify Checksums]
    
    [HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\Verify Checksums\command]
    @="nircmd exec hide cmd /c chcp 65001 && echo | set /p x=%1 | clip | clip2 /r c:\\hash.txt | c:\\hash.bat"
    
    ==================================
    
    @ECHO OFF
    
    for /f "delims=" %%x in (c:\hash.txt) do set "hash=%%x"
    
    c:\multihash\multihasher.exe /HASH=md5,sha512 %hash%
    
    
     
  14. farag

    farag MDL Member

    Joined:
    Apr 1, 2014
    Messages:
    168
    Likes Received:
    201
    Trophy Points:
    10
    Use PowerShell, Luke...
     
  15. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60

    Well....

    I don't know what to say.

    You spent 5 hours doing this for me ?

    Brilliant.

    Thank you very much. !!!!!!!!
     
  16. haz367

    haz367 MDL Senior Member

    Joined:
    Jan 11, 2020
    Messages:
    325
    Likes Received:
    415
    Trophy Points:
    10
    Also did it a bit for my own :p a challenge u know...

    Glad it worked :)
     
  17. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    Well after having a bit of bother I think I have got it.

    This REG entry
    Code:
    @="mshta vbscript:CreateObject(\"wscript.shell\").Run(\"cmd /c chcp 65001 && echo | set /p x=\"\"%1\"\" | clip | c:\\hash.bat\",0)(window.close)"
    points to C\hash.bat
    Code:
    @ECHO OFF
    
    c:\VeriHash\Verihash.exe 
    which calls my VeriHash.exe which when started it reads the clipboard and displays the image name and full path.

    NO command console window flashes up at all, which is exactly what I wanted..

    Many thanks to haz367. Brilliant work on your part. could not have done it without your help..

    THANKS !!
     
  18. haz367

    haz367 MDL Senior Member

    Joined:
    Jan 11, 2020
    Messages:
    325
    Likes Received:
    415
    Trophy Points:
    10
    If it didn't work for you, the issue is that the System32 files should be placed in the Windows folder instead....It works fine on both 7/10 :)

    Nice small line u got there :) Care to share the program? Thx!
     
  19. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    I will look at placing the files in Windows.

    my little project isn’t at any stage yet to be shared right now. Never really thought about sharing it actually.
    More of, I wanted to do summet specific when I realised the .iso images of Windows 10 I download suddenly changed to SHA256 instead of SHA1 ?
    The usual app I used never handled SHA256 so I decided to script some .bats up to calculate checksums.
    This sort of turned into making a nice looking GUI.

    once I get the rough edges smoothed out I’ll drop you a line and let you have it.
    Would be interesting getting somebody else’s opinion, good or bad.

    making the GUI with Autoplay Media Studio. It’s the only one I know how to use.

    cheers again.
     
  20. tnx

    tnx MDL Expert

    Joined:
    Sep 2, 2008
    Messages:
    1,659
    Likes Received:
    256
    Trophy Points:
    60
    Altered the REG file script a little more, eliminating the need for a .bat file

    I now have this
    Code:
    @="mshta vbscript:CreateObject(\"wscript.shell\").Run(\"cmd /c chcp 65001 && echo | set /p x=\"\"%1\"\" | clip | c:\\VeriHash\\VeriHash.exe\",0)(window.close)"
    works pretty well