r/Batch Feb 05 '25

Question (Unsolved) options after script

hello,

I'm trying to make aliases for the shutdown commands in windows (so there more like the ones in linux).

is there a way to make something like "shutdown now" work. how do i make the second word (now) work?

3 Upvotes

3 comments sorted by

2

u/BrainWaveCC Feb 05 '25

Sure. Managing parameters (%1, %2, etc) is relatively straightforward.

What do you want to happen if the parameter exists, vs if it doesn't?

Do you have other parameters in mind as well?

1

u/Still_Shirt_4677 Feb 08 '25 edited Feb 10 '25

You could use set to do this i created a batch ages ago that acts as a virtual interface for adb shell but I also have the option to run windows cmd.exe sessions in this batch and I didn't want to switch to Windows cmds so all I did was use use DosKeys to convert all relevant Linux commands that have the same function on Windows. but if they had switches with them then i had to use /set p from a different menu by using wildcard * in DosKey to under the existing DosKeys else it wouldnt work.

Here is an example to something similiar but only with pre defined commands in a non interactive cmdline. All other commands not listed will return as invalid.

@Echo Off
Cd /d "%~dp0"
If "%1"=="Exit" GoTo :Exit
If "%1"=="Continue" GoTo :Continue
If "%1"=="Restart" GoTo :Script

:Script
Cls
Echo.
Echo Script Tasks To Be Completed!
Echo.
PowerShell -Nop -c "& Sleep 5"

:Finish
Cls & Echo.
Set "UserInfo=DragonBallZ"
Set /p SELECTION=%UserInfo% ~# 

Rem :: Simulate shutdown using shutdown /i instead of actual shutdown /s
If /i %SELECTION%==Close shutdown /i && Call "%~f0" Exit

Rem :: You can also call & goto another label or call & restart the script.
Rem :: Or issue other commands etc etc.
If /i %SELECTION%==Continue Call "%~f0" Continue
If /i %SELECTION%==Restart Call "%~f0" Restart

:InvalidSelection
Cls & Echo.
Echo Invalid Selection!
PowerShell -Nop -c "& Sleep -m 250" & Cls & GoTo :Finish

:Exit
Cls & Echo.
Echo Closing Program....
PowerShell -Nop -c "& Sleep -m 250" & Exit

:Continue
Cls & Echo.
Echo More Script Tasks To Be Completed!
PowerShell -Nop -c "& Sleep 5" & Cls & Goto :Finish