[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Closing A FileSystemObject And Mapped Network Drive

Posted on 2004-10-22
11
Medium Priority
?
732 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
Comment
Question by:CraigBroadman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12381155
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
ID: 12381171
and here:

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

Expert Comment

by:vinnyd79
ID: 12381247
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:CraigBroadman
ID: 12381251
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
ID: 12381388
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
ID: 12382159
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:
PaulHews earned 1200 total points
ID: 12386973
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
ID: 12401050
Thanks very much for your help
0
 
LVL 38

Expert Comment

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

Author Comment

by:CraigBroadman
ID: 12401436
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
ID: 12401703
I'll subscribe there, but it looks like the other experts are already helping you out.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

650 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