Batch Files – Repeat certain tasks with a click

dos_command_prompt_batch_file

Batch file is a file which holds a list of commands or instructions that are executed once the batch file is executed. Basically the commands are DOS commands that are executed as if they were executed one by one on the command prompt. Batch files are of great help when you need to repeat a certain task many times. You can just specify what is to be done and it will be done when that batch file is executed.

Before moving further, you must be familiar with DOS because in this post I am going to explain only about batch files. To learn more about DOS commands, check out this link : Ms DOS help and commands

What it can do?

The limiting factor is you and the DOS itself i.e. What we want to do and can it be done through DOS. Not every program supports command line version (it can’t be executed in the command prompt).

Example #1 (Open Multiple Applications)

You need to open up different set of applications depending on what you intend to do. Instead of clicking their icons one by one, you can save time by creating a batch file. For example: If I am in a mood of browsing internet while listening to songs, I can create a batch file with the following content:

start /max C:\Progra~1\Mozill~1\firefox.exe
start /min C:\Progra~1\Google\Google~1\googletalk.exe
start /min C:\Progra~1\Window~2\wmplayer.exe

Start command is used to start window applications. For more info about this command, type start /? at the command prompt. Here, I have assumed that all the programs are installed in their default folder. If you have installed them somewhere else, then make changes accordingly.

Example #2 (Copy files)

You need to copy all the files or certain files whenever you insert a CD/DVD/Pen drive, then you can create a batch file with the following content:

xcopy h:\*.* d:\docs\ /s

It is assumed that h:\ is your source drive (CD/DVD drive or pen drive) and you want to copy all the files to the docs folder residing on D drive.

xcopy h:\*.mp3 d:\songs\ /s

It is assumed that h:\ is your source drive (CD/DVD drive or pen drive) and you want to copy all the mp3s to the songs folder residing on D drive. Again, type xcopy /? at the command prompt for more about this command.

Example #3 (Delete Junk Files)

Following file types are considered as junk files : *.~* ~*.* *.??~ *.— *.tmp *._mp *.old *.bak *.syd *.chk *.gid mscreate.dir *.dmp 0???????.nch

You can make a batch file that scans for them and deletes them with the following content:

del c:\*.~* /s /q
del c:\~*.* /s /q
del c:\*.??~ /s /q
del c:\*.— /s /q
del c:\*.tmp /s /q
del c:\*._mp /s /q
del c:\*.old /s /q
del c:\*.bak /s /q
del c:\*.syd /s /q
del c:\*.chk /s /q
del c:\*.gid /s /q
del c:\mscreate.dir /s /q
del c:\*.dmp /s /q
del c:\0???????.nch /s /q
del c:\windows\temp\*.* /q
del c:\temp\*.* /q
del d:\*.~* /s /q
del d:\~*.* /s /q
del d:\*.??~ /s /q
del d:\*.— /s /q
del d:\*.tmp /s /q
del d:\*._mp /s /q
del d:\*.old /s /q
del d:\*.bak /s /q
del d:\*.syd /s /q
del d:\*.chk /s /q
del d:\*.gid /s /q
del d:\mscreate.dir /s /q
del d:\*.dmp /s /q
del d:\0???????.nch /s /q

Add commands for other partitions in a similar fashion. Again, type del /? at the command prompt for more about this command.

Example #4 (Defagment and Shutdown the computer)

You can make a batch file that defragments the drive you specify and then shutdown the computer. Here is what you need to have it :

defrag c:
defrag d:
shutdown /s /t 10 /c “Defragmentation Complete !”

Add commands for other drives too. Shutdown command shutdown the computer. Again, type shutdown /? at the command prompt for more about this command.

To know more about any command, type command name followed by /? at the command prompt.

For more info regarding batch files, check this link : Batch File Help

You can convert a batch file (*.bat) to a executable file (*.exe) online by this link: Online *.bat to *.exe

Batch File work around the blogosphere:

How to Lock Files and Folders without any Software

Safely Remove Virus from your USB/Thumb/Pen Drive

There is still more left. You can create batch files that can delay, check conditions and do a lot more. Give them a try and you will end up using them. If you face any trouble in implementing a particular thing, feel free to get in touch with me. I will try my best to help you out.


Comments

13 responses to “Batch Files – Repeat certain tasks with a click”

  1. Wow man, what a nice collection of to-do things with batch files. Excellent one.
    Stumbled!!!

    Also, thanks for the mention.
    πŸ™‚

  2. @Rakshit
    You are welcome. Call it a co-incidence or anything. I was writing about batch files and just then I came to know about this post of yours.

  3. Good one. I’d read about using batch files, this should help. Time to read up the links too πŸ˜€

  4. @Sumesh
    You will enjoy the links and will find it pretty easy too. 😎
    Do let me know if you have created any exclusive batch file of yours.

  5. […] help on batch files and link to online *.bat to *.exe convertor, following post is recommended: Creating Batch Files Related PostsHow to safely remove your USB drive when Safely Remove Hardware icon disappears from […]

  6. […] Batch Files – Repeat certain tasks with a click […]

  7. dhandeep Avatar
    dhandeep

    hi
    i want to kno if we can make a dialog box
    sayin anythin & havin ok button with it
    usin batch
    thankx

  8. @Dhandeep
    Sorry mate. I don’t know if even that can be done. Do share if you got to know.

  9. This site proved very useful for me the most I can say about this site is that this is really amazing ………….

    Thank you.

  10. @A. Kr. Thakran
    You are welcome!

  11. could create a batch which directly scanning , detecting and deleting files.
    Scan, detect then delete the file: batch files.
    Thanks

    1. @Alphonse
      Can you explain a bit more what you want?
      A command like

      del c:\*.dmp /s /q

      will actually search for all files with extension dmp including the sub-directories because of the switch /s. Switch /q just puts in quiet mode.

  12. […] This occurs because the temporary file created for printing the job can’t be deleted because it is being continuously accessed by windows. One solution is to kill the spoolsv.exe service using Task Manager and delete all jobs present in C:WindowsSystem32spoolPRINTERS and then either restart the spoolsv.exe service or restart the computer so that the service is restarted by itself. But we can do all these steps by automating the work with batch files. […]