logo

Tuesday, June 19, 2007

Batch Files - Get the job done with a click

by Sushma Verma 2 comments


UPDATE: Improved version of this article is here :

Batch Files - Repeat certain tasks with a click



How many times we have to repeat a sequence of steps in our daily tasks? Well! The answer can vary widely for different users? Batch Files help me a lot in my work that I tend to repeat often.
The limit of things that they can do for you limits on you! Before giving any explanations and examples, I would like to define it.

What is a Batch File?
Batch files are files that allow users to create a lists of commands and/or programs to run once the batch file has been executed. A simple batch file does not require any special programming skills and can be done by users who only know DOS commands.

What it can do for us?
Ex#1 You need to run multiple programs at a single click. Like you started your internet connection and now want your browser, IM client, P2P client etc to start up. You can write commands of calling them one by one and save them in a batch file. And now when you execute this batch file, it will run all the commands of calling different applications to start one by one.
After executing one command i.e. calling of a application it waits for it to terminate before it can execute the next command. I am looking for a solution for this and hopefully will be back with a solution soon.
Use start command for launching applications!
Ex#2 You got a CD mixed with content and want specific files to be copied in specific folders. You can create a batch file containing the command and when it is executed, your specified files will be copied to the targeted folder.
Its upto you, what work you want to get done!
Lets Start with making Batch files!
Open Notepad or create a new txt file (Right Click > New > Text Document)
Type in the DOS commands (Explained further below)
Now File Menu > Save As... and save it with any name with *.bat extension (make sure Save as type is selected as All Files)

Help on DOS Commands
For a list of DOS commands, check this link!
Keywords followed after a command are called switches.
Remember! To need help with a particular command, type the command followed by /? the command prompt.
For Ex: dir /? will list all the switches that you can use with it.
For advanced options on creating batch files, check this link!

Some Examples
I would only explain the commands here!

Ex#1 Clean your PC from junk files
We will use the del command, type del /? to know what switches you can use with these commands.
We need to use the /s switch so that it can also delete junk files located in sub directories and the /q switch to be in quiet mode.
Note: You can use other switches as per scenario.
A command will be like this : del c:\*.tmp /s /q
Now what you need to do is:
Repeat the command in next line by changing the file extension *.dmp or *.chk or whatever you want.
Repeat all the above commands by changing the drive letter to d:\ or e:\ (depends on number of partitions on your hard disk)
Here is a list of files that are junk:
(I got the list from older version of System mechanic)
*.~*
~*.*
*.??~
*.---
*.tmp
*._mp
*.old
*.bak
*.syd
*.chk
*.gid
mscreate.dir
*.dmp
0???????.nch
You will have the batch file contents like following:
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
The text highlighted empties the directory irrespective of the content.

Ex#2 Defragment your partitions and shut down the computer
(This command works on XP - defrag)
Use these
defrag c:
defrag d:
(Add commands according to the number of partitions)
shutdown /s /t 10 /c "Defragmentation Complete !!"

Use shutdown /? to know what does the switches do!
(Left as an exercise for you)

If you need any kind of help, feel free to ask!

Update: Convert your batch (*.bat) files into executables (*.exe) by this tutorial!
(This tutorial was able to create *.exe file out of *.bat files but it doesn't work for me well but it does for others. So, Verify yourself!)
With the *.exe file created now you can pin the file in start menu for easy reach!

Comments 2 comments
Anonymous said...

Ex#2 is interesting, thanks for the tip.

Apart from 'automating' to a certain extent, the generally poor performance of the XP defragger, does it improve frag times or help achieve better defragmentation? I don't really like the built-in xp utility because of its drawbacks.

I recently started using Diskeeper Pro because I was fed up by XP's inability to defragment my 2 drives properly. Diskeeper defrags in a *fraction of the time* that XP used to take, and I can leave it on the automatic mode where it defrags continuously in the background so don't even have to worry much about scheduling a manual defrag (save for a once-in-a-while MFT and system files defrag). So its a huge leap over the XP defrag console in terms of user friendliness as well as overall performance.

I will be switching to Vista Business early next month, and I have heard horror stories of the defragger in Vista too....apparently, zero improvement over the XP one, and in some ways, a step backword. Clearly Microsoft doesn't care about providing a decent built-in defrag program for Vista either. But from all reports on various forums,Diskeeper performs as excellently on vista as it has done on xp, so I expect I am in good shape.

Ashfame said...

Karthik,
Welcome here!
Yeah I too hate XP built in defragger. Even I use Diskeeper Pro.
In this case my batch file contents are
diskeeper c:
diskeeper d:
diskeeper e:
diskeeper f:
shutdown /f /s /t 5 /c "Defrag completed. System Shutting Down"

Actually I use automated feature only on Drive C b'coz on other drives I usually copy videos which I dont want to defragment until I have ripped them. So I have to manually defrag them and for that I use the batch file.
Thanx for your comment!

Ashfame
I am a tech freak who loves to share.
Homepage : Ashfame.com
You can mail me at ashishsainiashfam[at]gmail[dot]com

Subscribe feeds via e-mail
feed

Subscribe feeds rss From Other Blog

Advertise on this site Sponsored links

Categories

Sponsored Links