Reputation: 63
I'm trying to create a mapping script that mapping a network drive and I want to add a checking if the drive letter exist, then Do nothing just show 'Drive is in use' and if not exist, map it.
this is what i got so far:
@echo off
@net use G: /delete /yes
@net use G: \\domainIP\shared folder /user:username password /persistent:yes
:DONE
:EXIT
@pause
please help.
Upvotes: 1
Views: 13399
Reputation: 1329542
I have this one (VonC/setupsenv
#installs/drive_detection.bat
), based on the target mapped path.
It looks for the path, check there is a drive, and, if that drive is "Unavailable", it will launch an explorer.exe
in order to force the drive to be accessed/available again.
There is a simpler option with Powershell.
The reactivation process (where explorer.exe is launched, in order to "refresh" an existing drive displayed as "Unavailable") is:
:ActivateMappedNetworkDrive
set PROCESSNAME=explorer.exe
set PREFIX=start /min
set SUFFIX=%1
rem echo ~~~~~~~~~~~~~~~azerty
rem First save current pids with the wanted process name
set "RETPIDS="
set "OLDPIDS=p"
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (set "OLDPIDS=!OLDPIDS!%%ap")
rem Spawn new process(es)
%PREFIX% %PROCESSNAME% %SUFFIX%
rem Wait for a second (may be optional)
REM choice /c x /d x /t 1 > nul
C:\Windows\System32\timeout.exe /t 5 > NUL
rem Check and find processes missing in the old pid list
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (
if "!OLDPIDS:p%%ap=zz!"=="%OLDPIDS%" (set "RETPIDS=/PID %%a !RETPIDS!")
)
rem Kill the new threads (but no other)
taskkill %RETPIDS% /T > NUL 2>&1
goto:eof
Upvotes: 0
Reputation: 540
How about simply using:
if exist G:\ (echo Drive is in use) ELSE (net use G: \\domainIP\shared folder /user:username password /persistent:yes)
Upvotes: 1
Reputation: 491
if not exist r:\ net use r: \\127.0.0.1\C$
although if R: is always C$ then there is no need to test. Testing is wrong. It chews battery power, CPU cycles, Hard disk hits, etc.
So
net use r: \\127.0.0.1\C$
Will map it if it's not already mapped, else it will fail.
Upvotes: 0
Reputation: 204
This will check if the network drive exist
net use | find "G:"
if %errorlevel%==0 goto :exist
@net use G: \\domainIP\shared folder /user:username password /persistent:yes
goto :end
:exist
@echo Drive is in use
:end
Upvotes: 1