Want to make .bat (or .cmd) scripts? This is for you!
These are the simple stuff, but very useful.
echo
is basically the console.log or console.WriteLine of Batchfile. It is important to put @echo off
(or echo off
if that doesn't work) on the first line of your script.
Input: echo Never
Output: Never
appears on the console.
start
is a really important command, as it can start a program. The file has to exist in the working directory (the batch file's location by default). You can usually omit start
from the command and just put in Gonna.exe
, but in some cases, this is important.
Note: If you open a console app with start
on it, it will open as an individual program, rather than on the same command line window.
Input: start Gonna.exe
Output: Gonna.exe will open.
del
is a command for deleting files. The file has to exist in the working directory (the batch file's location by default).
Input: del Give.txt
Output: Give.txt is deleted.
cd
is a command for telling the prompt where the working directory is. It needs to exist to be valid (see mkdir).
Input: cd C:\You
Output: C:\You is the new working directory.
mkdir
is used for making directories.
Input: mkdir C:\Up
Output: C:\Up is created.
pause
is used for the "Press any key to continue" thing.
Input: pause
Output: Press any key to continue...
shows up from the console.
If you do pause >nul
, it will pause, but not say "Press any key to continue". You will still have to press any key to continue.
title
is used for making the title (the window name) of the command prompt what you want.
Input: title Test Prompt
Output: The title becomes Test Prompt.
call
is start
, but it runs the process seperately.
It starts the process, and reports what it says in the command line back to you, unlike start
, where the window becomes the process.
This can fix "clogging", where, for example, you run a python script, and it prematurely exits after the script finishes.
Input: call "pythonfiles/x64/python.exe" "dostuff.py" && call "pythonfiles/x64/python.exe" "dosomethingelse.py"
Output: It will start dostuff.py, and start dosomethingelse.py after it's finished, instead of exiting.
goto
is basically the functions of Batchfile.
You need to make a "function" for goto. You make a function by doing this:
:FunctionName
; your code here
There is your function.
To execute the function, do goto FunctionName
.
These are the stuff that you can copy and paste, and do what you please with it.
This has been tested to work on Windows XP and later.
@echo off
goto check_Permissions
:check_Permissions
net session >nul 2>&1
if %errorLevel% == 0 (
; put code that requires admin here
) else (
; if there is no admin, do something here
)
pause >nul
if not exist "; put your directory here" (
mkdir "; put your directory here"
)
set /p menu="Put what you want to say here"
if %menu%==1 goto OptionOne
if %menu%==2 goto OptionTwo
; You can put more options here, and change the numbers to anything
cls
:OptionOne
; Your code here
:OptionTwo
; Your code here