Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2021, 06:00 PM
mbcohn mbcohn is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Advanced Beginner
delete all word files in a nested directory
 
Join Date: Jan 2021
Posts: 32
mbcohn is on a distinguished road
Default delete all word files in a nested directory

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.
Reply With Quote
  #2  
Old 01-16-2021, 08:57 PM
macropod's Avatar
macropod macropod is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 01-16-2021, 10:56 PM
mbcohn mbcohn is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Advanced Beginner
delete all word files in a nested directory
 
Join Date: Jan 2021
Posts: 32
mbcohn is on a distinguished road
Default

Thanks, that seems to work great.


How does it also find .docx files in this case?
Reply With Quote
  #4  
Old 01-16-2021, 11:00 PM
macropod's Avatar
macropod macropod is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 01-16-2021, 11:28 PM
mbcohn mbcohn is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Advanced Beginner
delete all word files in a nested directory
 
Join Date: Jan 2021
Posts: 32
mbcohn is on a distinguished road
Default

Ok, thank you, this appears to be exactly what I need. I really appreciate your time.
Reply With Quote
  #6  
Old 01-17-2021, 04:07 PM
Guessed's Avatar
Guessed Guessed is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 01-17-2021, 04:17 PM
macropod's Avatar
macropod macropod is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 01-18-2021, 05:03 PM
mbcohn mbcohn is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Advanced Beginner
delete all word files in a nested directory
 
Join Date: Jan 2021
Posts: 32
mbcohn is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 01-18-2021, 06:45 PM
macropod's Avatar
macropod macropod is offline delete all word files in a nested directory Windows 10 delete all word files in a nested directory Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
with:
Code:
On Error Resume Next
but that might result in files being left behind. Running the macro a second time might me a safer option and would take little time.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
delete document, loop folders

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
delete all word files in a nested directory 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
delete all word files in a nested directory 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:29 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft