Batch files allow you to automate a lot of actions on Windows thanks to DOS commands which will be interpreted by the "cmd.exe" program present in all versions of Windows.
On Windows, a batch file is a simple text file with a list of commands and sometimes conditions (as in a real program).
Little advice to start with batch files : use a text editor like Notepad++ to benefit from syntax highlighting and thus better understand what you write.
Among the available commands, you will find basic commands :
You can also use network commands that correspond to the network command line programs available in all versions of Windows :
Note that it is also possible to call another batch file from yours using the "CALL" command.
Finally, know that you can add "/?" after any command to obtain the list of options available for it and informations allowing you to use it correctly.
To manage some conditions in a batch file, there are keywords : IF, FOR, GOTO, ...
To make your program more pleasant to use, we recommend that you hide the display of commands.
To do this, simply add this line at the beginning of your batch file.
By default, when you launch a batch file, the window title is "C:\Windows\System32\cmd.exe".
To display a custom title, just use the "title" command like this :
title Program name
To learn in depth how to create professional batch files with conditions, variables, loops, ... we recommend that you go to the "Batcher" website which explains from A to Z everything you need to know about batch files.
To ask the user to enter a value, you must use the SET command like this :
@echo off REM We ask the user to enter a value. Param /p REM Note : Remember to leave a space at the end of the line so that the text REM does not stick to the program text. Set /p val=Enter a value: REM Then, we display the value that the user entered. echo You wrote : %val% pause
Retrieving a part of a variable can be useful in several cases and in particular to recover the different pieces of a date, the extension of a file from its full name, ...
In short, here is how it works :
REM The line below will retrieve the characters of the specified variable from index 5 REM and over a length of 2 characters. Then, it will display the result in the console. echo %variable_name:~5,2%
Thanks to this syntax, we can retrieve the day, month, year, hour, minutes, ... from the current date and time.
@echo off REM Full date. For example, in Windows 7 (in French), this variable contains this : jeu. 30/05/2013 echo Date : %date% echo Day : %date:~5,2% echo Month : %date:~8,2% echo Year : %date:~11,4% REM Echo. displays a empty line. echo. REM Time. For example, in Windows 7 (in French), this variable contains the following : 11:12:36,67 echo Time : %time% echo Hour : %time:~0,2% echo Minutes : %time:~3,2% echo Seconds : %time:~6,2% echo Hundredths of seconds : %time:~9,2% pause
To replace one character with another in a variable, use the following syntax :
REM The line below will replace characters / by -. Then, will display the text in the console. REM This type of replacement is useful, for example to specify the date in the file name, REM as / are not allowed in filenames. echo %variable_name:/=-%
® InformatiWeb.net 2008-2020 - © Lionel Eppe - All rights reserved.
Total or partial reproduction of this site is prohibited and constitutes an infringement punishable by articles L.335-2 and following of the intellectual property Code.