Solved

net use

Posted on 2001-09-05
24
710 Views
Last Modified: 2008-02-01
I mapped a network drive to "W:" in a batch file - "x.bat". At the end of the batch file I do:
net use W: /delete

I type "x.bat" in a DOS box. the batch file cannot execute the "delete" command because the DOS box ends up in the W: drive.

How do I delete the mapping after the work is done? also
How do I judge if "W:" is already taken before I do the mapping?

thanks

0
Comment
Question by:txholdem
  • 12
  • 7
  • 3
  • +2
24 Comments
 
LVL 47

Expert Comment

by:dbrunton
ID: 6459250
>>How do I judge if "W:" is already taken before I do the mapping?

Probably a line like the below in your batch file

if not exist W:\nul goto exit
0
 
LVL 7

Expert Comment

by:rin1010
ID: 6459267

txholdem,

I may not understand exactly what's happening, but can you change drives
in the batch script prior to the delete command? To determine whether W: is available
you might have success testing for the nul device on that drive
and then running your command accordingly, like:

if exist w:\nul echo Drive Exists...

Here are links to some items that might pertain to your situation
(or may not have anything to do with it.) One page has a question similar to yours
and some followups at the bottom... Note the mention of a known bug in SP4
and one suggestion of the command:

net use w: /delete /y > nul: 2>nul:

If none of this is helpful, please post back with your os version
and preferably a copy of the relevant portions of your script
and someone can probably figure out a way to make it work.
Here are the urls of the articles...

"drive mapping won't delete while running from a batch file script..."
http://mlarchive.ima.com/winnt/1999/Jun/0611.html

http://www.denicomp.com/maprshd.htm

http://www.denicomp.com/maprexec.htm
 
0
 
LVL 10

Expert Comment

by:Longbow
ID: 6460585
In the batch you need to go to another drive
ie c:
you may not delete w: when the batch is working on E:
If an application is open on W: you need to close it before deleting the mapping
0
 
LVL 1

Author Comment

by:txholdem
ID: 6460695
rin1010,

what does "2>nul:" mean?
0
 
LVL 1

Author Comment

by:txholdem
ID: 6460714
I'll give the credit to rin1010, but I'll give another 50 points for a short DOS program that can

1, detect and record the current drive,
2, loop through E: - Z: to find available drives.
3, change to the drive and back to the original drive.

thanks
0
 
LVL 5

Expert Comment

by:PatOBrien
ID: 6461629

@echo off

::1. save current drive\path to temp bat file
set TempFile=c:\temp\myTemp.bat
for %%d in ('cd') do (echo %~dd >%TempFile% & echo cd %~pd%nd >>%TempFile%

::2. loop through drives - find unassigned drive
for %%d in (E F G H I J K L M N O P Q R S T U V W X Y Z) do net use %%d: >nul 2>nul|| echo Drive %%d: is not in use.

::3. loop through drives - find used drive - switch there and back (use bat file created in #1 to switch back)
for %%d in (E F G H I J K L M N O P Q R S T U V W X Y Z) do net use %%d: >nul 2>nul && cd %%d: & call %TempFile%

::clean up
del %TempFile%

0
 
LVL 5

Expert Comment

by:PatOBrien
ID: 6461647
let me know if you want an explanation...

actually, concepts used above can be used to answer your original questions...

A. How do I delete the mapping after the work is done? also
B. How do I judge if "W:" is already taken before I do the mapping?

B.
net use w: >nul 2>nul && echo Drive W: was found.

or

net use w: >nul 2>nul || echo Drive W: was not found.


A.  this has been answered previously...
switch to another drive before deleting the W:. However, you can't start the bat file from the W: drive and then deleted it.



0
 
LVL 5

Expert Comment

by:PatOBrien
ID: 6461678
2>nul means send error message to nothing

also, you can group commands on a single line with &, && or ||.


so,

    goodcommand & badcommand 2>nul

will first execute "goodcommand", then execute "badcommand" and any error messages from badcommand will not be displayed

     goodcommand && badcommand

will execute "goodcommand" and will execute "badcommand" only if "goodcommand" had no errors.

     badcommand && goodcommand

will execute "badcommand" and will not execute "goodcommand" if there were errors in "badcommand"


     badcommand || goodcommand

will execute "badcommand" and will execute "goodcommand" only if there WERE errors in badcommand.


0
 
LVL 7

Expert Comment

by:rin1010
ID: 6462457

txholdem,

If the test for the nul device to determine the existence of an available drive works for you,
then you can loop through the relevant drive letters to find an available drive.
The technique doesn't work under all situations and can be dependent on network software,
type of server and file system in use and also whether the drive is a removable device
not containing a disk. These things can be overcome with extra commands
and there are other methods to determine whether a drive is ready or available,
but not knowing your exact situation and os I'll just give an example
of checking for possible drive letters by testing for the nul device.

Other methods of eliminating possible errors include the use of external commands
such as Find and Choice, etc., and their errorlevel checking abilities if you don't mind
calling external apps. You can also use the internal Vol command to test for a valid drive,
but again, some of this is os specific, so for now I'll provide examples using command.com
internal commands to see if it works for you.

This just loops through each given drive letter and tests for the existence of
the nul device on each drive... You can edit the choice of letters to your specs...

for %%x in (e f g h i j k l m n o p q r s t u v w x y z) do if exist %%x:\nul echo %%x Exists...

The above merely echoes the letter of all valid drives to the screen...
You mentioned wanting to "loop through E: - Z: to find available drives"
but if you just want to get the drives available, then you can set the letters
to environment variables and refer to them when needed...
For example, to change to that last drive, you could do it like:

for %%x in (e f g h i j k l m n o p q r s t u v w x y z) do if exist %%x:\nul set lastdrv=%%x
echo.
echo   Changing to %lastdrv%: drive...
%lastdrv%:

Or maybe use something like this:

for %%x in (e f g h i j k l m n o p q r s t u v w x y z) do if exist %%x:\nul set %%x=%%x

Some notes about these commands; The EE formatting might break these
long command lines (having all the drive letters)... If so, make sure it's all one line.
You can change the letters to upper case in your script if you want...
I use lower case to keep the line shorter when formatted with EE's font.
Case doesn't matter but the variable is set accordingly and is how it's displayed.
Instead of using all the letters I'll just use a few in some of these examples
so the line doesn't get split... Remember that these examples are for msdos scripts
and can be modified for another os if necessary.
Also, if you test a drive not containing a disk (e.g., an empty cdrom drive)
you'll get the "Not ready reading drive x ... Abort, Retry, Fail?" error message...
There are methods to check for and eliminate this if needed,
but you can also use the command.com /f switch to bypass the arf error, like:

for %%x in (e f g h i j k s t u v w x y z) do command/f/c if exist %%x:\nul set nextdrv=%%x
cls
echo.
echo   Changing to %nextdrv%: drive...
%nextdrv%:

The error message still displays on screen, but that can be corrected if needed...
The cls command will remove it anyway. And again, there are other methods
to accomplish these things if you want to use externals like Find, etc...
These are general purpose examples for you to modify as needed and should work
using command.com under most circumstances.

You requested wanting to "detect and record the current drive..."
One method without using externals is to use the Prompt command
and create a couple of temporary batch files to set the current drive to a variable.
Here's a script to set the current drive's letter to a variable named %curdrv% ...

@echo off
echo @prompt set curdrv=$n:>~curdrv1.bat
command.com/c~curdrv1.bat>~curdrv2.bat
call ~curdrv2.bat
for %%x in (~curdrv*.bat) do del %%x
cls
echo.
echo   The current drive is %curdrv%


So putting it all together and having the script do similar to what you note;
"detect and record the current drive," "loop through E: - Z: to find available drives"
and "change to the drive and back to the original drive..."
you should be able to do something similar to this:


@echo off
echo @prompt set curdrv=$n:>~curdrv1.bat
command.com/c~curdrv1.bat>~curdrv2.bat
call ~curdrv2.bat
for %%x in (~curdrv*.bat) do del %%x
for %%x in (e f g h i j k l m n o p q r s t u v w x y z) do if exist %%x:\nul set lastdrv=%%x
cls
echo.
echo   Changing to %lastdrv%: drive...
%lastdrv%:
dir %lastdrv%:
echo.
echo   Changing back to %curdrv%
%curdrv%
echo.


Note the use of colons to specify a drive... These can be modified to suit your needs...
Also the "dir %nextdrv%:" command is just to show that the variable is set...
You can cd\ or whatever at that point... These are simply to give you ideas
to work with but will need editing as applies to your situation...
The above just finds the last valid drive so you can change
that to get what is available at the time.

Hope some of it works and is what you want... Please post back if you have questions
about any of it or need other methods or want to modify this further...
 
0
 
LVL 7

Accepted Solution

by:
rin1010 earned 100 total points
ID: 6462481

txholdem,

After testing some things again I see that one of my previous examples
may not work properly using the "command/f/c" portion...
If that's the case, just remove that part...
The script will then do as desired although you may encounter problems
with drive letters of removable devices. If so that can be eliminated in other ways.

Also I probably misunderstood what you meant about
"loop through E: - Z: to find available drives..."
If you mean to look for the next available drive letter not in use to map,
you can use the same technique but will want to verify that the nul device does not exist.
Also you'll want to test the letters backwards and the variable can then be set
to the first drive letter that doesn't exist. Here's an example,
again not using all the letters so as to keep it all on one line:

for %%x in (z y x w v u t s k j i h g f e d c) do if not exist %%x:\nul set mapdrive=%%x
echo   Next available drive letter is %mapdrive%

Sorry if there was confusion, but post back if you need alternatives...
 
0
 
LVL 1

Author Comment

by:txholdem
ID: 6463884
pat,
I've posted 50 points at a separate thread/question. I need some explanations for your earlier answer..
0
 
LVL 1

Author Comment

by:txholdem
ID: 6464381
rin1010,

I keep getting "out of environment space" error when I run this .bat:

echo @prompt set curdrv=$n:>~curdrv1.bat
command.com/c~curdrv1.bat>~curdrv2.bat
call ~curdrv2.bat
for %%x in (~curdrv*.bat) do del %%x

btw, can you explain the above a little? and how come I cannot see the file ~*.bat in the file manager and edit it with common editors?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:txholdem
ID: 6464390
I can set more environment variables actually. I also have get rid of quite a bit of settings from control panel | environment dialog. but still getting "out of environment space..."
0
 
LVL 1

Author Comment

by:txholdem
ID: 6464619
ok. I've solved the environment space problem. I still need the explanations of you code though. thanks.
0
 
LVL 7

Expert Comment

by:rin1010
ID: 6464792

txholdem,

You can increase the environment size for that script using the command.com /e switch
followed by the size in bytes that you want to set the environment...
For example;  command.com/e:2048  specifies an environment size of 2048 bytes.
The valid range is 160 to 32768. DOS will round it to a multiple of 16 bytes.
The initial default is 256 bytes so you might need to clean your path statement
or any other unnecessary environment variables that are being created.
To use it in that script to set the current drive into a variable you can try it like this:

@echo off
echo @prompt set curdrv=$n:>~curdrv1.bat
command.com/e:2048/c ~curdrv1.bat>~curdrv2.bat
call ~curdrv2.bat
for %%x in (~curdrv*.bat) do del %%x

If that doesn't work, increase the size or place the command where the error occurs.
Regarding your request for an explanation of it, I'll be glad to provide what I can,
but you'll see how it works after viewing the temporary batch files that are created.
The reason you're not seeing them after the script is run is because
the script deletes them after they're used...
Rem the line that deletes them and you can then see the files
in FileManager and open them in Notepad or whatever.

Also, the line that deletes the temporary files doesn't need to do it the way I did:

for %%x in (~curdrv*.bat) do del %%x

That can be done simply using the del command, like:

del ~curdrv*.bat

So try that and post back if you have further problems or questions about it...
Also you didn't mention your os nor whether the other scripts I posted did you any good.
Note my comment in your other question to Pat about the commands
that work only under NT and not for MS-DOS scripts,
which may be why the ones you mentioned failed.
But here's a modified version of the script you asked about:


@echo off
echo @prompt set curdrv=$n:>~curdrv1.bat
command.com/e:2048/c ~curdrv1.bat>~curdrv2.bat
call ~curdrv2.bat
del ~curdrv*.bat
cls
echo.
echo   The current drive is %curdrv%


 
0
 
LVL 1

Author Comment

by:txholdem
ID: 6465026
rin1010,

I need explanations about:
$n, ~, prompt

thanks
0
 
LVL 7

Expert Comment

by:rin1010
ID: 6465293

What it's doing, txholdem, is using the DOS Prompt command
and echoing instructions to the temporarily created batch file
to set the %curdrv% variable using the Prompt command's $N code
which specifies the current drive.

You could replace the $N: with $T or $D to set the current time or date into the variable.
Type Prompt/? at the prompt to get other Prompt codes. It's a handy script
and can easily be modified to achieve other tasks. Note the colon I added after the $N ...
The drive letter is returned without a colon, but as I previously mentioned,
the colon can alternatively be appended to the variable name.
Hope this explains it for you...
 
0
 
LVL 1

Author Comment

by:txholdem
ID: 6470952
rin1010, what's wrong with the following?
----------------------
for %%x in (z y x w ...) do if not exist %%x:\nul goto somewhere
:somewhere
set mapdrive %%x:
echo Next available drive is %mapdrive%:
-----------------------
error message:
environment variable mapdrive %x not defined...
0
 
LVL 1

Author Comment

by:txholdem
ID: 6471060
I also tried the following, which doesnt work either:
------------------
for %%x in (z y x w...) do if not exist %%x:\nul set mapdrive %%x goto somewhere

:somewhere
echo Next available drive is %mapdrive%:
0
 
LVL 1

Author Comment

by:txholdem
ID: 6471083
I know I should do this:
for %%x in (f g h i j k l m n o p q r s t u v w x y z) do if not exist %%x:\nul set mapdrive=%%x

but how do I break the loop when the drive is already found?
0
 
LVL 7

Expert Comment

by:rin1010
ID: 6471806

I didn't notice the portion of your previous question, txholdem,
about the tilde (~) used in my syntax... As you've probably discovered,
that's just part of the file names of the temporarily created batch files
used in setting the variable to the current drive...
I use the tilde so they're prominent in a file listing later,
in case they're not deleted by the script and left behind,
but their names can be changed if needed...
Just change the file specs in the other references too,
like in the command that deletes them, etc...

Concerning your newest questions; I see a couple of errors,
but you still haven't mentioned your os and version...
Regarding this following script example you pasted:

for %%x in (z y x w...) do if not exist %%x:\nul goto somewhere
:somewhere
set mapdrive %%x:
echo Next available drive is %mapdrive%:

The replaceable variable %%x just processes each string in the set (in parenthesis)
but doesn't retain a value once the set is processed...
So subsequently calling %x won't work because it doesn't have a value then.

In your next question you pasted and asked about this example:

for %%x in (z y x w...) do if not exist %%x:\nul set mapdrive %%x goto somewhere

In the above you need to use the equal sign with the Set command, like:

for %%x in (z y x w) do if not exist %%x:\nul set mapdrive=%%x

Also you can't issue another command (goto somewhere) in that particular manner
(that syntax would set the %mapdrive% variable to something like;  "w: goto somewhere" )
and if you 'goto' somewhere it would exit the For processing at that point,
so you'd additionally need to use a Loop to return.

That also relates to your last question where you asked
"how do I break the loop when the drive is already found?"
and this example you pasted:

for %%x in (f g h i j k n o p q r s t u v w x y z) do if not exist %%x:\nul set mapdrive=%%x

You don't necessarily need to break the loop... The For command processes
each letter in the set and will continuously change (or reset) the %mapdrive% variable,
depending on whether the nul device does or doesn't exist...
So the %mapdrive% variable becomes set to the last drive in your list of drive letters
not having the nul device. That's why I previously noted that to get the next available drive
you'll want to process the drive letters backwards... Then the %mapdrive% variable
will be set to the last drive tested not containing the nul device,
which would actually be the next drive available, like:

for %%x in (z y x w v u t s r q p o n k j i h g f) do if not exist %%x:\nul set mapdrive=%%x
echo   Next available drive letter is %mapdrive%:

Hope this clears some of it up and answers your additional questions...
 
0
 
LVL 1

Author Comment

by:txholdem
ID: 6471972
I tried 2 OS- NT4.0 and Windows98.

On the NT4.0:
----------------
echo @prompt set curdrv=$n:>z:\dir1\~curdrv1.bat
cmd.exe /c z:\dir1\~curdrv1.bat>z:\dir1\~curdrv2.bat
C:
cd\
cd "Program Files"
call z:\dir1\~curdrv2.bat
%curdrv%
del *curdrv*.bat
------------------
this works fine, while on the Windows98 machine, I replace the cmd.exe above with this line:
----------------------
command.com /e:4096 /c z:\dir1\~curdrv1.bat>z:\dir1\~curdrv2.bat
----------------------
this does not work- the file ~curdrv.bat is empty.. it seems that the 98 DOS box does not recognize the '$n' option.. however, when I do "prompt /?" I do see the option.
0
 
LVL 1

Author Comment

by:txholdem
ID: 6471977
Oops, I mean the ~curdrv2.bat file is empty. (~curdrv1.bat is fine.)
0
 
LVL 7

Expert Comment

by:rin1010
ID: 6478337

txholdem,

I initially debugged that script under Windows 98 and successfully tested it
under versions of DOS 5 and 6... It should work using the syntax you pasted.
Ensure that the drive specified to hold the temporary batch files exists
and that the correct path is given for each reference to those files.

I've encountered complications under certain circumstances
when spaces were used with or after the command.com switches.
Unlikely this pertains to your current problem but worth a mention.

Definitely make sure that the command.com referenced is to the correct copy
that you want to use and perhaps include its path, like:

c:\command.com/e:2048/c

Or try using the %comspec% variable, like:

%comspec%/e:2048/c

If you're still having problems with it, look over your syntax for any typos...
Also trap whatever error messages you're getting, which will give a clue,
e.g. if the z:\dir1 directory doesn't exist (which can also be created
temporarily in the script and then removed.) If it works when you're not
specifying other directories for the temp files (allowing them instead
to be created in and run from the working directory of the main batch file)
then something is wrong with the paths of the specified directories.

Here's a copy of the script that works under Windows 98
(calling the %comspec% environment variable)
and using an existing directory named Dir1 on the E: drive
to hold the temporary batch files:


@echo off
echo @prompt set curdrv=$n:>e:\dir1\~curdrv1.bat
%comspec%/e:4096/c e:\dir1\~curdrv1.bat>e:\dir1\~curdrv2.bat
call e:\dir1\~curdrv2.bat
del e:\dir1\~curdrv*.bat
cls
echo.
echo   The current drive is %curdrv%


 

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now