Page 1 of 1

Mapping to a network drive questions

Posted: 22 Jul 2009 12:18
by waaraa1331
Hello,
Here is the jist of what I am trying to do:

-Map drive to target machine
-transfer files to target machine
-run program on target machine
-store results on original machine

I need to make sure the drive letter I use to connect the target and original machine is available.

Is there a way to guarantee I always pick an available drive letter AND have the ability to retrieve that drive letter for use in transferring files back and forth? I am aware of the "net use * \\server\c$" command but not how to retrieve the drive letter chosen.

My other option is to have the user manually enter in a drive letter (net use %drive \\server\c$) but if they choose wrong the script just continues executing the rest of the commands and they obviously fail. Is there a way to confirm this command executed successfully? My idea is to keep having them enter a drive letter until they choose an available one. From here I can easily retrieve that information for use in transferring files.

Any other ideas would be greatly appreciated.

Posted: 22 Jul 2009 14:01
by avery_larry
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

Posted: 22 Jul 2009 19:16
by ghostmachine4
@OP, for the solution provided by avery, fsutils may not be available , eg in Win2k. either you have to download it somewhere or you can just use vbscript

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
set objNetwork = WScript.CreateObject("WScript.Network")
Set colDrives = objFS.Drives
For Each objDrive in colDrives
    drive=objDrive.DriveLetter
Next
nextdrive = Chr(Asc(drive)+1) 'get the next drive
'Map network drive
strPath = "\\Server\MyShare"
strUser = "administrator"
strPassword = "password"
objNetwork.MapNetworkDrive nextdrive, strPath, strUser, strPassword
If objFS.DriveExists(nextdrive) Then
   WScript.Echo "Successfully mapped drive : " & nextdrive & " to " & strPath
End If