Create BATch file - CMD, BAT commands in Windows (DOS)
BATch commands date back to the DOS era and are still a useful tool for certain uses, yet Windows PowerShell is usually the better choice for administering and automating tasks today, see: Windows PowerShell.
The commands described here were tested under Windows 2000/XP, but are also valid for Windows 10 / 11.
The command "help" in the Command Prompt provides an overview of the available commands. With the command name and /? more detailed information about the respective command can be displayed, e.g. xcopy /?
Applications of batch files
Connect network drives
Create files / directories
Logon script ...
The application areas for batch files are simple processes. For more complex processes, other scripting languages, such as Powershell, are predominantly better suited.
Creating batch files:
A BATch file can be created most simply over the editor, as example Notepad, by using as file extension ".cmd" or ".bat". (save as: filename.cmd, or filename.bat) (Attention: in Explorer you have to deactivate Extras, Folder Options, View, Hide extensions for known file types, otherwise the file will be saved as something.cmd.txt; alternatively the filename in the editor can also be put under quotation marks "something.cmd") Commands can then be written into the file, these will be executed when the file is started.
Basic: echo / echo off, pause command:
With the command @echo off, at the beginning of the batch file, the output of the command line can be suppressed, only the result of the command is then output.
With echo display text any text can be output.
The pause command waits for any keystroke
echo This is a Textfile
the batch command outputs the text: This is test file and then waits for a keystroke
small backup tasks with xcopy Backup folder:
Example to xcopy
xcopy c:\Documents d:\save /D /E /Y /I
Parameters of the xcopy command: /D means only newer files /E take subdirectories /Y overwrite files without asking /I create new folder if not existing
The command in a batch file copies all new files the folder d:\secure A network share of another PC can also be used as destination: xcopy c:\own~1 \securepc\secure /D /E /Y /I or when using long file names: (just put between quotes) xcopy "c:\own files". \securepc\secure /D /E /Y /I
You can also put the file into the autostart directory, so that everything is backed up at every system startup, or create backup jobs with scheduled tasks!
the option exclude can ignore certain files or directories: Example: xcopy "c:\Documents and Settings" z: /y /d /h /e /i /exclude:nocopy.txt For ignoring the file nocopy.txt is needed for this example. The file must be in the same folder as the batch file. As contents all files and/or folders can be specified, which are not to be copied. e.g.: ---------------- Temporary Internet Files .tmp ---------------- .tmp means all files with the ending tmp, the specified folders and/or files must stand in each case in a new line, the *-placeholder (so.* ; *.tmp) does not function!
With set var= the variable var can be given any value, or text.
There are a number of variables that are available from Windows by default. An overview of which variables are set is given by the command "set" without further option (i.e.: Start, Run, cmd, ... set) Typing "set" will output a list of all available variables with their contents: e.g. windir=c:\WINDOWS USERNAME= your user name or USERPROFILE the user profile currently logged in. Variables can be used within %: %USERPROFIL% or %USERDOMAIN% so always a % before and a % after the variable (as already with the self set variables).
echo in the loop
with goto the batch processing jumps to any position of the file
TEST: does a file exist?:
if exist %file.txt goto jump
if the file file.txt exists, the process jumps to jump Example:
if exist %datei.txt goto jump
echo File not found
echo File does not exist
Each command returns certain errorlevel: mostly errorlevel 1 means that the command was not successful: example:
xcopy c:\so c:\so2
if errorlevel 1 goto error
echo copy was not successful!
In this example the directory c:\so is copied to c:\so2, if the folder named "so" does not exist, the batch file writes: The copy was not successful!
Create a 2nd file, add:
echo hello > temp.txt
This command line creates a temp.txt file and writes hello in it! If the file temp.txt already exists, the whole content of the file is overwritten!
echo hello >> temp.txt
Adds the text hello to the temp.txt, i.e. with each time call of the command stands in temp.txt once more hello in it, the contents of the file remain.
creates then e.g.:20050921
Commands: open... Opens the FTP connection; requires: Server, username and password put... sends a file get... downloads a file lcd... change directory on computer cd... change directory on server bye... terminates the FTP connection
For image and program files you have to use binary mode, for text files ASCII mode.
put C:\documents\homepage\images\image.gif image.gif
Explanation: The file opens the FTP connection, switches to ASCII- mode (for text files); copies all file index.htm to the server; then switches to the image directory at the computer, switches to BINARY - mode and sends the image.gif- file!
Stored in a .ftp- file, it can be called with a BATch- file as follows: Calling the BATch file:
Even easier is the upload with the commandline tool curl