Solved

Closing A FileSystemObject And Mapped Network Drive

Posted on 2004-10-22
718 Views
Last Modified: 2010-07-27
See the below code that I have written.

I have created a Mapped Network Drive, then checked a specified file for the Date it was created and the Size using FileSystemObject.

The problem I have is that I cannot Disconnect the network drive using "Net Use strDrive /delete"

I think it is because the FileSystemObject that I have opened still has a hold on that drive as when I run "Net use E: /delete" in DOS it says....

"The device is being accessed by an active process"

The basic code I have is

'CREATES THE MAPPED DRIVE
Shell "net use F: \\CRAIGDESKTOP\C$"

'CHECKS THAT THE FILE EXISTS
If Dir(F:\Program Files\Bank.exe) <> "" Then
           flxVersion.TextMatrix(flxVersion.Row, 1) = MyDesktop

           'OPEN FILE TO GET DETAILS
           Set fso = CreateObject("Scripting.FileSystemObject")
           Set f = fso.GetFile(F:\Program Files\Bank.exe)

           'PUT DETAILS INTO FLEXGRID
           flxVersion.TextMatrix(flxVersion.Row, 2) = f.Size
           flxVersion.TextMatrix(flxVersion.Row, 3) = f.DateCreated
               
           'SET TO NOTHING ???????? DOESNT WORK?????
           Set f = Nothing
           Set fso = Nothing
Else
          flxVersion.TextMatrix(flxVersion.Row, 1) = MyDesktop
          flxVersion.TextMatrix(flxVersion.Row, 2) = "n/a"
          flxVersion.TextMatrix(flxVersion.Row, 2) = "n/a"
End If

'DELETE/DISCONNECT NETWORK DRIVE ??????????
Shell "net use F: /d"


Thanks in advance for any help
0
Question by:CraigBroadman
    11 Comments
     
    LVL 28

    Expert Comment

    by:vinnyd79
    Why map a drive,just try to access the file directly using the UNC path:

               'OPEN FILE TO GET DETAILS
               Set fso = CreateObject("Scripting.FileSystemObject")
               Set f = fso.GetFile("\\CRAIGDESKTOP\C$\Program Files\Bank.exe")
    0
     
    LVL 28

    Expert Comment

    by:vinnyd79
    and here:

    If Dir("\\CRAIGDESKTOP\C$\Program Files\Bank.exe") <> "" Then
    0
     
    LVL 28

    Expert Comment

    by:vinnyd79
    Or you might want to try shelling the command interpretor with the /c switch instead of shelling net use directly.

    i.e.
    'CREATES THE MAPPED DRIVE
    Shell Environ("ComSpec") & " /c net use F: \\CRAIGDESKTOP\C$", vbHide
    0
     

    Author Comment

    by:CraigBroadman
    I have to Map A Drive because there is other things that I need to do apart from this.
    0
     
    LVL 28

    Expert Comment

    by:vinnyd79
    I don't think FSO is the problem as you are setting it to Nothing. I think it is the way you are mapping the drive.  Here is a good example of mapping with FSO:

    http://www.computerperformance.co.uk/ezine/ezine3.htm
    0
     

    Author Comment

    by:CraigBroadman
    I can disconnect the drive if i take the fso code out. But i will have a look at the link and see what i can do.

    if it works will accept your answer

    Thanks
    0
     
    LVL 38

    Accepted Solution

    by:
    Here's an API version.  The real advantage here is that you can force disconnection.  It isn't just the FSO, any function call to check on a file seems to cause this problem, even though there are no files being opened.  Sorry I took out your flex and fso code for testing.  Should be easy for you to add back in.

    Option Explicit
    Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
    Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long

    Const RESOURCETYPE_DISK = &H1
    Const ForceDisconnect As Long = 1

    Private Type NETRESOURCE
      dwScope As Long
      dwType As Long
      dwDisplayType As Long
      dwUsage As Long
      lpLocalName As String
      lpRemoteName As String
      lpComment As String
      lpProvider As String
    End Type


    Private Sub Command1_Click()
        Const FileName = "G:\temp\test.txt"
        Dim NetRsc As NETRESOURCE
        NetRsc.lpRemoteName = "\\HOME\C$"
        NetRsc.lpLocalName = "G:"
        NetRsc.dwType = RESOURCETYPE_DISK
       
       
        WNetAddConnection2 NetRsc, vbNullString, vbNullString, 0

        If Dir(FileName) <> "" Then
                Debug.Print FileDateTime(FileName)
                Debug.Print FileLen(FileName)
        End If

       
        If WNetCancelConnection2(NetRsc.lpLocalName, 0, ForceDisconnect) = 0 Then
            Debug.Print "successfully disconnected"
        End If

    End Sub
    0
     

    Author Comment

    by:CraigBroadman
    Thanks very much for your help
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    No problem, thanks for the points.
    0
     

    Author Comment

    by:CraigBroadman
    Have just posted another question. Basically looking for other methods of moving a file, other than FSO.

    When I am mapping a client machine I copy their current file to a safe folder so that I can copy an updated version into the orginal location.

    However, this safe file cannot be deleted by the client as it says another person or program is currently using it.

    If you have any ideas for this i would be grateful, another 250 points up for grabs
    0
     
    LVL 38

    Expert Comment

    by:PaulHews
    I'll subscribe there, but it looks like the other experts are already helping you out.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now