I would like to make my multi-line batch script into a one liner so I can use it as a file explorer context menu entry. I know that I can link the batch file in the registry to call the batch script and it does work.
I am trying to get more proficient with my coding so I was wondering if anyone could take a look and give me some pointers. I have been researching and testing to find the answer myself but have come up short. I will include two scripts that both achieve the same thing and both work if I link the batch script path in the registry command key.
Script A
is more complex and offers more options for the code to follow depending on what it encounters during it's run. Script B
is simpler and still achieves the main purpose of the script in the end.
Script A is called from the registry in a custom context menu that runs this line of code in the registry entry's command key.
"C:\Users\jholl\OneDrive\Documents\01_Scripts\01_REGISTRY_MAIN\Registry_Edits\File_Explorer\Context_Menu\Restart_Explorer\Restart_Explorer.bat" "%V"
The reason I added the "%V"
at the end of the bat file is to pass custom command line arguments to the batch script so that when I access the context menu using a folder's directory\background and click the menu entry named "Restart Explorer" it captures the current directory in the "%~1" argument that I can then use to reopen file explorer to that same folder when I relaunch explorer.exe at the end of the script after taskkill closes all instances of explorer.exe.
This is way more convenient than having to relocate the folder again manually by navigating through file explorer.
@echo off
setlocal
prompt $g
call :restart_explorer "%~1"
goto:eof
:restart_explorer
tasklist /fi "imagename eq explorer.exe" | find /i /n "explorer.exe" >nul
if "%errorlevel%"=="0" (
start "" /wait taskkill /f /im explorer.exe &&^
start "" explorer &&^
start "" /max explorer "%~1" &&^
exit /b
) else (
goto failed
)
:failed
cls && echo.
echo taskkill was unable to find/close explorer.exe
echo it is most likely not running
timeout 5 >nul
exit /b
Script B is just a simplified code. No more. Same goal as above with less steps.
@echo off
setlocal
prompt $g
call :restart_explorer "%~1"
goto:eof
:restart_explorer
tasklist /fi "imagename eq explorer.exe" | find /i /n "explorer.exe" >nul
if "%errorlevel%"=="0" (start "" /wait taskkill /f /im explorer.exe)
start "" explorer &&^
start "" /max explorer "%~1" &&^
exit /b
Any ideas to help give me a boost would be greatly apprecaited. Again I'm looking to turn this into a one liner.