|
-
Apr 11th, 2001, 09:48 PM
#1
Thread Starter
PowerPoster
How do you delete all files underneath a folder but not the folder?
-
Apr 11th, 2001, 09:50 PM
#2
Get the folders name, delete the folder, and then create a new folder with a same name.
-
Apr 11th, 2001, 09:55 PM
#3
Thread Starter
PowerPoster
o.k i've got:
srce = "c:\test"
Kill (srce)
End Sub
But that don't work coz it's a folder!
How should it be?
-
Apr 11th, 2001, 09:59 PM
#4
Fanatic Member
Try this:
Code:
Private Sub Command1_Click()
DeleteFiles ("c:\tmp\tmp")
End Sub
Function DeleteFiles(Path As String)
Dim File As String
If Right(Path, 1) <> "\" Then Path = Path & "\"
If Len(Dir$(Path, vbDirectory)) <> 0 Then
File = Dir$(Path, vbNormal + vbHidden + vbSystem)
Do While Len(File)
MsgBox File
Kill (Path & File)
File = Dir
Loop
End If
End Function
-
Apr 11th, 2001, 10:02 PM
#5
Fanatic Member
If you want to delete the folder without fso including subdirectories and optionally send to the recycle bin:
In bas:
Code:
Option Explicit
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FOF_NOCONFIRMATION = &H10
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Function jKill(Path As String, Optional Recycle As Boolean = False) As Boolean
'By James Mayo
'Delete file Or Folder And optionally send To recycle bin
If Len(Dir$(Path)) <> 0 Or Len(Dir$(Path, vbDirectory)) <> 0 Then
Dim SHFileOp As SHFILEOPSTRUCT
With SHFileOp
'Delete the file
.wFunc = FO_DELETE
'Select the file
.pFrom = Path
.fFlags = FOF_NOCONFIRMATION
'Allow 'move To recycle bn'
If Recycle Then .fFlags = .fFlags Or FOF_ALLOWUNDO
End With
'perform file operation
SHFileOperation SHFileOp
jKill = True
End If
End Function
For example, killing directory and sending to recycle bin:
Code:
Private Sub Command1_Click()
jKill "c:\tmp\tmp", True
End Sub
-
Apr 11th, 2001, 10:04 PM
#6
You don't necessarily have to use Kill to delete the folder. You could use:
Code:
Declare Function RemoveDirectory Lib "kernel32.dll" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long
Private Sub Command1_Click()
Dim strDirName As String
strDirName = "C:\WINDOWS"
RemoveDirectory strDirName
MkDir strDirName
End Sub
Very quick and easy. Just remember to change the directory name.... 
I think mine is a lot easier James!!!
-
Apr 11th, 2001, 10:15 PM
#7
Thread Starter
PowerPoster
Dreamlx:
I have a problem a get an error "Path/File access error"
why is this?
I'll try jamesm's !
thanks
-
Apr 11th, 2001, 10:17 PM
#8
Thread Starter
PowerPoster
Dreamlax I have this so far:
Private Sub Command2_Click()
srce = "c:\test"
Dim strDirName As String
strDirName = srce
RemoveDirectory strDirName
MkDir strDirName
end sub
-
Apr 11th, 2001, 10:21 PM
#9
Remeber to put the API call in your form or module!
Code:
'Place the following line in the General Declarations, or if you put it in a module, make it public.
Private Declare Function RemoveDirectory Lib "kernel32.dll" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long
Private Sub Command2_Click()
Dim srce As String
srce = "c:\test"
RemoveDirectory srce
MkDir srce
End Sub
-
Apr 11th, 2001, 10:24 PM
#10
Thread Starter
PowerPoster
Cool thanks jamesm and dreamlax!
Works a charm
-
Apr 11th, 2001, 10:29 PM
#11
-
Apr 11th, 2001, 10:35 PM
#12
Thread Starter
PowerPoster
-
Apr 11th, 2001, 10:38 PM
#13
Phew, I was about to throw an aggro because it works on my computer and I deleted the folder with all the games in it by mistake. There was only 3 really old 486 gamesa so I'm not mad.
-
Apr 11th, 2001, 10:38 PM
#14
Fanatic Member
Dreamlax,
RemoveDirectory API only works where the folder contains no subdirectories and no files, else it will fail. Why not just use rmdir instead? Additionally it has no ability to send to recycle bin.
Last edited by JamesM; Apr 11th, 2001 at 11:00 PM.
-
Apr 11th, 2001, 10:53 PM
#15
Thread Starter
PowerPoster
-
Apr 11th, 2001, 10:56 PM
#16
strange, it deleted the games folder. Unless that was your code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|