Thursday, January 27, 2011

How to Export Registry Keys Through Batch File

Long time ago I wrote a post on having a deal with Windows registry in batch files. It's time to return to this topic and share a small, but still very useful batch file that will help you to export registry keys to a REG file. This code is really simple, still you can include it into complex script that you create with help of our powerful batch files editor.
Here is the code of this batch file:
@echo off regedit /ea %Temp%\ChkReg.txt "HKEY_CURRENT_USER\Software\MyPrg-1" if exist %Temp%\ChkReg.txt type %Temp%\ChkReg.txt>reg.reg regedit /ea %Temp%\ChkReg.txt "HKEY_LOCAL_MACHINE\Software\MyPrg-1" if exist %Temp%\ChkReg.txt findstr /v /b /i "REGEDIT4" %Temp%\ChkReg.txt>>reg.reg if exist %Temp%\ChkReg.txt del %Temp%\ChkReg.txt
As far as you can see, first of all we export the necessary keys to ChkReg.txt, than copying data itself to REG file, and then remove the temporary TXT. Hope this script will be useful for you.

Tuesday, January 25, 2011

Batch File to Retrieve Batch Files from Network Drive and Pack Them


Here is a useful script that administrators can use to retrieve automatically files from the network drive and pack them immediately after that. All the actions are being logged to the LOG file called by default 1.log. You can easily modify this script with help of Dr.Batcher, a powerful utility designed to create and modify batch files.
@echo Starting>> 1.log
@Echo off
@DATE /t >> 1.log
@TIME /T >> 1.log
@echo Copying and logging results ...
@xcopy v:\post\*.doc /z /c /Q >> 1.log
@echo Packing... >> 1.log
@rar a -ag + YYYY:MM:DD:HH:MM \*.doc >> 1.log
@echo Moving the archive to TEMP folder... >> 1.log
@move *.rar TEMP
@echo Removing DOC... >> 1.log
@del /F d:\*.doc
@echo Done... >> 1.log
Have any suggestions on this batch file? Leave your comments!

Saturday, January 22, 2011

Batch File To Restart Explorer And Reopen Windows

Though this is not a very common task, I suppose this example of batch scripting is still very useful for advanced users of batch files. You can change anything you want in a couple of minutes, especially with help of our powerful and handy batch files editor.
Here is the code to restart Windows Explorer and reopen all windows of the running applications:
@echo off setlocal enabledelayedexpansion for /f "usebackq tokens=8*" %%a in (`"cmdow /t /f | find /i "explorer""`) do ( if /i "%%a"=="explorer" set "open_windows=!open_windows! "%%b"" ) taskkill /f /im explorer.exe>nul start explorer for %%a in (%open_windows%) do ( if /i %%a=="My Computer" (start /min explorer shell:DriveFolder) else ( start /min explorer %%a) )
Hope you'll find this code useful.

Wednesday, January 19, 2011

Detecting the Insertion of USB Flash in Batch Files

How to detect that user inserts USB Flash drive and make computer shut down after this? Here you can see step-by-step explanation.
1. Download and install Dr.Batcher, the program designed to create and edit batch files.
2. Start Dr.Batcher and create new empty batch script (File->New->Empty Batch Script).
3. Add command named "Display a message" (Command->Add), click OK in the "Add Command" dialog.
4. Choose "off" as the first parameter and click "OK".
5. Add command named "Set Label".
6. Set loop as a label name, click "OK".
7. Add command named "If (Condition)". Choose "Edit the condition" in the appeared dialog.
8. Choose "File Exists (Condition Only)". 9. Set the letter of the flash drive as the parameter in the following dialog. Note: "\." after the drive letter should not be removed.
9. Add command named "Go to a Label".
10. Set loop as the label name. Click "OK".
11. Following the instructions in paragraphs 5 and 6, add a label with name end.
12. Add command named "Shut Down or Restart Computer".
13. Set the first parameter of this command equal to -r to restart or -s to shut down
14. To make this file start automatically when you log on, place it inside the "Startup" folder in Start menu.
15. Enjoy!

Monday, January 17, 2011

What Does Bring Dr.Batcher 2.1.3?

We are glad to introduce Dr.Batcher 2.1.3. This version fixes a lot of issues reported by the users of 32-bit version of Windows 7, in the following version we are going to fix issues with running Dr.Batcher under 64-bit Winfows 7. What else will you find in this version? Here is the complete list of new features:
  • new options for changing the way of starting Dr.Batcher;
  • support for storing spaces between command name and its parameters;
  • tabbed "Common Options" dialog;
  • improved compatibility with 32-bit Windows 7;
  • small bugfixes.
Here are some screenshots of the new Common Options dialog:




Hope you'll enjoy our work. Please remember that you are always welcome with your reports and suggestions.

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.

Translate