PDA

View Full Version : Error after RmDir !!



anthosss
10-05-2017, 12:01 AM
Hello,

I am inside a folder doing the following:
1- i am checking if a folder named "no_links" exists or not
2- if folder "no_links" doesn't exist i am simply creating the folder
3- if folder "no_links" exists, i am checking if it contains any files. If there are files so i delete them then i delete the folder and i try to create again the folder named "no_links" and here i get the error "Run-time error '75': path/file access error"

I click on the end then i must go to the main folder where the folder "no_links" still showing and i must do a refresh ao the folder "no_links" disapear to be able to create it again.

Here is my code:


Dim strPath as String
strPath = ThisWorkbook.Path & "\"

Dim strFld as String
strFld = strPath & "no_links\"

Dim oFld as Object
Dim oFle as Object

If Dir(strFld, vbDirectory) <> "" Then
Set oFld = CreateObject("Scripting.FileSystemObject").GetFolder(strFld)

If Dir(strFld & "\*") <> "" Then
For Each oFle in oFld.Files
Kill(oFle.Path)
Next
End If

Set oFle = Nothing
Set oFld = Nothing

RmDir (strFld)
MkDir (strFld) 'on this command line i get the error
Else
MkDir (strFld)
End If


Thank you

mdmackillop
10-05-2017, 01:09 AM
Stolen from here (https://stackoverflow.com/questions/25401789/remove-directory-and-its-contents-files-subdirectories-without-using-filesys)

Sub Test2()
strPath = ThisWorkbook.Path & "\"
dirstr = strPath & "no_links"
On Error Resume Next
If Right(dirstr, 1) <> "\" Then dirstr = dirstr & "\"
Kill dirstr & "*.*"
RmDir dirstr
MkDir dirstr
On Error GoTo 0
End Sub

Bob Phillips
10-05-2017, 01:11 AM
Keep it simple, assume all instances met and deal with tha


Dim strPath As String
Dim filename As String
strPath = ThisWorkbook.Path & "\no_links\"

filename = Dir(strPath & "*.*", vbNormal)
Do While filename <> ""

Kill strPath & filename
filename = Dir()
Loop

On Error Resume Next
RmDir strPath
On Error GoTo 0

MkDir strPath

snb
10-05-2017, 01:53 AM
Simple ?


Sub M_snb()
c00 = "G:\OF\no_link"

With CreateObject("scripting.filesystemobject")
If .FolderExists(c00) Then .deletefolder c00
.CreateFolder c00
End With
End Sub

But why deleting/creating a folder over and over again ?

anthosss
10-07-2017, 12:44 AM
Sorry for my late reply

I will try the above mentioned methods and refer back

Thank you everyone