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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RmDir fails after Win98 to XP upgrade. 1

Status
Not open for further replies.

BrendanDeTracey

Programmer
Apr 15, 2004
10
CA
Hi,
The office has finally upgraded from Win98 to XP. Now my VB code which worked perfectly under Win98 fails when executing the RmDir command. The Kill command works fine.

Is this a security problem? I have admin privileges and own the directory to be removed.

Thanks,
Brendan
 
Have you tried creating a directory C:\test then doing a
rmdir "C:\test" in VB?

Assuming that works, maybe it's something to do with the actual path (embedded spaces perhaps?), permissions, read only or whatever.

What error message do you get?
 
Yes, I have tried the test you described and it worked.
I am getting file/path access error 75.
 
Could it be that the folder you are trying to remove is referenced via a relative path rather than absolute? If so, try the full folder path and see if that works.
 
The Directory I am trying to remove is:

CMC_Path & path(j)

where strings:
CMC_Path = "c:\iofs_pom\cmc\"
path(j) = "2004010112"

Neither string contains any spaces.
The directory is empty.
 
Can you remove the folder from the DOS command prompt?

In VB Immediate window, what happens if you type

rmdir "c:\iofs_pom\cmc\2004010112"?

I confess that I'm now clutching at straws a bit - especially if Kill command works on same string concatenation but have you investigated the permissions on the folder?
 
Strange,
When I comment out the call to Clean_directory, and ensure the directory is empty, the RmDir works. Somehow the Sub Clean_directory is the source of my ills.


Call Clean_directory(CMC_Path & path(j) & "\")
RmDir CMC_Path & path(j)
''''''''''''''''''''''''''''''''''''''''''''''''''
'Directory must be terminated with a "\".
Public Sub Clean_directory(Directory As String)
On Error Resume Next
If Dir(Directory, vbDirectory) <> "" Then
Kill Directory & "*"
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''
 
I wonder if this could be a timing issue - assuming you have not commented out your Clean_directory call, does it work if the directory is empty before you run this vb code?

Perhaps the operating system is still busy killing off contents of the directory when you return to call RmDir. Not sure if insertion of a DoEvents prior to RmDir or some sort of delay might help?
 
Timing is not the problem. I break on the RmDir line and then press F8 to execute it, after a slight pause.

Executing the RmDir command from the intermediate window invokes the same error. All Directories are read-only, and changing that attribute does not solve the problem.

I may try deleting entire directory+contents using Windows scripting. No big deal, but I thought there must be a simple explanation for this.
 
This seems to work for me using the FileSystemObject.

' Add a reference to Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim CMC_Path As String
Dim Path(0) As String
Set fso = CreateObject("Scripting.FileSystemObject")
CMC_Path = "c:\iofs_pom\cmc\"
Path(0) = "2004010112"
Call Clean_directory(CMC_Path & Path(0) & "\")
If fso.FolderExists(CMC_Path & Path(0)) Then fso.DeleteFolder CMC_Path & Path(0), True
Set fso = Nothing
MsgBox "Complete!", vbInformation
End Sub

Private Sub Clean_directory(Directory As String)
Dim MyFolder As String
Dim fs As FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
MyFolder = Dir(Directory, vbDirectory)
Do While MyFolder <> ""
If MyFolder <> "." And MyFolder <> ".." Then
If (GetAttr(Directory & MyFolder) And vbDirectory) = vbDirectory Then
fs.DeleteFolder Directory & MyFolder, True
End If
End If
MyFolder = Dir
Loop
Set fs = Nothing
End Sub

Swi
 
Thanks Glasgow and Swi,

I used the first code block, since it will delete a folder and its contents, but did not use the 'True' option.

Dim fso As FileSystemObject

should be:
Dim fso

I will never use Kill or RmDir ever again.
 
As this is the VB6 forum, I assume we're not talking about VBScript.

BrendanDeTracey said:
Dim fso As FileSystemObject

should be:
Dim fso

Why? In general I would always type a variable, rather than leave it as a variant. If there is some very specific reason that you want to be a Variant, you should declare it explicitly as Variant

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
johnwm,

I am stuck with VB5 in which:
Dim fso As FileSystemObject

causes the error:
Compile Error: User-defined type not defined.
 
Ah, yes - you'll want to add the relevant reference first...(Microsoft Scripting Runtime, in case you were wondering)

 
I should have made the comment more apparent.
'***********************************************
' Add a reference to Microsoft Scripting Runtime
'***********************************************

' Add a reference to Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim fso As FileSystemObject
Dim CMC_Path As String
Dim Path(0) As String
Set fso = CreateObject("Scripting.FileSystemObject")
CMC_Path = "c:\iofs_pom\cmc\"
Path(0) = "2004010112"
Call Clean_directory(CMC_Path & Path(0) & "\")
If fso.FolderExists(CMC_Path & Path(0)) Then fso.DeleteFolder CMC_Path & Path(0), True
Set fso = Nothing
MsgBox "Complete!", vbInformation
End Sub

Private Sub Clean_directory(Directory As String)
Dim MyFolder As String
Dim fs As FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
MyFolder = Dir(Directory, vbDirectory)
Do While MyFolder <> ""
If MyFolder <> "." And MyFolder <> ".." Then
If (GetAttr(Directory & MyFolder) And vbDirectory) = vbDirectory Then
fs.DeleteFolder Directory & MyFolder, True
End If
End If
MyFolder = Dir
Loop
Set fs = Nothing
End Sub

Swi
 
I have added Reference to MSR. Thanks for suggestion.

How about this shorter alternative for Sub Clean_directory?
Are variables fileList and File both type FileSystemObject?

Code:
Public Sub Clean_directory(Directory As String)
Dim fso As FileSystemObject, fileList As FileSystemObject, File As FileSystemObject

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fileList = fso.GetFolder(Directory)
    For Each File In fileList.Files
     File.Delete
    Next

End Sub
 
Addendum:

Code:
Public Sub Clean_directory(Directory As String)
Dim fso As FileSystemObject, fileList As Folder,
   _singleFile As File

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fileList = fso.GetFolder(Directory)
    For Each singleFile In fileList.Files
     singleFile.Delete
    Next

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top