Introduction: Optimize Batch Codes

There are many cool batch codes but many ae very long and inefficient here is a tutorial explaining how to shorten them for optimal prefomance.

Step 1: If Statements

Nearly all batch codes have an if statement
If %cho% ==Y goto yes
If %cho% ==y goto yes
But this isn't a good way to do things look at this script to select a website

Script starts here
@echo off
Cls
Echo chose a site
Echo 1 google.com
Echo 2 example.com
Set /p site=
If %site% == 1 goto 1
If %site% == 2 goto 2
:1
Start google.com
:2
Start google.com
Pause
Script_ends

What's wrong with this script first why use %site% just use %s% it's shorter not that it makes a big deal but it is shorter especially if you have a lot of sites you want to go to. Secondly don't goto 1 or 2 just type
if %s% == 1 start google.com
Again this is shorter

The next code is also inefficient

Script below
@ECHO off
Cls
Set /p cho=
If %cho% == Y goto yes
If %cho% == y goto yes
Script ends

Rather use if /I so either y or Y will be interpreted the same way
If /I %cho% ==y goto yes

Step 2: Using 2 Commands at Once

This doesn't shorten the amount of text used but it can shorten the amount of lines
Using && joins two commands like this

@echo off
Cls

That works fine but you can also do this

@echo off && cls

Both will give you the same output.

Step 3: Set Variables

Setting variables can shorten things in two different ways. The first way will be setting data the second will be setting commands.
This is the script I will use to demonstrate using the set command

Script begins
@echo off && cls
Echo type 1 for calculator
Echo type two for internet
Set /p cho=
goto %cho%
:1
Start calculator.bat
:2
Start google.com
Pause
Script_ends

now I know that I showed you how to start google.com but let's say you have a different section of code that doesn't start something this script goes to what ever the user types in.

Modified script
@echo off && cls
Set "e=echo"
%e% type 1 for calculator
%e% type two for internet
Set /p cho=
goto %cho%
:1
Start calculator.bat
:2
Start google.com
Pause
Script_ends

What this does is sets the command echo as %e% now it only shaved off one character this is 3 and echo is 4 but it can be used if you use a command a lot or can be used for a longer command like seen below.

set "var=if %word%=="
%var%1234

Step 4: Developer Notes

At the beginning of lots of scripts developers leave thir name and matey how to use it and the script becomes littered like this

Script
:: blah
:: blah
Rem blah
Rem blah
Rem blah
Script_ends

To avoid having lots of comments use this

Script
Goto skip
Blah
Blah
Blah
:skip
Script_ends

This will allow you to remove comment lines like rem or ::

Step 5: The Challange

Take the following script optimize it and post it in the comments


cls
@ECHO OFF
title Folder Private
if EXIST "HTG Locker" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "HTG Locker"
attrib +h +s "HTG Locker"
echo Folder locked
goto End
:UNLOCK
echo Enter password to unlock folder
set/p "pass=>"
if NOT %pass%== PASSWORD_GOES_HERE goto FAIL
attrib -h -s "HTG Locker"
ren "HTG Locker" Private
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
:End


Please also view my other batch codes here

My favorite is a FULL SCREEN matrix here

Thank you everyone