Batch File Examples
Password Example
The following batch file can be used to establish a password for
running a program. The batch file is named START.BAT and calls the
program named CALC.EXE.
@ECHO OFF
IF %1==ExeScript GOTO OK
ECHO WRONG PASSWORD
GOTO END
:OK
ECHO PASSWORD IS ACCEPTED...STARTING
CALC.EXE
:END
Below you'll see the response of the computer to various
commands.
First the bad password. At the prompt type START 123.
C:/>
C:/>START 123
C:/>BAD PASSWORD
Now use the correct password. Type the correct command at the
prompt.
C:/>
C:/>START ExeScript
C:/>PASSWORD IS ACCEPTED...STARTING
At this point the CALC.EXE program starts.
Backup your .doc files (Windows NT/2000/XP version)
usage: backbat backupdir
where: backupdir is the directory to copy your .doc files
@echo off
if not "%1"=="" goto argsok
echo usage: %0 backupdir
echo where: backupdir is the directory to copy your .doc files
goto end
:argsok
setlocal
set backupdir=%1
if not exist %backupdir% goto notfile
echo %backupdir% is a file
goto end
:notfile
rem If the directory does not exist, create it.
if exist %backupdir%\nul goto skipdir
md %backupdir%
if "%errorlevel%"=="0" goto skipdir
echo Error creating backup directory
goto end
:skipdir
rem Copy each .doc file one at a time.
rem Note: the for loop variable (%%b) must be contain only one letter.
for %%b in ( *.doc ) do copy %%b %backupdir% > nul
rem Use the for loop again to check if each file was copied (since it is
rem difficult to run multiple commands in a for loop).
for %%b in ( *.doc ) do if not exist %backupdir%\%%b echo %%b was not copied
:end
rem Clean up
endlocal
How to make a time log
In the following example, you can create a time log of when the
batch file is loaded or, for example, in the autoexec.bat file when
someone logs into a computer.
ECHO. |TIME > TIME
COPY LOG +TIME
An alternate slightly more complicated method that, to our
knowledge, cannot be used in Windows NT, Windows 2000 or Windows ME
would be the following:
echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> log
Another alternative is:
echo. |time |find "current" >> log
For the above batch file to work properly, you must create a
file called log by typing "edit log" (without the quotes) and then
save and exit the file which will create the file at 0 bytes. If
this file is not created or not created properly, you will receive
the error message, "Content of destination lost before copy".
See also:
Tips for Writing Batch
Files
Copyright © ScriptCode.com 2010. All rights reserved.