Menu
InformatiWeb
  • Index
  • Courses
  • Tutorials
  • InformatiWeb Pro

Login

Registration Password lost ?
FR
  • IT
    • Articles
    • Backup
    • BIOS
    • Live CD
    • MultiBoot
    • Security
    • Virtualization
    • Web
    • Windows
  • InformatiWeb
  • Tutorials
  • IT
  • Windows
  • How to automate tasks on Windows using batch files
  • Windows
  • 30 May 2013 at 08:58 UTC
  • Lionel

How to automate tasks on Windows using batch files

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.

  1. Available commands
  2. Conditions
  3. Disable display of commands
  4. Change the title of the DOS window
  5. Learn how to create a batch file
  6. Bonus
    1. Ask the user to enter a value
    2. Retrieve a part of a variable
    3. Replace a character with another in a variable

1. Available commands

Among the available commands, you will find basic commands :

  • CD : to move in a folder or a parent folder by using "../"
  • DIR : to list the contents of a folder
  • MKDIR : to create folders
  • RMDIR : to delete them
  • COPY or XCOPY : to copy files
  • REN : to rename files or folders
  • DEL : to delete files
  • ECHO : to display a line of text
  • ECHO. : to display an empty line
  • ATTRIB : to change the attributes of a file
  • and more

You can also use network commands that correspond to the network command line programs available in all versions of Windows :

  • IPCONFIG : to display the TCP/IP configuration of your network cards, empty the DNS cache (with "/flushdns"), ...
  • PING : to quickly test the network connection between 2 computers or servers (if PING is allowed in the firewall of the source and destination PC)
  • TRACERT : to test a network connection and try to find where the packet ends up on the way
  • NSLOOKUP : to obtain the IP address of a server from its domain name
  • NETSTAT : to view active network connections on your computer
  • and more

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.

2. Conditions

To manage some conditions in a batch file, there are keywords : IF, FOR, GOTO, ...

3. Disable display of commands

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.

Batch

@echo off

Explanations :

  • echo off : allows you to disable the display of the following commands (as well as the path of the current folder)
  • @ : allows you to hide the command itself. In other words, don't display the "echo off".

4. Change the title of the DOS window

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 :

Batch

title Program name

5. Learn how to create a batch file

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.

6. Bonus

6.1. Ask the user to enter a value

To ask the user to enter a value, you must use the SET command like this :

Batch

@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

6.2. Retrieve a part of a variable

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 :

Batch

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.

Batch

@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

6.3. Replace a character with another in a variable

To replace one character with another in a variable, use the following syntax :

Batch

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:/=-%

Share this tutorial

Partager
Tweet

To see also

  • Windows 10 - Change the network profile used

    Windows 4/4/2022

    Windows 10 - Change the network profile used

  • Windows 7 / 8 / 8.1 / 10 / 11 - Create a virtual Wifi network

    Windows 8/9/2022

    Windows 7 / 8 / 8.1 / 10 / 11 - Create a virtual Wifi network

  • Windows 8 - Update the Windows Update client (fix error 80072EFE)

    Windows 8/31/2021

    Windows 8 - Update the Windows Update client (fix error 80072EFE)

  • Windows 8 / 8.1 / 10 / 11 - Switching from IDE to AHCI without reinstalling Windows

    Windows 9/21/2021

    Windows 8 / 8.1 / 10 / 11 - Switching from IDE to AHCI without reinstalling Windows

Comments

You must be logged in to post a comment

Share your opinion

Pinned content

  • Useful softwares
  • Our programs
  • Terms and conditions
  • Share your opinion

InformatiWeb Pro

  • Win. Server administration
  • Linux Administration
  • Virtualization

Contact

  • Guest book
  • Technical support
  • Contact

® InformatiWeb.net 2008-2022 - © 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.