Managing Network Resources Using the Command Line


If you find yourself repeating certain network and file operations over and over, day after day, it makes sense to try to automate the processes. You may get so used to the graphical interface that you forget the command line, but it's still there, and you can perform drive mappings and printer selections with the command line almost as easily as from the GUI. Batch files, which were so familiar in the old DOS days, are still around and are a great way to perform repetitive administrative tasks.

I use batch files to perform simple computer-to-computer backups of important files. Let's say I want to back up the folder c:\book on my computer to a shared folder of the same name on another computer named abalone. Here's what a batch file to do this might look like:

 @echo off net use q: /delete 1>nul 2>nul net use q: \\abalone\book xcopy c:/book q: /e /r /c /y net use q: /delete exit 

Of course, I could bring up Explorer, locate my c:\book folder and the abalone book folder, drag the folder from one computer to the other, and repeat this process every time I want to make a copy of my files. But a shortcut to the above batch file on my desktop will do the same job with a double-click, and I can add the batch file as a Scheduled Task to run automatically every night. Now which seems more convenientthat nifty GUI or the humble command-line batch file? Knowing the net utilities gives you an extra set of tools to work with, and their ancient origins shouldn't make them seem less worthy!

The net command comes to us virtually unchanged since the original PC network software developed by Microsoft and IBM back in the early 1980s. There are so many variations of the net command that I think of them as separate commands: net view, net use, net whatever. Each net command contains a word that selects a subcommand or operation type.

You can get online help listing all the net subcommands by typing net /?, and detailed help by typing or net command /?, where command is any one of the net subcommands.

Mapping Drives with net use

The net use command is the most useful of the command-line network functions. net use makes and disconnects drive mappings, and establishes printer redirection for command-line programs. The basic command is as follows:

 net use drive sharename 

The following example

 net use q: \\abalone\book 

maps drive letter q to the shared folder \\abalone\book. You can't replace the shared folder attached to an already mapped drive, so it's best to place commands in the batch file to delete any previous mapping before trying to make a new one:

 net use q: /delete net use q: \\abalone\book 

The /delete command prints an error message if there was no previous message. An elegant solution to this is to redirect the output of the first net command to NUL, which discards any output. I usually redirect both standard output and standard error output with

 net use q: /delete 1>nul 2>nul 

to ensure that this command will do its work silently.

You can add the /persistent:yes option to a net use command to make the drive mapping return when you log off and back on, matching the function of the Reconnect at Login check box in the graphical drive mapping tool.

You can also map a drive to a subfolder of a shared foldermimicking the "map root" function familiar to Novell NetWare veterans. Subfolder mapping lets you run legacy DOS applications that require that certain files or directories to be placed in the root directory of a hard disk. You can fool them into running with data on a shared network folder. I like to do this because it lets me store data in a centralized place where it will get backed up regularly.

For example, suppose the hypothetical program runit needs to see its data files in the current root directory, and runs from directory \startdir. I want all the files to reside in a shared network folder, in a subdirectory oldprog of a shared folder named \\server\officedata. This batch file does the trick:

 @echo off net use e: /delete 1>nul 2>nul net use e: \\server\officedata\oldprog e: cd \startdir runit c: net use e: /delete exit 

Creating a shortcut to this batch file in Windows XP lets me run the old program with a double-click.

net use also maps network printers to the legacy DOS printer devices LPT1, LPT2, and LPT3. The capture printer setting found in Windows 9x is not available, and the only way to redirect DOS program output to a network printer is through net use.

The following command directs DOS application LPT1 printer output to the network printer:

 net use lpt1: \\server\printername 

The following command cancels it:

 net use lpt1: /delete 



Special Edition Using Microsoft Windows XP Professional
Special Edition Using Microsoft Windows XP Professional (3rd Edition)
ISBN: 0789732807
EAN: 2147483647
Year: 2003
Pages: 450

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net