A batch file contains a series of DOS (Windows) commands and is commonly written to automate frequently performed and repetitive computer tasks.
CREATE AND SAVE A BATCH FILE
Open notepad, type your batch script (examples shown below), then save as type: All Files (*.*) and make sure the encoding is ANSI. Name your file and give it a .bat or .cmd file name.
TIP: Use the .cmd extension if you are using Windows 2000 or more recent. This uses 32 bit execution instead of 16 bit.
TIP: If a path has spaces, enclose it in quote marks: “C:\path with spaces\”
TIP: DOS commands are CaSe InSeNsItIvE
TIP: DOS commands, by default, will display the script as well as the executed output, including all complete paths.
BASIC BATCH COMMANDS
ECHO – Displays whatever text is written after it on the screen (ie it prints it’s own arguments).
ECHO This text will be written to the screen
ECHO. [This makes a blank line]
@ECHO OFF – Hides the script from view. Normally the program shows the lines of the executed output. @ECHO OFF hides the script output which makes for tidier viewing. The ‘@’ sign tells the program to also hide ‘ECHO OFF’.
@ECHO OFF [This will hide the scripts that follow] ...
START – Runs a file with its default application.
START "title" /MIN Myscript.bat
START "" /WAIT photoshop.exe
Title is require. You can use “” if you really don’t want a title for the window.
/MIN – The opened window is minimised. (or /MAX for maximised)
/WAIT – Waits for the program to finish opening before continuing the script
REM – This is a comment – a REMark that does not get executed. Another way to do this is ::
REM This text will not be written to the screen or executed. It is here for your viewing pleasure!
MKDIR/RMDIR – Creates and removes directories.
[This creates 2 new directories and deletes the first one] MKDIR "C:\newdirectory 1" MKDIR "C:\newdirectory 2" RMDIR "C:\newdirectory 1"
DEL – Deletes a file/s. You can also use ERASE.
DEL "delete this file.txt" ERASE "Erase this file.txt"
COPY – Copy’s a file/s.
COPY "C:\my folder\copy this file.txt" "D:\New folder\newfile.txt"
CLS – Clears the screen.
PAUSE – Suspends processing of the program and displays ‘Press any key to continue’. Normally the script will execute and then swiftly close. PAUSE enables you to see the results of the executed scripts.
PAUSE
XCOPY– Copies files with extra options:
TIP: XCOPY will not copy open files and cannot copy paths more than 254 characters.
@ECHO OFF XCOPY C:\original C:\backupfolder /m /e /y
This copies files from an original folder to a backup folder.
/m – Specifies only updated files will be copied.
/e – Specifies that all sub-directories in the listed directory will be copied.
/y – Keeps the confirmation message appearing every time a file is overwritten.
Random examples
ECHO
@echo off echo Hello World! pause
Outputs to:
Hello world!
Press any key to continue
XCOPY
REM Copy a file to another folder XCOPY "C:\folder\my file" D:\folder\my file
REM Copy a folder inluding all sub folders XCOPY C:\folder* D:\Backup /i [/i states the destination as a folder
Made with a little help from: