[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 741
  • Last Modified:

Closing A FileSystemObject And Mapped Network Drive

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
CraigBroadman
Asked:
CraigBroadman
  • 4
  • 4
  • 3
1 Solution
 
vinnyd79Commented:
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
 
vinnyd79Commented:
and here:

If Dir("\\CRAIGDESKTOP\C$\Program Files\Bank.exe") <> "" Then
0
 
vinnyd79Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CraigBroadmanAuthor Commented:
I have to Map A Drive because there is other things that I need to do apart from this.
0
 
vinnyd79Commented:
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
 
CraigBroadmanAuthor Commented:
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
 
PaulHewsCommented:
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
 
CraigBroadmanAuthor Commented:
Thanks very much for your help
0
 
PaulHewsCommented:
No problem, thanks for the points.
0
 
CraigBroadmanAuthor Commented:
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
 
PaulHewsCommented:
I'll subscribe there, but it looks like the other experts are already helping you out.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now