Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemObject - Accessing network folders

Status
Not open for further replies.

venkman

Programmer
Oct 9, 2001
467
US
How do I get a reference to a folder object which is the top folder of a network drive with no local mapping? For instance with local drives the following code would get me the top folder of the c drive:

Dim FSO As New FileSystemObject
Dim FolderRef As Folder
Set FolderRef = FSO.GetFolder("c:\")

I had thought that the same would work for a network drive:

Set FolderRef = FSO.GetFolder("\\networkfolder")

but it doesn't. However you can get network sub-folders doing:
Set FolderRef = FSO.GetFolder("\\networkfolder\subfolder\")

What's the story here? why are network servers treated differently?

-Venkman
 
I haven't actually tried this, but did you try:

Set FolderRef = FSO.GetFolder("\\networkfolder\")

with the trailing backslash?


Rob
[flowerface]
 
If I am understanding you correctly, you are trying to get to a folder on the top level of the C drive through the network. If it is a computer that you are accessing, you can also use the computer name. For example,

\\server\filename.xls

Where "server" is the computer name. ----------------------------------------
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning."
Rich Cook
----------------------------------------
 
Rob- Tried that one... didn't work. Haven't tried it yet but I wonder if "\\servername\." works?

Xavier - I think that your example would work for getting a speciffic file, or subdirectory, but I'm trying to get the top level directory of a server.

I think the issue at hand here is that "\\servername\" is not actually a folder. It looks like one in the explorer to give a standard look and feel, but it is not equivalent to a folder. Instead it's a list of shared folders and shared printers, and cannot be treated as a folder by the FileSystemObject. This is just a guess, does that sound right to anyone else. If this is the case, is there a way to get a list of shared folders on a server in VBA? Is there some kind of Samba interface out there for VBA?

-Venkman
 
By golly, I have the same problem - can't get to the root of the network folder structure. I even tried:

set folderref=fso.getfolder("\\home3\mlr")
set folderref=folderref.parentfolder

after which folderref is Nothing.
Very odd. Let us know if the api function helps you out.

Rob
[flowerface]
 
okay, found a bunch of links on the web giving info on this... here's two pretty good ones:



Apparently, the process is slightly complex in that you have to import a bunch of functions and data structures from the dll named "netapi32" .

Using the code from these websites and the msdn documentation ( I wrote a module that creates two public functions. One returns a collection of all network servers on your network. The other returns all the shares on a particular server. If you use these functions you should be careful as collections only store 255 items (as far as I know) and the actual number of shares or servers may be much higher. Much of the code here was copied, but I felt it might be helpful if I posted it here also.
Code is included below.

-Venkman

Option Explicit

Private Type SERVER_INFO_101
sv101_platform_id As Long
sv101_name As Long
sv101_version_major As Long
sv101_version_minor As Long
sv101_type As Long
sv101_comment As Long
End Type

Public Type SHARE_INFO_1
Netname As String
ShareType As Long
Remark As String
End Type




Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const ERROR_ACCESS_DENIED = 5&
Private Const NO_ERROR = 0

Private Const STYPE_DISKTREE = 0 ' /* disk share */
Private Const STYPE_PRINTQ = 1 ' /* printer share */
Private Const STYPE_DEVICE = 2
Private Const STYPE_IPC = 3
Private Const STYPE_SPECIAL = &H80000000

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, uFrom As Any, _
ByVal lSize As Long)

Private Declare Function NetShareEnum Lib "netapi32" (ByVal lpServerName As Long, ByVal dwLevel As Long, lpBuffer As Any, ByVal dwPrefMaxLen As Long, EntriesRead As Long, TotalEntries As Long, hResume As Long) As Long



Private Declare Function NetServerEnum Lib "netapi32" _
(ByVal servername As Long, _
ByVal Level As Long, _
buf As Any, _
ByVal prefmaxlen As Long, _
EntriesRead As Long, _
TotalEntries As Long, _
ByVal servertype As Long, _
ByVal domain As Long, _
Resume_Handle As Long) As Long

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long

Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long

Private Function PointerToStringW(ByVal lpStringW As Long) As String
Dim Buffer() As Byte
Dim nLen As Long

If lpStringW Then
nLen = lstrlenW(lpStringW) * 2
If nLen Then
ReDim Buffer(0 To (nLen - 1)) As Byte
CopyMemory Buffer(0), ByVal lpStringW, nLen
PointerToStringW = Buffer
End If
End If
End Function

Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
Dim tmp() As Byte
Dim tmplen As Long

If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp

End If
End If

End Function

Private Function PointerToDWord(ByVal lpDWord As Long) As Long
Call CopyMemory(PointerToDWord, ByVal lpDWord, 4)
End Function

Public Function GetServers() As Collection
Dim bufPtr As Long
Dim dwEntriesRead As Long
Dim dwTotalEntries As Long
Dim dwResumeHandle As Long
Dim lSuccess As Long
Const NERR_SUCCESS As Long = 0&
Const ERROR_MORE_DATA As Long = 234&
Dim cnt As Long
Dim se101 As SERVER_INFO_101
Dim nStructSize As Long
Dim colResult As New Collection

nStructSize = LenB(se101)

lSuccess = NetServerEnum(0&, 101, bufPtr, -1, dwEntriesRead, dwTotalEntries, &HFFFFFFFF, _
0&, dwResumeHandle)

If lSuccess = NERR_SUCCESS And _
lSuccess <> ERROR_MORE_DATA Then

For cnt = 0 To dwEntriesRead - 1
CopyMemory se101, ByVal bufPtr + (nStructSize * cnt), nStructSize
colResult.Add GetPointerToByteStringW(se101.sv101_name)
Next
End If
Call NetApiBufferFree(bufPtr)
Set GetServers = colResult
End Function

Public Function GetShares(strServer As String) As Collection
Const NO_ERROR = 0

Dim colRes As New Collection
Dim lpBuffer As Long
Dim EntriesRead As Long
Dim TotalEntries As Long
Dim hResume As Long
Dim Offset As Long
Dim nRet As Long
Dim i As Long
Dim Level As Integer
Dim currShareType As Long
Dim currName As String
Dim lpStrPtr As Long


Level = 1
lpStrPtr = StrPtr(strServer)
nRet = NetShareEnum(lpStrPtr, Level, lpBuffer, -1, EntriesRead, TotalEntries, hResume)
If nRet = NO_ERROR Then
If EntriesRead > 0 Then
For i = 0 To EntriesRead
currShareType = PointerToDWord(lpBuffer + Offset + 4)
If currShareType = 0 Then ' it's a disk
currName = PointerToStringW(PointerToDWord(lpBuffer + Offset))
If Right(currName, 1) <> &quot;$&quot; Then
colRes.Add currName
End If
End If
Offset = Offset + 12
Next
End If
Else
' handle the error
End If

Set GetShares = colRes
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top