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.

Help

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

Creating batch files:

Basic: echo / echo off, pause command:

 @echo off 
 echo This is a Textfile
 pause 

small backup tasks with xcopy
Backup folder:

@echo off
 xcopy c:\Documents d:\save /D /E /Y /I
 pause 

xcopy exclude

Set variables:

@echo off 
 set var=value
 echo %var% 
 pause 

Windows variables:

Jump:

@echo off 
 :start 
 echo in the loop
 goto start 

TEST: does a file exist?:

if exist %file.txt goto jump
@echo off 
 
 if exist %datei.txt goto jump
 echo File not found
 goto END
 
 :jump
 echo File does not exist
 
 :END

Errorlevel:

 @echo off
 xcopy c:\so c:\so2
 if errorlevel 1 goto error
 goto end
 :error
 echo copy was not successful!
 :end
 pause

Create a 2nd file, add:

echo hello > temp.txt 
echo hello >> temp.txt 

Date

set year=%date:~-4%
set month=%date:~-7,2%
set day=%date:~-10,2%
 
echo %year%%month%%day%

creates then e.g.:20050921

Automate FTP:

OPEN www.ftpserver.com 
 username
 password
 
 CD html 
 ASCII 
 PUT C:\documents\homepage\index.htm 
 CD .. 
 
 CD images
 BINARY 
 put C:\documents\homepage\images\image.gif image.gif 
 bye 
ftp -s:dieftpdatei.ftp
curl -T C:\documents\homepage\index.htm --user USERNAME:PASSWORD

further topics or instructions for beginners

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details