Page 1 of 1

Remembered network connections in a batch file

Posted: 21 Mar 2010 21:35
by brettstix
I have a batch file used by over 40 people to update a folder on their Laptop. The source folder is stored on a server and whenever the users come into the office and plug in to the network they can run the batch file to update the folder on their laptop.

The batch file uses the NET command to connect to a network folder and then runs XCOPY with various switches to only copy anything new. This works well in most cases.

In the batch file I have the command "NET USE Z: \\192.168.1.1\DATA". Some of the users already have Z: mapped to a different network location and as such the batch file will display a message "The local device name has a remembered connection to another network resource" and does not connect. As a result it does not copy any of the updated files.

Is there a way of using an IF command to do something like ....

Code: Select all

IF Z: has a remembered connection to \\192.168.1.1\DATA
...
Reconnect Network Drive to Z: using NET command
set ServerFolder=Z:\UPDATES
...

IF Z: has a remembered connection to[b] [i]SOME OTHER ADDRESS[/i][/b]
...
Connect network drive to Y: using NET command
set ServerFolder=Y:\UPDATES


Or some other way of doing it.

I do not want to remove any remembered connections from anyones computers.

Everyone is running Windows XP.

If that is not clear enough or more information is required I am happy to supply more info.

Thanks.

Re: Remembered network connections in a batch file

Posted: 23 Mar 2010 06:33
by aGerman
Have a look to this example. That could be a way to figure out what you need.

Code: Select all

@echo off &setlocal

set "found=FALSE"
for /f "tokens=1,2,3 delims= " %%a in ('net use^|findstr /i /l /c:"\\192.168.1.1\DATA "') do (
  set "found=TRUE"
  set "status=%%a"
  set "drive=%%b"
  set "share=%%c"
)
if %found%==TRUE (
  echo Share "%share%" is mapped on drive "%drive%". Status is "%status%".
) else (
  echo Share is not mapped.
)

echo.

for %%a in (D E 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 %%a: set "freedrive=%%a:"
)
echo Last free drive is "%freedrive%".

echo.
pause


Regards
aGerman

Re: Remembered network connections in a batch file

Posted: 26 Mar 2010 09:40
by avery_larry
I created this batch file for the purpose of finding an unused drive letter (on XP):

freedrv.cmd

Code: Select all

    :: Version 0.2
    :: This file will echo a free drive letter.  It will also set a variable
    :: to that drive letter if it is passed the variable name like this:
    :: freedrv variablename

@setlocal enabledelayedexpansion
@echo off
set freedrv_drives=
for /f "usebackq tokens=1*" %%a in (`FSUTIL FSINFO DRIVES ^| find ":"`) do (
   if /i "%%a" NEQ "Drives:" (
      set freedrv_drives=%%a !freedrv_drives!
      ) else (
         if not 1%%b==1 set freedrv_drives=%%b !freedrv_drives!
   )
)
call :process
endlocal && echo %freedrv_drive%&&if not 1%1==1 set %1=%freedrv_drive%
goto :eof

:process
for %%a in (z y x w v u t s r q p o n m l k j i h g f) do (
   echo %freedrv_drives% | find /i "%%a:\" >nul 2>nul
   if errorlevel 1 (
      set freedrv_drive=%%a
      exit /b
   )
)
goto :eof


It will echo a free drive letter. It will also, if you pass it the name of a variable, set the variable to that free drive letter:

Code: Select all

@echo off
call freedrv drv
echo Free drive letter is %drv%
net use %drv%: \\192.168.1.1\data
xcopy %drv%:\syncfolder c:\syncfolder ...