Cool Batch Tricks - Part III

Hello guys,

        I'm back again with the 3rd part of Cool Batch Tricks. If you’re seeing this post, you must have seen the earlier posts, if you haven’t then click here.

       Cool Batch Tricks - Part I                                          Cool Batch Tricks - Part II

 Its better that yo see earlier parts as it will help you to understand the code.


So, Let’s start.   

Today, we will again learn two cool programs. They are :-


1.    Shutdowm Program

This programs schedules a Shutdown process after a specified period of time. You can Shutdown, Restrart, Log Off, Hibernate your Windows with this program


Code:


@echo off
color a
title Aborting Shutdown
echo Aborting Shutdown
shutdown.exe -a
if %errorlevel%==1116 goto timer
echo The Shutdown was Aborted
pause
exit

:timer
title Settng Up Shutdown
cls
echo.
echo.
echo WARNING : Timer Does not work with Log Off and Hibernate
echo.
echo.
set time=000
echo Enter the time-out period in seconds
set /p time=Time:
echo Time set as %time% seconds

:choice
echo What you want to do:
echo.
echo 1. Shutdown & set p1=Shutdown
echo 2. Restart & set p2=Restart
echo 3. Log Off & set p3=Log_Off
echo 4. Hibernate & set p4=Hibernate
echo.
echo Choose Your Option:
set /p a=Option:
if %a%==1 set p=%p1% & shutdown.exe -s -t %time%
if %a%==2 set p=%p2% & shutdown.exe -r -t %time%
if %a%==3 set p=%p3% & shutdown.exe -l
if %a%==4 set p=%p4% & shutdown.exe -h -t
echo Your computer will %p% in %time% seconds
pause
exit






Output:

Aborting Shutdown


Shutting Down after 500 Secs
        




          I won’t be explaining the lines I already explained in previous posts.

          This program first Aborts any shutdown if any is predefined. For this we use the shutdown.exe with parameter /a. Here another variable named ‘errorlevel’ is introduced. If a command is performed and if any error occurs then the error code is stored in this variable. We use this to check for any predefined shutdowns, if there are any it will terminate them and exit. Other wise, it jumps to the next section. As warned, the timer doesn’t work for Log Off and Hibernate. As you can see, we’ve used & in some lines. It is used for starting a new line.

          I hope you understand the program. If not then comment your problem.
We’ll definitely help you.

2.   Task Manager

We know there exists a GUI application, but it would be more cool to get a CLI.

Code:

@echo off
Title Task Manager
color a
:start
cls
echo.
echo.
echo ------------
echo Task Manager
echo ------------
echo.
echo.
echo What you want to do??
echo 1. Get Tasklist
echo 2. Create a new task
echo 3. End a Task
echo 4. Exit
set /p c=Choice:
if %c%==1 goto tasklist
if %c%==2 goto newtask
if %c%==3 goto endtask
if %c%==4 goto exit
echo Wrong Choice
pause
goto start

:tasklist
cls
echo The current running tasks are :-
echo.
tasklist
pause
goto start

:newtask
cls
echo Enter the path of the new task :
set /p p=Path with filename and extension:
start %p%
if %errorlevel%==9059 goto error
echo Task has been successfully launched
pause
goto start

:error
cls
if %errorlevel%==9059 echo The path or program name is incorrect
if %errorlevel%==128 echo The program %tname% is not running
pause
goto start

:endtask
cls
echo Enter the name of the task to end:
set /p tname=Name (without extension):
taskkill /IM %tname%.exe
if %errorlevel%==128 goto error
echo The program %tname% was successfully terminated
pause
goto start

:exit
cls
echo Exitting Program...
pause
exit


Output:

Show Tasklist

       
Create Fucntion


          Here, we’ve used the three commands for three different functions. They are tasklist to show task List,start to start an application,taskkill to End a Task. It may look lengthy but it is very simple to understand. The command taskkill is used with varius parameters. For ex. You can modify the code and end a task by using it Process ID. For that open Command Prompt and type, taskkill /?.

Batch Files :                                  1. Shutdown 
                                                      2. Task Manager


So, these were the two programs of this tutorial. If you like this post please support us by sharing the post. Don't Forget to visit our Facebook & Instagram pages, If you want to contact us, write us at Remedyhub11@gmail.com & don’t forget to subscribe.