Due to a router error as well as the need to wait for my NetGear ReadyNas drives to start up I've created a batch file which both checks that the IP address is local and which loops on the Net Use commands. Both features have been tested and they work.
I was particularly pleased to learn how to pipe output fdrom a .exe (in this case ping.exe), search it for a string and parse the found string without redirecting the output to a file :)
My new NetMap.Bat is appended :)
Thanks, love & respect,
Psy <3
Faith : Hope : Unity
United World Awakening WWW.UWA.Org
NEW NETMAP.BAT
@ECHO OFF
:: WAIT FOR THE NETWORK TO START THEN MAP NAS DRIVES Ping works even if drives are sleeping...
:: During testing I found that the Ping command may work even if the device is not connected to
:: the LAN! Ping displays the device name prefixed "lan." & weirdly may show a competely different IP
:: address from than that displayed by the NAS e.g. 92.242.132.16 instead of 192.168.1.7
:: IT TURNED OUT THAT THE HUAWEI HG635 DOESN'T ALWAYS SEE ETHERNET DEVICE BY NAME! A Sr TalkTalk sales
:: guy arranged for the latest super router to be sent to me - a D-Link! - sadly it proved not to have
:: 1Gb Ethernet ports so I reverted to the one I bought on eBay & had to make this bullet proof:
SET ReadyNas=UwaNas1
:: New approach: checking for 192.xxx is better than checking ERRORLEVEL after pinging the NAS:
:: Note if not 192.xxx then ping raises ERRORLEVEL > 0 but the cause can't be determined without
:: searching/parsing the output file. SETLOCAL is crucual 'cos of the weird way batch file are run:
SETLOCAL
FOR /F "tokens=4* delims=. " %%a in ('Ping %ReadyNas% -n 1 ^| find "Ping statistics for"') DO (
SET FirstIp=%%a
SET RestOfIp=%%b
)
IF NOT %FirstIp% == 192 (
ECHO Router error: Ping returned a non-local IP address %FirstIp%.%RestOfIp% for %ReadyNas%!
ECHO I'm afraid you're going to have to reboot because of a firmware bug in the Huawei635!!
GOTO :Exit
)
ECHO Ping OK %ReadyNas% is at IP address %FirstIp%.%RestOfIp% now mapping network drives...
ECHO Net Use * /Del /Yes >NUL
Net Use * /Del /Yes >NUL
IF ERRORLEVEL 1 GOTO :Error
:: /a => arithmetical variable (default is string)
SET /a WaitCount = 0
SET /a WaitTime = 5
SET /a WaitLimit = 8
GOTO :NetUse
:: IF Net Use didn't work then we have to wait for the NAS disks to wake up
:Wait
IF %WaitCount% GTR %WaitLimit% GOTO :Error
SET /a WaitCount += 1
:: The ECHO was for a SLEEP command which isn't available by default under W10
:: (and maybe other versions as well: plainly batch file commands have moved on a
:: lot since my DOS days when there wasn't even "IF c<condition> (<set of commands>)")!
:: Next command is needed as TIMEOUT displays a countdown but ends in saying it's 'Waiting 0 seconds'!
ECHO Waiting %WaitTime% seconds
TIMEOUT %WaitTime% >NUL
:: Now try to map Z: & U: drives (this can take a while IF disks have spun down)
:: ECHO Y | Net Use * /Del doesn't work 'cos Net.Exe doesn't process pipe (hence the YES opt)!
:NetUse
ECHO Net Use Z: "\%ReadyNas%\PsyData"
Net Use Z: "\%ReadyNas%\PsyData"
IF ERRORLEVEL 1 GOTO :Wait
ECHO Net Use U: "\%ReadyNas%\Utils"
Net Use U: "\%ReadyNas%\Utils"
IF NOT ERRORLEVEL 1 GOTO :Exit
GOTO :Wait
:Error
ECHO Last command ERRORLEVEL is %ERRORLEVEL%
:Exit
PAUSE
EXIT