Problem solve Get help with specific problems with your technologies, process and projects.

Why batch files are still useful

Reusing old batch file commands saves time and gets the job done just as well as scripting.

Please let us know how useful you find this tip by rating it below. Do you have a useful Windows tip, timesaver...

or workaround to share? Submit it to our monthly tip contest and you could win a prize! And don't forget to go in and rate other tips users have submitted this month: Easy Mail Server changes Add or modify the registry via group policy Use Windows XP sysprep to create images Speed up remote access to network resources Use HP's JetDirect printer server to count bad packets Protect Win XP files during power outage

Scripting and coding is fine for those who wish to spend their day learning all the ins and outs of the programming language. But for me, reusing some old batch file commands has saved me time and gets the job done just as well (without having to teach this "old dog" any new tricks). If you are like me and know how to create batch files, then this tip will be a basic review for you. If you are new, then I hope you will find this tip useful.

Typically these batch file commands are universal between Windows versions, but you may find some that are not. However, there is typically a substitute for the version you wish to use.

To create a batch file, you need only open Notepad.exe, type in the batch syntax, then save the file as a .bat extension (batchfile.bat).

Batch files are great for logon scripts, for automating PC environment configurations and for simply making IT life easier.

If you ever want to know more about a batch file command, typically you just have to type /? after the command to get a list of options.

I have attached examples of batch files for you to review and use below. Some of these include:
-Create, copy and delete files/folders
-Create ODBCs
-Create mapped drives and install network printers
-Condition checking
-Synchronizing time

Example of Windows Batch File Commands:

REM #====================================
REM # Description: Creating comment Lines REM # These are used to explain what your REM # batch files are doing 
REM #====================================

REM - Type REM in front of every line you wish REM - the batch file to ignore.
Basically REM - they are used for comment lines, like these: REM #==================================== REM # REM # Description: @ECHO states whether to REM # have the batch file present the text REM # it is running on the screen - On or Off REM # REM #==================================== @ECHO off @ECHO on REM #========================================= REM # REM # Create directories - The example shows how to REM # create a folder on the c: drive REM # REM #============================================ c: cd md C:folder1 md c:folder1folder2 REM #============================================ REM # REM # Delete, copy and set attributes for files REM # The quotes are needed if the REM # path to the file is broken, such as REM # c:documents and settingsfile1.txt REM # Here I delete, copy, remove Read only and set REM # the hidden attributes on some files REM # REM #============================================ del "C:file.txt" copy c:file2.txt c:folder1folder2 /Y attrib -r c:folder1folder2file2.txt attrib +h c:folder1folder2file2.txt REM #==================================== REM # REM # Description: The Net command can do a lot REM # and one is synchronizing the time between REM # two systems. Running the following will REM # synchronize the time on the system with REM # a system called server1. Also the second REM # example synchs the PC1's time to an NTP REM # server's time REM # REM #=================================== net time server1 /set /y NET TIME PC1 /SETNTP:ntpservername REM #=================================== REM # REM # Description: Mapping Network Drives REM # allows a system to set a drive letter REM # to connect to a remote network share REM # So P: will map to server1's shared REM # folder called share1. Also, I am REM # deleting a H: mapped drive REM # REM #=================================== net use p: server1share1 /persistant:yes net use h: /delete REM #===================================== REM # REM # Conditional statements allow you to REM # set conditions for the batch file to REM # meet before processing the rest of the REM # batch. Such as checking for the existence REM # of a file and process the batch file REM # accordingly. REM # REM #===================================== if exist c:file.txt (goto 2) ELSE goto 1 :1 @ECHO on ECHO The file IS NOT there @echo off goto end :2 @ECHO on ECHO The file IS there @echo off goto end :END REM #======================================= REM # REM # Description: Installs/removes network printers REM # If the printer driver is installed on the REM # system running this batch file, it will connect REM # to the shared printer (also, the server maybe REM # configured to download the printer driver for REM # clients requesting it) REM # REM #========================================= REM # Used to delete network printers rundll32 printui.dll,PrintUIEntry /q /dn /n server1sharedprinter1 REM # Used to create network printers rundll32 printui.dll,PrintUIEntry /in /c server1 /n server1sharedprinter2 REM #========================================== REM # REM # Create ODBCs for connecting to backend databases REM # The example shows a connection to a SQL server REM # using a Windows NT login REM # REM #=========================================== odbcconf /a {configsysdsn "SQL Server" "DSN=ODBCname|Database=DBname|SERVER=server1instance1|Trusted_Connection=yes"}

For more examples, use your favorite search engine and look for Windows Batch File Commands. You'll turn up sites like these:
10 most used batch commands
An A-Z Index of the Windows NT/XP command line.

This was last published in July 2005

Dig Deeper on Microsoft Active Directory



Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

This was a great post, Also I never knew about the server time sync? This is ideal for me, As soon ill be working with ms server2012 and thought firstly Ill have to get to grips with command prompt for diagnostics and IP checks