C++/VB - Find a unused drive letter
Asked By Jason
17-Mar-10 05:01 PM
Hello
I have got a to use the subst cmd on a PC, and when the script is run I will not
know what drives are mapped at that time, so I'd like to be able to find a
letter that is not mapped, then I will just use that in the subst cmd.
My question, is how do i find a drive letter that is not mapped?
Scripting.FileSystemObject
(1)
ObjFSO.DriveExists
(1)
ObjFSO.Drives
(1)
ObjDrive.DriveLetter
(1)
ObjFSO
(1)
CreateObject
(1)
DriveExists
(1)
StrAvailable
(1)
Richard Mueller [MVP] replied to Jason

The Drives collection of the FileSystemObject is a collection of all known
drives. You could enumerate all drives, network and local, with:
=======
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDrive In objFSO.Drives
Wscript.Echo objDrive.DriveLetter & ", " & objDrive.Path
Next
=======
You could setup a dictionary object of known letters, then test a letter to
see if it is used. Better might be to just test using the DriveExists
method. For example:
=========
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.DriveExists("K:") = False) Then
' Drive K: is not used.
Next
========
You could even loop through several candidate letters until you find one
available. For example:
=========
Set objFSO = CreateObject("Scripting.FileSystemObject")
arrDrives = Array("K:", "M:", "J:", "E:", "N:")
For Each strDrive In arrDrives
If (objFSO.DriveExists(strDrive) = False) Then
strAvailable = strDrive
Exit For
End If
Next
=======
The value of strAvailable will be the first drive letter available. If it is
blank, none are available.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Jason replied to Richard Mueller [MVP]

in advance (never mind the msgboxes, I just put them in there for testing): * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** Dim objFSO Dim strLocalFolder Dim strFile Dim objFile Dim strLine Dim strNumLines Dim strFrank Dim strHeader Dim strFooter Dim strNewText Set objFSO = CreateObject("Scripting.FileSystemObject") Set strLocalFolder = objFSO.GetFolder("C: \ PHFN007") Set WshShell = WScript.CreateObject("WScript.Shell") Const ForReading = 1 Const ForWriting = 2 strLocalFolder.Files strFileName = strLocalFolder & " \ " & strFile.Name If Left(strFile.Name, 7) = "PHFN007" Then Set objFile = objFSO.OpenTextFile(strFileName, ForReading) 'msgbox "anything" Do Until objFile.AtEndOfStream strLine = objFile.readLine Loop strNumLines = objFile strLocalFolder.Files strFileName = strLocalFolder & " \ " & strFile.Name If Left(strFile.Name, 7) = "PHFN007" Then Set objFile = objFSO.OpenTextFile(strFileName, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.readLine If i = 1 Then strHeader 113700 Then 'msgbox "113700" 'End if Loop End If Next msgbox "replace done" Set objFile = objFSO.CreateTextFile("C: \ PHFN007 \ test2.txt", ForWriting) objFile.WriteLine(strHeader) objFile.Write(strNewText) objFile.WriteLine(strFooter
Function DeleteLine(strKey) 'DeleteLine Function by TomRiddle 2008 Const ForReading = 1:Const ForWriting = 2 Dim objFSO, objFile, strLine, strLineCase, strNewFile, strFile, LineNumber REM, CheckCase strFile = "C: \ proxyaddresses.txt" strKey = "x400" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.Readline REM If CheckCase = 0 then strLine&vbcrlf End If Else strNewFile = strNewFile&strLine&vbcrlf End If Loop objFile.Close Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForWriting) objFile.Write strNewFile objFile.Close Set objFile = Nothing Set
is not what I want at all. Thanks for any help! - -- -- -- -- -- -- -- -- -- -- -- -- -- - Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c: \ file1.txt", ForReading) 'CREATING ARRAY1 FROM FILE 'Create a single string Do to Ubound(array1) array1(i) = Trim(array1(i)) Next 'CREATING ARRAY2 FROM FILE Set objTextFile = objFSO.OpenTextFile _ ("c: \ file2.txt", ForReading) 'Create a single string with Do Until objTextFile.AtEndOfStream array2)) End If 'Create array 3 ReDim array3(arrayLength) 'Create the optput file Set objTextFile = objFSO.createTextFile ("output.txt", ForAppending) 'Populate array 3 and write to file For i = 0 to is not | what I want at all. | | Thanks for any help! | | - -- -- -- -- -- -- -- -- -- -- -- -- -- - | | Const ForReading = 1 | Set objFSO = CreateObject("Scripting.FileSystemObject") | Set objTextFile = objFSO.OpenTextFile _ | ("c: \ file1.txt", ForReading) | | | 'CREATING ARRAY1 FROM FILE | 'Create
3D"yes" Sub Window_Onload LoadDropDown End Sub '''''''''''''''''''''''''''''''' '''Attempt Creating files''''' '''''''''''''''''''''''''''' Const ForWriting = 3D 2 Set objFSO = 3D CreateObject("Scripting.FileSystemObject") Set objFile = 3D objFSO.OpenTextFile("script.txt", ForWriting) objFile.Write Now objFile.Close ''''''''''''''''''''''''''''''''''''''''''''' Sub LoadDropDown ClearListBox Set objFS = 3D CreateObject("Scripting.FileSystemObject") Set objFile = 3D objFS.OpenTextFile("Oracle.txt") strScanners = 3D objFile.ReadAll objFile.Close arrScanners = 3D objOption.value '''''''''''''''''''''''''''''''' ''''''''Creating files''''''''''' ''''''''''''''''''''''''''''''''' 'Const ForReading = 3D 1, ForWriting = 3D 2, ForAppending = 3D 8 'Set objFSO = 3D CreateObject("Scripting.FileSystemObject") 'Set objCreatedFile = 3D objFSO.CreateTextFile("servers.txt", True) 'wscript.sleep 1000 'objCreatedFile.Close 'Set
131), c(232), c(098), c(033), . . . . . . . omitted. . . . . . . . . . c(000), c(000 ), "") Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("audio.wav", 2, True) For i = 0 To 67593 f.write 131), c(232), c(098), c(033), . . . . . . . omitted. . . . . . . . . . c(000), c(000 ), "") Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("audio.wav", 2, True) For i = 0 To 67593 f.write the solution might be as simple as . . . s = 3D "%71%77%65%72%74%79%20%63%76%62%6E% . . .6D%6D" Set fso = 3D CreateObject("Scripting.FileSystemObject") Set f = 3D fso.OpenTextFile("audio.wav", 2, True) f.write unescape(s) f.close a case, the solution might be as simple as . . . s = "%71%77%65%72%74%79%20%63%76%62%6E% . . .6D%6D" Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("audio.wav", 2, True) f.write unescape(s) f.close() I