Saturday, January 15, 2011

Step-by-Step Tutorial for Writing Advanced Batch Files

Though Dr.Batcher allows everyone to create batch files without having any knowledge in this area, we are asked sometimes about step-by-step lessons on batch files creation. Of course we are thinking of writing such tutorial, but that's long time matter. So here are some links on tutorials created by other people.
Add ImageYou can find a really good lessons on batch files at http://www.allenware.com/icsw/icswidx.htm. The set of lessons start with learning ECHO command and environment variables, then it describes using child shells, continues with lessons on ERRORLEVEL, working with files, debugging batch scripts and so on. Every lesson is coherent and well-illustrated.I should say that I consider these lessons to be useful even for those people who are familiar with batch files. Why? Because sometimes it is useful to refresh your knowledge and find something new in usual techniques

Thursday, January 13, 2011

How to Sort Out Files of Differnet Types with Help of Batch Files

Have you got directories with thousands and thousands of unsorted files? Sure everyone has. I also have folder called 'Downloads' where I place almost everything downloaded from the Web. Usually I clear it once or twice per month and then start to fill it with 'digital garbage' again. But in this case I often lose gems among the garbage, that's this week I decided to sort files in 'Downloads' instead of removing them. I didn't want to lose my time and started Dr.Batcher to create the batch file which had to sort everything without my additional effort. I won't waste your time describing what I tried to do, just give you the code that was created after some experiments.

@ECHO off
REM Creating folders to separate files of different types
IF not EXIST TXT MD TXT
IF not EXIST AVI MD AVI
IF not EXIST MP3 MD MP3
IF not EXIST JPG MD JPG
IF not EXIST RAR MD RAR
IF not EXIST HTM MD HTM
IF not EXIST EXE MD EXE
ECHO Moving text files
MOVE /-Y *.txt TXT
MOVE /-Y *.doc TXT
MOVE /-Y *.rtf TXT
MOVE /-Y *.PDF TXT
ECHO Moving movies
MOVE /-Y *.avi AVI
MOVE /-Y *.mpg AVI
MOVE /-Y *.divx AVI
MOVE /-Y *.xvid AVI
ECHO Moving audio
MOVE /-Y *.mp3 MP3
MOVE /-Y *.wav MP3
MOVE /-Y *.ogg MP3
MOVE /-Y *.wma MP3
ECHO Moving images
MOVE /-Y *.jpg JPG
MOVE /-Y *.bmp JPG
MOVE /-Y *.gif JPG
MOVE /-Y *.png JPG
ECHO Moving archives
MOVE /-Y *.RAR RAR
MOVE /-Y *.ZIP RAR
MOVE /-Y *.gz RAR
MOVE /-Y *.7z RAR
ECHO Moving executables
MOVE /-Y *.exe EXE
ECHO Everything is sorted!
PAUSE

As far as you can see, this script is quite simple though it is long enough. This Flag '/-Y' is added to avoid replacing one file existing in a target folder with a new file with the same name. Hope you'll find this script useful.

Saturday, January 8, 2011

Batch File To Clean Temp Folder

There is a lot of utilities for Windows designed especially for cleaning temporary files in Windows Temp folder and some other places where they usually can be found. Some of these programs are freeware, some of them are paid. With help of small batch script instead of one of the such utilities you can save your money and some space on HDD. Also this batch script gives you great flexibility: you will be able to add new folders to be cleaned if you need.
The script below cleans folder named 'C:\Temp' and makes it default temp folder for those applications who use the folder given in TEMP variable:
@ECHO Cleaning temporary folder...
DEL /S /Q /F C:\TEMP > NUL
SET TMP=C:\TEMP
SET TEMP=C:\TEMP
SET PKTMP=C:\TEMP
@ECHO Temporary folder is cleaned
If you want to clean also Windows default temp folder, you should also add line with 'DEL' for 'c:\Documents and Settings\Your User Name\Local Settings\Temp' (for Windows XP). Of course, the simplest way to modify this batch script is using Dr.Batcher.

Wednesday, January 5, 2011

Funny But Common Mistake

Once Dr.Batcher's support team received a letter describing a problem with running batch file after re-installing Windows. The batch file was called 'shutdown.bat', and its purpose was to turn off computer in single mouse click. It contained the following line of code:
shutdown -s -t 0
Do you know what was wrong? Of course, the renaming file to 'shutdown1.bat' completely solved the problem. This mistake is quite silly and funny, but I consider it to be rather common, that's why I decided to pay your attention on it. When a batch file doesn't work at all, though its code seems to be correct, first of all check its name: probably, it's the cause of its wrong behavior.

Monday, January 3, 2011

Five Really Useful Sites On Batch Files

For those who often deal with batch files we are glad to advice some useful sites with different tips, tricks and solutions for batch files. Hope you'll find some of them useful if you use to read this blog.

  1. Rob van der Woude's Scripting Pages (http://www.robvanderwoude.com/battech.php). The most useful site not only for those who use batch files, but also for those who use newer scripting techniques. You can find tips, tricks, ready-to-use batch files and list of useful software for batch scripting there.
  2. Batch File Help on ComputerHope (http://www.computerhope.com/batch.htm). Really useful page for beginners. Of course, Dr.Batcher lets you start to create batch files without studying their syntax, but you'd better study this site before writing batch files in Dr.Batcher.
  3. DOS Batch Programming (http://www.ericphelps.com/batch). This site is useful especially for those who create or edit batch files for old computers running MS-DOS. But it's still useful for people who create batch scripts for Windows.
  4. Commandline.co.uk (http://www.commandline.co.uk). Additional tools are sometimes necessary to create powerful batch file. You can find plenty of useful command-line utilities for Windows that can be used in batch files.
  5. The Windows Command Line, Batch Files, and Scripting- Using the Command Shell(DOS Prompt) (http://commandwindows.com). This site contains lots of useful information not only about batch files, but also about different other aspects of using Windows command line.

Thursday, December 30, 2010

Batch File for IP Change


There is a plenty of users that want to change the IP address of a computer with help of batch files. There is a simple way to perform it, all you need is standard netsh command and some imagination to 'invent' a new IP address.
The following line of code will help you to set the IP address and the subnet mask:
netsh int ip set address name = "LAN" source = static addr = 127.0.0.1 mask = 255.255.255.205
Of course, you have to use real IP address instead of samples given above (127.0.0.1 and 255.255.255.205).
Also we advice you to learn netsh command because it's really useful for different networking operations.

Friday, December 24, 2010

Batch File to Delete Itself

Sometimes users of Dr.Batcher ask us how to create a batch script that is able to delete itself. It is not hard, and the example of self-destructive script is placed in Dr.Batcher's installation package.
The trick is to call command-line processor (previously known as Command.com) with instruction to delete this batch file. While the processor is working, the source batch file will be already finished. Thus the processor will be able to delete it.
This is 'theory'. Practically you are to write the following batch code. Caution: do not run this file in folders with any valueable content, it may cause its loss or destruction.
REM Self-Deleting Batch Script
REM This script will delete all files in current folder, including itself

REM Don't forget to make backup copy of all files in current forder before its execution
ECHO This script will delete everything in current folder

ECHO Terminate it to cancel the action

PAUSE
@ECHO OFF

CD ..
START CMD /C RMDIR /S /Q "%~dp0"
The logic of whole the script is situated in the last construction. It starts the command-line processor and tells it to remove the whole directory where this batch script is placed. Of course, you can write additional batch file with this command instead. Also you can use 'DEL' instead of 'RMDIR' if you want to delete only batch file itself.

Translate