Thursday, January 10, 2013

What is errorlevel?



 Sooner or later every batch files creator faces the problem of checking the result of one command execution before executing the other one. For that we have built-in variable “errorlevel” that changes its value after each command execution. Its value can vary for different commands used in batch files. Usually successful execution of the command is displayed as zero value of variable, but not always other value implies any errors. So if you create batch file, you should remember about this meaning of errorlevel.
This variable is used as follows:
if errorlevel 0 echo Errorlevel is 0 or greater
Instead of “echo Errorlevel is 0 or greater” you can put any necessary command. After “if” you can add “not” (no quotation marks for this time again) – then the value becomes diametrically opposed. It should be mentioned that “errorlevel N”-construction does not mean precise equality of variable “errorlevel” to value N. It means that the command will be executed only in case “errorlevel” value is or is greater than N. Thus it is more convenient to check errors with the help of “if errorlevel” –construction than to execute the command successfully. But if you write “if not errorlevel 1”, that means in case “errorlevel” is nonnegative, we obtain the result of “errorlevel” equal to zero. Unfortunately nonnegative values of this variable are not guaranteed. For more details you can read about “if errorlevel” –construction in Windows help manual.

Saturday, January 5, 2013

Why Is There No Percentage Mark in the Line Seen on the Screen?



In batch files percentage mark is used to indicate variables, or variable environments, or internal variables of the BAT-file. So, if you’d like to see percentage mark on the screen, you just need to use it twice: for example, “echo This day performed 0.25%% of the whole project”. By the way, is not the only symbol that has a special meaning in BAT files. Such symbols also include “>”, “<”, etc. They can be displayed on the screen with the help of combinations “^<”, “^>” (i.e. you put a tick before the symbol).

Wednesday, December 19, 2012

How to Convert Batch File to EXE

Despite the fact batch files are quite useful for different purposes, it is better sometimes to convert them into executable ones. Today we will learn how to convert batch files to EXE. The easiest and fastest way to convert it is utility Dr.Batcher. It offers a special wizard that lets you convert batch file to EXE in several clicks.
1. Download Dr.Batcher and install it on your computer
2. Start Dr.Batcher and open the batch file you want to convert.
3. Choose ‘Tools’ -> ‘Compile EXE File…’. Follow the wizard instructions.
4. Enter the output file name, choose application type and security options. Check ‘Console’ to create application that will show console output during the batch file execution, or choose ‘Invisible’ to hide the console output. Check ‘Protect batch file and additional files to pervert reverse engineering’ to encrypt the output file. Click ‘Next’.
5. Choose the additional files your script needs while execution. It might be some external applications (for example, zip for backup scripts), other scripts or any other file. Click ‘Next’.
6. Click ‘Next’ to create the executable file.
7. Click ‘Run the created file’ to test the converted EXE or ‘Finish’ to exit the wizard.

Friday, September 7, 2012

New Version of Dr.Batcher Offers Backup Script Wizard

The new version of Dr.Batcher, the award-winning tool for creating and editing batch files, comes with support for creating full-featured backup and restore scripts in a few clicks.
Version 2.2 of Dr.Batcher, a powerful tool for batch files creation and editing designed both for advanced users and newbies, offers brand new Backup Script Wizard. Now one can create compressed (ZIP, CAB archive formats supported) or uncompressed backups with the help of batch scripts, and restore them. All compressed backups can be protected with passwords, also the level of compression is changeable.
It should be noticed that the new version of Dr.Batcher comes with multi-document tabbed user interface. Now one can easily move from one batch file to another during programming them and make changes in many files without opening and closing each of them. Dr.Batcher can automatically load all recently opened files, so user doesn’t have to remember all of them.
Among other changes, in Dr.Batcher 2.2 one can meet support for automatic adding the copyright message on the top of the script, support for auto-saving scripts after the specified period of time, updated settings.
Dr.Batcher still provides users both with professional script editor and simple editor for those who don’t know batch files syntax well. The application comes with support for dozens of most commonly used DOS and Windows commands, also users can extend the list by writing their own XMLs with commands descriptions. While editing a batch file, user can switch between visual editor (Simple mode) and text editor (Professional mode) any time he wants.

Saturday, September 17, 2011

Useful Script for Network Backup

Here is an interesting backup script from one of our customers. It requires an external tool to be run, but allows to perform backup from many computers over the network:
@echo off
@set dircmd=/b
set folder=Documents and Settings
for /F "tokens=1 usebackq delims==\ " %%i IN (`net view`) DO (
echo Start copying %%i...
start /wait /min /high robocopy.exe "\\%%i\C$" X:\backup\uim\%%i\C *.doc *.xls /S /R:1 /W:120 /XD RECYCLER batmail "Application Data" game* "Default User" "ÿáí½«¡δ" "Local Settings" windows winxp wxp w2k winnt "Program Files" Temp NetHood Recycled Install "The Bat!" "System Volume Information" readme /XF "~*.*" readme /XJ /XO
start /wait /min /high robocopy.exe "\\%%i\D$" X:\backup\uim\%%i\D *.doc *.xls /S /R:0 /W:1 /XD RECYCLER SCANJET batmail "Application Data" game* "Default User" "ÿáí½«¡δ" "Local Settings" Temp Recycled Install readme/XF "~*.*" "System Volume Information" readme /XJ /XO

Wednesday, September 14, 2011

Creating a New User Account via Batch Files

Q: I need to write a batch file that creates a new user account. The account's name and password should be passed as parameters like batch_file name pass. What should I do?

A:
Start Dr.Batcher and type the following script:

@echo off
if "%~2"=="" goto error
if "%~1"=="" goto error
net user %~1 %~2 /add
goto :EOF
:error
echo Usage: %~0 user password

That's all!

Friday, September 9, 2011

Removing Unnecessary Trailing Spaces

Imagine: we have a variable with some string data, and we have to remove unnecessary spaces from the end of this variable. The code below shows how to solve this problem:

@echo off
setlocal
set "a=111 222 "

:loop
set /a n+=1
for /f "tokens=%n%" %%i in ("%a%") do (
if not "%%i"=="" set b=%b%%%i && goto:loop
)

set a=%b:~0,-1%
set b=
echo "%a%"


Of course, you have to change this code a bit before using it. You can do it with the help of our award-winning batch files editor. Hope you'll find this example useful.

Translate