DOS batch files do not have the most flexible scripting syntax. They are also not the most modern or full featured. One will often use a VBS or Perl script to glue together or bootstrap complex or outdated software.
Even so if you have ever wanted or needed to access a flexible database from a DOS batch file, but though the overhead of installing a full-blown database that understood SQL (so you don’t need to learn some wacky new syntax), now is your chance.
I won’t go into all the details of Sqlite but merely show you a start to end set of steps to get you going. I hope once you have seen it in action you will think up new and interesting ways of applying the same techniques in your own projects.
Before I continue I will say that this is not the only database that can perform this sort of thing. When talking about alternatives Berkley DB springs to mind. There are also other alternatives, each with their slant on query languages. For now I will only provide an Sqlite example because it is the new kid on the block and is, up to now, still a little less known than the other (possibly dated) alternatives. One added advantage over all the others is the fact that Sqlite and its code is released into the public domain.
Starting from the very beginning
You will need to download a single executable file, the Sqlite command line shell from the Sqlite download page: http://www.sqlite.org/download.html
Sqlite runs on numerous operating systems. The version we are looking for will be under the heading “Precompiled Binaries For Windows“.
The filename should something like: sqlite-shell-win32-x86-3070400.zip
Download and unzip the file and you will find a the executable file: sqlite3.exe
Unzip the file into a convenient directory.
Run the Sqlite shell and create a database file
To run the command line shell (in windows) you can click on Start->Run…
Then enter cmd to open a DOS command shell and press OK.
cd to the directory that sqlite is in or specify the full path, then put the name of the new (to be created database file), I will call mine mydatabase..sqlite.
C:\>cd sqlite C:\sqlite>sqlite3.exe mydatabase.sqlite
Now we have an empty database.
Create a table and some test data
Enter the following into the Sqlite shell.
CREATE TABLE person ( id int, name varchar(30), phone varchar(30) ); INSERT INTO person VALUES (1, 'Jim', '123446223'); INSERT INTO person VALUES (2, 'Tom', '232124303'); INSERT INTO person VALUES (3, 'Bill', '812947283'); INSERT INTO person VALUES (4, 'Alice', '351246233'); .exit
Now for the Batch file magic
We will create some batch files that can look up a person’s details given an integer id. You can be more creative.
We will make the .bat files from the DOS command line for this example.
echo @echo SELECT name from person where id=^%1^;^| sqlite3.exe mydatabase.sqlite > person.bat echo @echo SELECT phone from person where id=^%1^;^| sqlite3.exe mydatabase.sqlite > phone.bat
What we have done is create two batch files that each take a single parameter (%1) which in this case will need to be the id of the person we are looking for when called. The echo command is used to pass the Sql select statement to the database. person.bat will return the name of the person for the given id. phone.bat will return the corresponding phone number.
To verify this we can take a quick look inside the .bat files
C:\sqlite>type person.bat echo SELECT name from person where id=%1;| sqlite3.exe mydatabase.sqlite C:\sqlite>type phone.bat echo SELECT phone from person where id=%1;| sqlite3.exe mydatabase.sqlite
Now for the results
C:\sqlite>person.bat 3 Bill C:\sqlite>phone.bat 3 812947283
Now we have the details of the person with the id of 3. The SQL can get as complicated as you like.
A single data file (mydatabase.sqlite), one (or more) batch files (person.bat and phone.bat) and a single executable (sqlite3.exe) and you have a fulling working application.
Maybe this would not make a full-blown application but a very powerful, easy to deploy configuration database for a legacy program launcher.
You can get a fancy as you like. It’s as simple as that.