![]() |
Sample Win9x Batch Files |
![]() |
|
Insure QBASIC exists on a user's machine
Not just test for it, but download and install it automatically too!
Just stick this code in front of any batch file that needs QBASIC.
Save the current directory
Done without using the environment. Put this "pushpath.bat" file in
your path. It will create the matching "poppath.bat". You run pusshpath
first, then do something that changes your current directory, then run
poppath to reset the directory to where you started.
Is there a disk in the A: drive?
Using the /f option of command.com allows you to find out without crashing
your batch program.
How to format a disk automatically
Just about the worst thing you would want to automate, but it's your
life.
Run a command in every subdirectory
Automatically change to each subdirectory on your drive (or part of
it) and run whatever command you want.
Is a COM port available
Check to see if a com port is available before proceeding.
When was a file last accessed?
Use the DEBUG command to trim the output of the DIR /V command to get
just the access date.
Get the root name of a file
Only works on short names, but if you ever want to dump the file extension,
here's a way.
Find Disk Size
Use the DEBUG command to trim the output of the CHKDSK command to get
disk size.
Find Disk free space
Use the DEBUG command to trim the output of the DIR command to get
the free space.
Hide your batch file code
What if you have to embed a password or something else you don't want
seen in your code?
When was a file created?
Use the DEBUG command to trim the output of the DIR command to get
the file created date.
Make a beep
You can misuse the CHOICE command or spin up a quick QBASIC program.
No ANSI needed.
Make a time delay
Put your computer to sleep for a while. At least put the particular
DOS window you are in to sleep.
Incrementing
It isn't real math, but batch files can add one to a number to help
you set up loops or generate sequential file names or whatever you want.
Find all FTP servers
Actually just the FTP servers on any small subnet. You build a loop
that counts from 1 to 254 and let it script the FTP program. Three batch
programs zipped up. You configure and run hunt.bat, it calls the other
two.
Generate a random number
Grab the hundredths digit from the time.
Get user input from batch file
Sure, you've done it, but can you do it without having to hit a Ctrl-Z,
without ANSI, without a debug script, and without a separate "set" file?
Get user input from an NT batch file
All the old tricks fail. Luckily, QBASIC can be used and is standard
equipment in NT. Windows Scripting works better if you have it.
Put the time into the environment
This batch file shows the trick of using a command output as a command
line. In this case the "Current Time..." output of the TIME command is
used to call a batch file named "CURRENT.BAT".
Read the output of DOS commands
Use FIND.EXE and CHOICE.COM (if needed) to make decisions based on
the output of any DOS command. This isn't the discovery of the year, but
it can be a very handy trick to know! How would you use this? Tell if ANSI
is in memory. Tell if Windows is running. And all automatically!
Read and write to INI files
A "pure" batch file solution requiring nothing more than standard DOS.
It won't work with complex INI files like WIN.INI, but is instead intended
for simple one-section private INI data files. Store all your stuff in
an INI file instead of in the environment!
Get the user name
Shows how to grab the current Windows or network user name out of the
system registry.
Get the user name
Parse the output of the NET command and use the first word as the name
of a batch file.
Get the user name
Parse the output of the NET command, but use DEBUG instead of relying
on the first word.
Get the computer name
Parse the output of the NET command and use DEBUG to strip the leading
// from the result.
Copy the most recent file
When a process automatically generates files, it can really help to
have a single file name for the most recently generated file.
Track when anybody logs in
How to get a program to run when Windows starts or a new user logs
in.
How to shut down a Windows program
This is actually a generic "How to control another app" question. It
isn't DOS, but it can be done.
Time delay and For-Next loops
Demonstrates two things: using CHOICE to effect a one-second time delay,
and using environment variables to track nested "for-next" loops
in batch files (yes, you can!) to multiply the one second up to sixty seconds
(actually sixty seconds plus batch processing time). And what did I decide
this file should do with it's one-minute delay? Why, display the time,
of course!
Subroutines in batch files.
Structured programming is here at last! The batch file simply CALLs
itself and passes the name of a label it wants to GOTO. Each label contains
the standalone "subroutine" code and exits when finished, returning control
to the calling incidence. Sound confusing? Just take a look at the simple
code and run it.
FTP scripting
The FTP program can be scripted. Assuming you know how to type FTP
commands, this tells you that little bit more you need to generate a script
-- or turn your script into a batch file. Now you can set up FTP script
batch files to automate your downloads.
Get file sizes in HEX
How to use DEBUG to retrieve the lower 16 bits (64k) of a file's size.
Although it isn't done, I show how to get the upper 16 bits as well.
Long and short file names
Shown are several tricks for converting back-and-forth from long to
short names, getting the drive letter, and getting the file extension.
This isn't made for reading. Just save it as a BAT file and drop a file
on it with Explorer. If it gives you what you want, steal the appropriate
code section.
Print multiple files or multiple copies
Even though it may be a Windows program that does the printing, you
can automate the process with a batch file.
How to make a DOS window close
after your batch program finishes. It only takes two lines of code!
Running a batch file after Windows shuts
down.
It's a great time to run last-minute tests or delete temporary files.
Want to know how to exit windows and shut down
your computer
from a batch file? Rundll.exe can do the impossible.
Run a specified program only on certain days
at boot time.
This demo makes use of two tricks: It reads a custom INI file, and
it puts the current day into the environment.
How to get around the "Do you really want to do
this?" prompt.
Have your batch file send the desired answer..
How to make a randomly-named file.
Or at least how to get an almost random filename guaranteed not to
exist in your TEMP directory.
Wait for your friend's dynamic web site
to come up. Keep pinging it until it responds. Then launch whatever
you want. Automatically.
Enter your password automatically
so you can login or attach to file servers at work.
Ping an entire subnet and find the live computers.
This demonstrates how to increment a number from 1 to 255 and build
a loop based on the incremented number.
Play sounds
Annoy the cat. Play all your sounds with just enough delay between
sounds to let the cat relax.
Rename Files with time and date
Many programs generate files which need to be renamed with unique names.
Time and date usually do the trick.
Rename all files from one extension to
another
Shown here renaming VXE files to EXE by creating a list of files to
be changed and changing them one-at-a-time
Rename all files from one extension to
another
Shown here renaming VXE files to EXE by changing into every subdirectory
one-at-a-time and doing a wildcard rename.
Inner Peace
No, I didn't do this! I helped out with a few tips. It's just as odd
as it sounds and implementing it in DOS batch is even odder. But it is
the biggest batch file I know of. When it starts, it generates about 60
temporary batch file utilities you can peek at.
Bad links? Questions? Send me mail.