PDA

View Full Version : Sleeper: How to remove a dir which already contains a dir



Harrij
06-29-2005, 01:21 PM
How do i remove a dir which already contains a dir.
I empty the main dir with MyFile and the Kill statement but it wont Kill the Dir which is also in the main dir.

Also the renaming is diffycult as i can't get access to the dir for renaming.

Any help is welcome
:banghead: :help

Jacob Hilderbrand
06-29-2005, 01:29 PM
Try something like this.

Set a reference to the Microsoft Scripting Runtime (Tools | References).



Option Explicit

Sub DelFolder()
Dim FSO As FileSystemObject
Dim F As Folder
Set FSO = New FileSystemObject
Set F = FSO.GetFolder("C:\MyDir")
F.Delete
End Sub

Harrij
06-29-2005, 02:02 PM
Is it possible to save the folder(dir) in the same name as the original ??
how do i go about this as I get a message that the folder(dir) already exists

a save.as statement wont work as you know

Jacob Hilderbrand
06-29-2005, 02:06 PM
Can you post the code you are using now?

Harrij
06-29-2005, 02:11 PM
I will copy the code from my work
I am running a full VBA automisation project in order to automate our laboratory
my brain is exploding after two years of work
some fine tunings are still missing as I just finished my study in this area at the age of 52
thanks if you can help me out

Harrij
06-30-2005, 09:40 AM
I tried these two solutions. Neither work.



Sub KILL_DIR()
'de var4 = *.d directory wordt gerenamed naar een standaard naam
'var4 = "7.d"
MyFile = Dir("D:\DATA5\" & var4 & "\") ' var4 = bv folder 1.d
For I = 1 To 20
If MyFile <> "" Then
Kill "D:\DATA5\" & var4 & "\" & MyFile
MyFile = Dir()
If MyFile = "" Then
I = 21
End If
End If
Next I
On Error Resume Next
RmDir ("D:\DATA5\" & var4 & "") ' The folder still has a folder 'inside. RmDir doesn't work this way
var4 = ""
'------------------------------------------
this is the second tryout !!!
'-------------------------------------------
'ChDrive "D"
'gezocht wordt naar dir \D:\DATA5\" & var4 &
'MyOutputLocation = "D:\DATA5\" & var4 & ""
'PathExists = VBA.Dir(PathName:=MyOutputLocation, Attributes:=vbDirectory)
'If PathExists = UCase(var4) Then
'Application.DisplayAlerts = False
'
'OldName = "D:\DATA5\" & var4 & "": NewName = "D:\DATA5\OldDirs" ' Define file names."
'On Error Resume Next
'Name OldName As NewName ' Rename file var4 *.d dirs.
'End If ' This doesn't work as the old and newname ae the same. 'Which is needed.
End Sub

Zack Barresse
06-30-2005, 02:28 PM
Moved from the Pay-for-Answers to the Excel Help forum. :)

Killian
06-30-2005, 02:55 PM
I would also recommend the Microsoft Scripting Runtime's FileSystemObject for this kind of work.
The MSDN library has a comprehensive section on it (as one might expect) here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjfilesystem.asp)

You could play with this code to do what you want, I think


Option Explicit
'!!!Set a reference to the Microsoft Scripting Runtime (Tools>References)!!!
Const strTARGET_PATH As String = "C:\TEMP\TESTFOLDER"

Sub MakeTree()
'you can create mutli-nested indexed directory structures
Dim fso As FileSystemObject
Dim targetfldr As Folder
Dim fldr_level_1 As Folder
Dim fldr_level_2 As Folder
Dim f As File
Dim a As Long
Dim b As Long
Set fso = New FileSystemObject
Set targetfldr = fso.CreateFolder(strTARGET_PATH)
For a = 1 To 20
Set fldr_level_1 = fso.CreateFolder(targetfldr.Path & Application.PathSeparator & "SUBTEST_1_" & a)
For b = 1 To 10
Set fldr_level_2 = fso.CreateFolder(fldr_level_1.Path & Application.PathSeparator & "SUBTEST_2_" & b)
Next
Next
End Sub

Sub DeleteTree() ' a nice old-fashioded name for a routine :-)
'DeleteFolder doesn't care if there's something inside
Const strMSGTITLE As String = "Confirm folder delete"
Const strMSGTEXT As String = "Confirm deletion of folder"
Dim fso As FileSystemObject
Dim targetfldr As Folder
Dim msgboxresponse As Integer
Set fso = New FileSystemObject
If fso.FolderExists(strTARGET_PATH) Then
Set targetfldr = fso.GetFolder(strTARGET_PATH)
msgboxresponse = MsgBox(strMSGTEXT & vbLf & targetfldr.Path, _
vbOKCancel, strMSGTITLE)
If msgboxresponse = vbOK Then
fso.DeleteFolder (targetfldr.Path)
End If
End If
End Sub