Skip to content

Instantly share code, notes, and snippets.

@thepwrtank18
Last active July 26, 2021 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thepwrtank18/2cad5894c4d5d741b1f5b6ba4db2a249 to your computer and use it in GitHub Desktop.
Save thepwrtank18/2cad5894c4d5d741b1f5b6ba4db2a249 to your computer and use it in GitHub Desktop.
Batchfile Tips and Tricks

Batchfile

Want to make .bat (or .cmd) scripts? This is for you!

Simple stuff

These are the simple stuff, but very useful.

echo

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

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

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

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

mkdiris used for making directories.

Input: mkdir C:\Up

Output: C:\Up is created.

pause

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

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

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

goto is basically the functions of Batchfile.

Part 1

You need to make a "function" for goto. You make a function by doing this:

:FunctionName
; your code here

There is your function.

Part 2

To execute the function, do goto FunctionName.

Other stuff

These are the stuff that you can copy and paste, and do what you please with it.

Check for admin

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

Create folder if it doesn't exist

if not exist "; put your directory here" (
   mkdir "; put your directory here"
)

Create a choice

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment