Optimizing Batch Codes II
Hello everyone, I haven posted a tutorial in a while so here is a sequel to optimizing batch codes. The original can be found here!
I also recommend A full screen matrix batch code here
Thank you everyone for over 50,000 views!
& Again
The & command allows you to run multiple commands on one line of code.
For example
...
echo hello & goto lable
...
This can be useful for not using lots of lines in a long code.
Something I found out recently is that some commands dont require a space; however, I wouldn't recommend the following method because it impedes on readability
...
Echo hello&goto lable
...
This will have the same output.
Set Doesn't Need Spaces
Set /p is a very useful allowing users to input and set /a a counterpart that can preform basic arithmatic (+-/*).
...
Set /p var=
...
Can be shortened to
...
Set /pvar=
...
Again I don't recommend using this often but to save a byte here and there it works.
Counting From Zero
Occasionally seting a variable to count something is required for example if I want to keep track of how many times a batch file is run and save the number in a text file I can.
Here is a basic counter
...
set count=0
set/acount+=1
...
Notice I removed the space in set /a
The first line isn't necessary because it automatically starts at zero.
Redirection
In batch it is possible to send text or a variable to a text file. A space isn't needed before the ">" and it's still legible.
...
@Echo This is some Text > FileName.txt
:no space
@Echo Some more text>FileName.txt
:I'm not using goto so this lable should work as a comment too.
...
@echo Off
Every batch script you see will probably start with that line but if it's short it might not have to.
@echo off is 9 bytes so if you have less than 9 lines you can use "@" to suppress it
...
@echo hello world
@ pause
...
Or even shorter
...
@echo hello world&pause
...
Final Note
Thanks everyone I hope these helped you. Not everything in the prequel was mentioned so I recommend checking it out.