0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
slyfox1186
  • 301
  • 1
  • 13
  • This site is for help fixing problematic code, your code doesn't have a problem, and is therefore off topic. – Compo Nov 13 '19 at 14:35
  • I will reread the rules to make sure I am following the protocol. However, I somewhat disagree. I have not achieved a working code in a one liner form. This is my ultimate goal. Please try to see this as a work around that is not ideal and therefore requires improvement. I don't know if you will agree with that but that was my thought process when I asked for help. Could you at least tell me if you know it is possible to convert this into a one line script? Or do you not know either? – slyfox1186 Nov 13 '19 at 14:35
  • Sorry, but in order for us to help you with your one liner, you must post the one liner code which is failing to work as written and intended, and along side it fully explain what happened when you ran it which differed from the intent. – Compo Nov 13 '19 at 15:13
  • Ok I'll get to working on that. Thanks for reaching out. – slyfox1186 Nov 13 '19 at 15:16
  • 2
    You can try replacing all the new lines with `&`. You won't be able to use `goto` or `call` -- but then again both of those look to be completely pointless in your code. – avery_larry Nov 13 '19 at 16:45
  • ... and `if "%errorlevel%"=="0"` can be replaced by `&&` – Stephan Nov 13 '19 at 20:05
  • Thanks for the info guys. I am trying to understand why adding goto and call commands are pointless. I guess I must have a different goal in how I want my script to execute. If the goal is to keep the code as minimal as possible I can understand why that would be a good thing. I apprecaite the feed back so far. – slyfox1186 Nov 13 '19 at 21:17
  • 1
    The reason for replacing the `if` statement with `&&` isn't to keep it short, but to avoid that ugly [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) (also `call` and `goto` don't work outside batch files) – Stephan Nov 14 '19 at 08:16
  • I have rolled back your latest edit. Please do not edit your question to be or to include the answer. There is an answer location provided exactly for that purpose. – Compo Nov 21 '19 at 19:00
  • Ok. Thanks for the information. – slyfox1186 Nov 21 '19 at 19:02

0 Answers0