DirToEnv
Thursday, September 4th, 2008 | Editorial
Objective
To write a utility that will take a directory listing, present it to the user, and take whichever file or directory they choose and assign the name of that directory to a system variable for use in an external batch file.
Purpose
To automate a process at work it was necessary that the names of two directories, a destination and a source, be input. The specifications of the automation process was that it be easily editable by “anyone,” and thus that it not be something that needed to be compiled every time it was changed, in case someone else didn’t have the necessary software.
Basically the boss wanted a set of batch files. Thus, when I wanted to take input without forcing the user to open the command line I needed to resort to utilities. Such utilities exist but since our input requirements were so specific I thought a menu would be better. So I decided to write my own utility.
Development
I started with the decision to do this in C. The decision was largely arbitrary and based on the fact that I still need to keep a reference sheet near by when I work with C++ to avoid any hybrid code.
As with any task the best idea is to break it up into parts. This task consisted of:
- Get an environment variable
- Get a prompt (optional)
- Read the directory
- Display the directory
- Get input
- Output the chosen directory to the environment variable
This program would take possibly 2 or 3 command line prompts. I want to keep things simple, so instead of inputting the directory you want to read, I just had the program list the directory where it is run, like the command ‘dir’. Thus only the environment variable and the optional prompt needed to be passed to the program.
One of the first challenges was learning how to get a directory listing from inside a program. Ask Google and ye shall receive.
With that out of the way, the next challenge was to decide how to get input from the user. For this project where the menu was not the focus simplicity was better, but it had to be friendly as well. I decided to number each entry and ask for the user to input the number. I decided on displaying the choices in columns. The first version made assumptions about columns and lines, as well as the size of the directories or files. Afterward I went back and fixed that. Now before display a few things are calculated. First of all the longest file is found while the files are being read in and it’s length is kept in the variable longest. Next:
longest += 4; /* To allow for the numbers for input */
numcols = COLS / longest; /* Using the PDCurses variable COLS */
maxonscreen = (LINES - 5) * numcols; /*Using the PDCurses variable LINES */
And that’s everything I need to format the output. I was pretty proud of the result, once I got all the bugs worked out.
The final problem to be solved was that of setting the environment variable. I thought I had that figured out with the putenv(const *char) fuction, but every time I ran the program it apparently failed to set the environment variable, even tho it cast no error. I checked and rechecked and was sure I was doing it correctly. Finally I had the program call system(”echo %temp%”); from inside the program then typing echo %temp% after the program ran and discovered that while the program is running the variables are set correctly but the moment the program stops running the environment variable is destroyed.
It was then I was reminded about DOS state machines, which are apparently still an issue is Windows XP, being that a program runs in it’s own little world on the stack and any variables, even environment variables, created during the life of that program are destroyed at the end of that program. And while there are complex solutions that involve possibly breaking the memory structure I realized that in the end I’d be better off just making a batch file to run externally. So I cracked open Awari, copied and modified the code to make an external file, and prompted the user at the end of the program to run c:\temp.bat for themselves to set the system variable, easy enough to accomplish from inside a batch file.
Conclusion
Pretty, no. Does the job, yes. Better than just a batch file alone, absolutely. And it felt good to use my talents for work (finally).
No comments yet.
Leave a comment
Subscribe
Support Cymon's Games
Recent Comments
- David Brady on Book feedback
- Joe on Happy Thanksgiving
- Mike on Happy Thanksgiving
- Joe on NumbrixGenerator
- Joe on Book feedback
- Craig on Book feedback
- Craig on Numbrix
- Joe on Numbrix















