![]() |
|
#1
|
|||
|
|||
|
Hi. All I'm trying to do is this:
a) The user selects the parent directory. b) The program loops through the parent directory and any subdirectories, deleting all Word files (both .doc and .docx) as it goes. c) The remaining files live happily ever after. I googled around a bit, and found some similar situations, but not exactly what I need, and I haven't succeeded in modifying anything I've found. Thanks for your attention. |
|
#2
|
||||
|
||||
|
Try:
Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String
Sub KillDocuments()
Application.ScreenUpdating = False
Dim TopLevelFolder As String
Dim TheFolders As Variant, aFolder As Variant
Dim strFile As String, i As Long
TopLevelFolder = GetFolder
StrFolds = vbCr & TopLevelFolder
If TopLevelFolder = "" Then Exit Sub
If FSO Is Nothing Then
Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
strFile = Dir(CStr(Split(StrFolds, vbCr)(i)) & "\*.doc", vbNormal)
Do While strFile <> ""
Kill CStr(Split(StrFolds, vbCr)(i)) & "\" & strFile
strFile = Dir()
Loop
Next
Application.ScreenUpdating = True
End Sub
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
RecurseWriteFolderName (SubFolder)
Next
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Thanks, that seems to work great.
How does it also find .docx files in this case? |
|
#4
|
||||
|
||||
|
Because the code doesn't care what's after .doc in the extension. Accordingly the code would also delete .docm files. If that's a problem, an extension test could be introduced to exclude those.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
Ok, thank you, this appears to be exactly what I need. I really appreciate your time.
|
|
#6
|
||||
|
||||
|
Paul has done a good job which but there would be a simpler way that cuts down on the code looping through subfolders.
There are shell (command prompt) commands that can do the iterative deletion more simply. Refer to Recursively Delete a Specific File Type from all Subfolders | SumTips for an example of one line deleting all files in subfolders. Running Shell commands in VBA https://www.myonlinetraininghub.com/...mand%20Prompt. You would most likely still want Paul's folder selection code.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#7
|
||||
|
||||
|
Yes, I have used Shell code myself (see https://www.msofficeforums.com/142257-post5.html). I prefer to avoid the screen flickering, though.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#8
|
|||
|
|||
|
Running the code on a series of nested folders, many Word files were deleted, but eventually there was a Run-time error 53 on:
Code:
Kill CStr(Split(StrFolds, vbCr)(i)) & "\" & strFile I have looked at the folder where it stopped, and I don't see anything unusual about the files there. I ran the program again, this time aiming it only at the problem directory, and it worked fine. I wish I could tell you the names of the files or directories, but unfortunately that's not possible. |
|
#9
|
||||
|
||||
|
Run-time error 53 typically means the file concerned was not found. That may be just the result of a timing issue.
One could precede: Code:
Kill CStr(Split(StrFolds, vbCr)(i)) & "\" & strFile Code:
On Error Resume Next
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Tags |
| delete document, loop folders |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Search in Nested Tables to Delete Rows
|
JingleBelle | Word VBA | 6 | 11-13-2020 07:36 AM |
| Recent files not showing up unless they're in the default save directory | jderouen | Word | 0 | 05-12-2017 06:51 AM |
How to Loop in directory to work with all files
|
PRA007 | Word VBA | 2 | 01-17-2016 09:04 PM |
| Copy Files to a directory | elmnas | Word VBA | 8 | 07-11-2014 12:07 AM |
| Weird problem - word docs are producing temp files that won't self delete | David92595 | Word | 0 | 07-07-2011 04:07 PM |