Deleting and Re-Creating a folder

P

Peter Hallett

I am trying to use the DeleteFolder Method to delete a folder, called, say,
"C:\FolderName", followed by CreateFolder to recreate it. I have used Help
to determine the appropriate syntax but seem to be having no success in
getting the compiler to accept my efforts. Would someone be kind enough to
enlighten me?
 
D

Douglas J. Steele

There's no need for to bring FSO into the equation: VBA has built-in
commands to do this:

'delete the folder
RmDir "C:\mytester"
MsgBox "folder deleted", vbOKOnly

'create the folder
MkDir "C:\mytester"
 
P

Peter Hallett

Thank you. That clears it up nicely. The trouble with compilers is that
they are ot very sympathetic if you get it 'nearly right', as I did.

The one thing that still puzzles me, however, is what fs is being
dimensioned as.
 
P

Peter Hallett

And that is even easier! It would never have occurred to me to use DOS
commands in that way. Many thanks
 
D

Douglas J. Steele

Valid point.

In general, though, I never use FSO in conjunction with VBA, just with
VBScript.
 
P

Peter Hallett

Ah!, now there's an interesting fly in the ointment!

The reason I chose to delete the folder and recreate it was that it
generally contains a lot of files and it appeared easier to remove it and
then recreate it rather than try to delete all the files. However, when one
door closes another usually opens and it occurs to me that, if DOS commands
can be used directly, then a very simple way of achieving what I am
attempting to do is simply to write:-

Del "C:\FolderName\*.*

I don't then have to bother to delete and recreate folders. Obviously the
best way of testing this is to 'suck it and see', but if anyone has any
comments I would be pleased to read them.

One thing about this Discussion Group is that I always learn something to my
advantage (and, hopefully, to others) every time I pose a question.
 
P

Peter Hallett

I'll respond to my own suggestion, before anyone else does.

No, it apparently does not work. The VBA compiler regards Del as an
undefined Sub.

Ah well. Good try, but, unless anyone has any better suggestions, it looks
as if the whole thing has come full circle and it's back to DeleteFolder and
CreateFolder
 
S

Stuart McCall

Peter Hallett said:
I'll respond to my own suggestion, before anyone else does.

No, it apparently does not work. The VBA compiler regards Del as an
undefined Sub.

Ah well. Good try, but, unless anyone has any better suggestions, it
looks
as if the whole thing has come full circle and it's back to DeleteFolder
and
CreateFolder
<Snip>

As you discovered, Del isn't a VBA keyword. The equivalent is Kill. So to
remove a folder and all it's contents:

Dim fdr As String

fdr = "C:\temp"
Kill fdr & "\*.*" 'Delete all files
RmDir fdr 'Remove the (now empty) folder
 
R

RDub

Peter have you looked at the VB Kill Statement? It accepts wild cards and
will remove all of the files in a directory no fuss no muss. Kill
"C:\FolderName\*.*

I am in the same camp as Doug is. I would never use FSO in an Access
application. Any time you use non native stuff in an Access application you
are betting the farm on the computer having the component you need. If the
component is not there or was there but has been removed then your app comes
to a screeching halt There are IT departments aplenty that have either
removed or deactivated FSO from the computers in their domains, in effort to
foil the insertion and spread of malware.

In general my Modus Operandi has always been if there is an Access way to do
it that's how I do it. When there is no Access way to do it, I look at the
OS first, then and only then will look at importing other objects (like the
File Scripting Object, or other Office applications) into my application.
If you decide to stick with the FSO make sure you have adequate error
handling in your code to allow you app to die gracefully when it runs on a
computer without the component.

Rdub
 
P

Peter Hallett

As I say, the more you dig in this Discussion Group, the more nuggets you find!

I was unaware of Kill but I will certainly give it a try. Is it necessary
to append "\*.*" to fdr, by the way? Could it not be included in fdr as part
of the initial asignment?
 
P

Peter Hallett

Thanks, RDub. From my reply to Stuart McCall, you will see that I will take
on the suggestion of using Kill, now that I know it exists!

I go along with you about always using Access native utilities wherever
possible - not from any deep knowledge of Access but from bitter experience.
Access's motto seems to be, "I'll do it my way." All too frequently I have
found that attempts to make it do it differently end in failure.
 
S

Stuart McCall

Peter Hallett said:
As I say, the more you dig in this Discussion Group, the more nuggets you
find!

I was unaware of Kill but I will certainly give it a try. Is it necessary
to append "\*.*" to fdr, by the way? Could it not be included in fdr as
part
of the initial asignment?

Well if you said:

fdr = "C:\temp\*.*"

then yes, you could just use:

Kill fdr

but then you'd have to remove the "\*.*" in order to use it for the RmDir
statement's argument.

Simpler the way I posted, IMO. The choice is yours.
 
S

Stuart McCall

Peter Hallett said:
As I say, the more you dig in this Discussion Group, the more nuggets you
find!

I was unaware of Kill but I will certainly give it a try. Is it necessary
to append "\*.*" to fdr, by the way? Could it not be included in fdr as
part
of the initial asignment?

Well if you said:

fdr = "C:\temp\*.*"

then yes, you could just use:

Kill fdr

but then you'd have to remove the "\*.*" in order to use it for the RmDir
statement's argument.

Simpler the way I posted, IMO. The choice is yours.
 
P

Peter Hallett

You are right, of course.
--
Peter Hallett


Stuart McCall said:
Well if you said:

fdr = "C:\temp\*.*"

then yes, you could just use:

Kill fdr

but then you'd have to remove the "\*.*" in order to use it for the RmDir
statement's argument.

Simpler the way I posted, IMO. The choice is yours.
 
P

Peter Hallett

This one is not going to give up that easily.

It turns out that the folder containing the files to be deleted also
contains folders, which are also to be deleted. It appears that Kill
“C:\ParentFolder\*.*†only removes the files, leaving the folders and a
resulting problem of how to delete them. The task is made particularly
difficult by the fact that the names of the folders contained in ParentFolder
are, for all practical purposes, unknown. They are recreated every time a
new batch of work orders is processed and carry names submitted by outside
agencies. There can at times by 50 or more such folders and they all contain
files, making it impossible to use RmDir, even if the folder names were
known. Do correct me if I am wrong but, despite the objections, it appears
things might have gone full circle and brought us back to DeleteFolder as
perhaps the only viable solution.

Briefly, to reiterate the requirement, the objective is to empty a folder,
say FolderName, of its contents, whatever they may be. Either of two methods
would be appropriate. The first is simply to flush the folder, leaving the
resulting empty folder in place and the second is to delete the folder,
including its contents and then to recreate it.
 
P

Peter Hallett

(I posted this over an hour ago but the system appears to have consumed my
previous submission, so I’ll try again.)

This one is not going to give up that easily.

The folder containing the files to be deleted also contains folders, which
are also to be deleted. It appears that Kill “C:\ParentFolder\*.*†only
removes the files, leaving the folders and a resulting problem of how to
delete them. The task is made particularly difficult by the fact that the
names of the folders contained in ParentFolder are, for all practical
purposes, unknown. They are recreated automatically every time a new batch
of work orders is processed and carry names submitted by outside agencies.
There can at times be 50 or more such folders and they all contain files,
making it impossible to use RmDir, even if the folder names were known. Do
correct me if I am wrong but, despite the objections, DeleteFolder, followed
by CreateFolder, is beginning to look like the only viable solution.

Briefly, to reiterate the problem, the objective is to empty a folder, say
FolderName, of its contents, whatever they may be. Either of two methods
would be appropriate. The first is simply to flush the folder, leaving the
resulting empty folder in place and the second is to delete the folder,
including its contents and then to recreate it.
 
S

Stuart McCall

Peter Hallett said:
(I posted this over an hour ago but the system appears to have consumed my
previous submission, so I’ll try again.)

This one is not going to give up that easily.

The folder containing the files to be deleted also contains folders, which
are also to be deleted. It appears that Kill “C:\ParentFolder\*.*” only
removes the files, leaving the folders and a resulting problem of how to
delete them. The task is made particularly difficult by the fact that the
names of the folders contained in ParentFolder are, for all practical
purposes, unknown. They are recreated automatically every time a new
batch
of work orders is processed and carry names submitted by outside agencies.
There can at times be 50 or more such folders and they all contain files,
making it impossible to use RmDir, even if the folder names were known.
Do
correct me if I am wrong but, despite the objections, DeleteFolder,
followed
by CreateFolder, is beginning to look like the only viable solution.

Briefly, to reiterate the problem, the objective is to empty a folder, say
FolderName, of its contents, whatever they may be. Either of two methods
would be appropriate. The first is simply to flush the folder, leaving
the
resulting empty folder in place and the second is to delete the folder,
including its contents and then to recreate it.
<SNIP>

This function will empty all your folders and subfolders:

Sub EmptyFolderTree(ByVal Path As String)
Dim s As String
Dim thisDir As String
Dim dirList As New Collection

If Right$(Path, 1) <> "\" Then Path = Path & "\"
dirList.Add Path
Do While dirList.Count
'Remove this directory from directory list
thisDir = dirList.item(1)
dirList.Remove 1
'Find all files and folders in current & add to list
s = Dir$(thisDir, vbDirectory)
Do While Len(s)
If Asc(s) <> 46 Then 'Exclude "." and ".."
If GetAttr(thisDir & s) = vbDirectory Then
dirList.Add thisDir & s & "\" 'Add the folder
Else
Debug.Print thisDir & s
End If
End If
s = Dir$
Loop
Loop
Set dirList = Nothing
End Sub

Replace Debug.Print with Kill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top