Unix Tip: Use your Unix scripting skills to write a batch file

ITworld.com –

Attaching a remote drive or directory on a Windows box is called "drive mapping" though the process is essentially the same as what we call mounting in the Unix world. While I rarely spend any significant time working on the Windows end of a Samba connection, I recently had an opportunity to assist a user who was tired of repeatedly having to manually connect directories from a couple of Solaris servers equipped with samba for sharing directories with select Windows desktops.

The solution was to whip up a batch file that mapped the drives on login. Batch files, though far less sophisticated than their Unix shell script counterparts, nonetheless have many of the important features that I have become accustomed to working with on my Unix systems. These include such things as redirection, testable return codes, the ability to test for the existence of a file and the equivalent of /dev/null for discarding command output, errors and all.

Batch files can include comments (lines that stat with "REM" for remark or the less distracting "::"). They provide a looping mechanism that uses the syntax "FOR %%VAR IN (LIST) DO". They provide a command (CHOICE) that works like a case statement. There is even a FIND command that works like grep. While the language still seems primitive in comparison to bash and perl, I was surprised that I was easily able to write a script that did what I needed it to do and automate the process. Admittedly, it was splattered with GOTOs -- something that would have horrified my CS professors from decades ago. Still, it was a functional language and I was pleased that I managed to mimic what I would have done on a Solaris or Linux system on a Windows XP box.

The command that is used to map drives on a Windows box is called "NET USE". While there is a GUI process for mapping drives, I prefer to open a DOS command window to view and modify my drive mappings to any point and click operation.

NET USE without arguments prints a list of the drives that are mapped along with any that were previously mapped and since disconnected. The command is similar to the Unix mount command.

Status       Local     Remote                    Network

OK           L:        \\central\Library         Microsoft Windows Network
Unavailable  M:        \\bigserver\shs           Microsoft Windows Network
Disconnected P:        \\bug-tracker\projects    Microsoft Windows Network
OK           R:        \\smokey\reports          Microsoft Windows Network

Instead of using mount points to connect drives to a system, Windows systems assign letters to each drive. So you can have an alphabet's worth of mappings at any point in time. A:, C: and D: are generally assigned to (frequently absent) floppy drives, the system's hard disk and CDROM. You are free to use the others however you see fit.

To map a drive, use the syntax "NET USE <letter> \\host\directory". For example, "NET USE R: \\smokey\reports". To supply a username and password with the NET USE command, you can add it on the command line like this:

> NET USE R: \\smokey\reports password /USER:alice

Return codes in batch are stored in a variable named %ERRORLEVEL% (or "ERRORLEVEL"). Much like you might test for the successful completion of a command in a bash script by looking at $?, you can determine whether a batch command was successfully executed by evaluating the content of %ERRORLEVEL%. Here's an example:

if not %ERRORLEVEL% 0 goto OOPS

The capitalization is arbitrary. You could also do the same thing with "if not errorlevel 0 goto oops".

Putting this together, we might try to map a drive and then check whether the mapping was successful:

NET USE R: \\smokey\reports password /USER:alice
if not %ERRORLEVEL% 0 goto OOPS
echo Drive mapping failed

Since "stale" drive mapping associations might interfere with your attempt to map the drive afresh, however, you might need to delete a drive mapping before reinstating it. To avoid sending alarming messages to your use, however, you might want to squelch any messages that would be displayed if you tried to delete a drive that wasn't currently mapped.

No problem:

NET USE R: /delete 2> NUL
NET USE R: \\smokey\reports password /USER:alice 2> NUL
if not %ERRORLEVEL% 0 goto OOPS
echo Drive mapping failed

To be even more cautious, you might add a command to ping the system from which the drive is to be mapped before you attempt to map the drive -- just to make sure it's available.

ping smokey | find "TTL=" >NUL
if ERRORLEVEL 1 echo No  response from smokey
goto OOPS

The ">NUL" expression at the end of the ping command line sends both standard and error output to the bit bucket.

If you are building a batch file to map arbitrary drives, you can replace K:, \\smokey\reports, the password and alice with variables:

ping %system% | find "TTL=" >NUL
if ERRORLEVEL 1 echo No  response from %system%
NET USE %L%: /delete 2> NUL
NET USE %L%: \\%system%\%dir% %pass% /USER:%user% 2> NUL
if not %ERRORLEVEL% 0 goto OOPS
echo Drive mapping failed

If you omit the username and password from your NET USE commands, the user will be prompted to enter this information when the batch file is run.

One of the problems with batch files is that you have to be in the directory containing the files to execute them. That is, of course, unless you add the directory containing the files to your search path. The batch language, like Unix systems, provides a search path to facilitate your use of its commands. And, as you might suspect, that environment variable is named %PATH%. So, if you add the directory containing your batch files to %PATH%, you can execute them regardless of your position in your Windows file system. To do this, you can find and modify the PATH= statement in your autoexec.bat or other system startup file or you can make the change using your Windows GUI.

Unlike shell scripts on Unix systems, batch files are run by typing their names without their extensions. So to run a batch file named mapdrive.bat, you would type only "mapdrive".

To run a batch file whenever a user logs in, put the file in the directory shown below (replacing <username> with the person's login name:

C:\Documents and Settings\<username>\Start Menu\Programs\Startup

Clearly, putting the person's password in the batch file may represent something of a security risk. You will have to decide if mapping a drive in this way plays well in your environment.

ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon