Help: NET USE always fails 1st time; 2nd try always works

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
I have a Windows batch file that I wrote to automate interconnectivity between two database systems. (It massages the data along the way and uses various tools to get the job done.) Near the beginning of the script, I call a NET USE command to establish a connection to a network share where temporary data files will be stored during the operation.

I'm still debugging the code, but eventually I'd like to put it in a scheduled task and only worry about it when it breaks. Right now, I'm starting the code manually, and on a "cold" morning start, I get an error 53 from the NET USE. (My code has a lot of error trapping.) If I close the program and try it again seconds later, it works.

This pattern of working on a consecutive try is very repeatable. I'm tempted to write in a second attempt and move on, but I'm not comfortable with ugly code that I don't understand. I'll also have to support this code in the foreseeable future.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
Can you write the batch file such that it keeps retrying the net use command until it is able to connect. (I use
Code:
IF EXIST P:\SomeEmptyTextFile.txt
to verify that my P: drive is mapped.)
 

kevinthenerd

Platinum Member
Jun 27, 2002
2,908
0
76
Can you write the batch file such that it keeps retrying the net use command until it is able to connect. (I use
Code:
IF EXIST P:\SomeEmptyTextFile.txt
to verify that my P: drive is mapped.)

That's a better work-around than what I had in mind, and I might consider limiting it to a certain number of attempts before dying completely.

I'd still like to know the root cause, though.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
I have a batch file that runs at startup and sometimes its net use command fails because the batch file was running before all the network services were even loaded. My solution was to simply insert a delay:

ping 123.45.67.89 -n 1 -w 5000 > nul

gives a 5 second delay. You might want to insert a delay before calling your net use, or simply call it twice with a delay inbetween.
 
Oct 19, 2000
17,860
4
81
All I could find was this:

http://technet.microsoft.com/en-us/library/cc940100.aspx

That makes me think that maybe the NIC has some kind of power savings turned on, but I don't know if you're booting up the machine fresh or if you're maybe coming to a computer that's been sitting idle for a while.

Might also try using IP's instead of names, might get you around the error.

Just some thoughts.