Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving folders and contents in Excel 2007 VBA

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
US
I'm quessing that there is no simple way in Excel 2007 VBA to move a folder and all it's contents (including all potential files and sub-folders) to a new location as a folder under an existing, user-defined folder. I found the writeup on Tek Tips about the MoveFolder function, but that assumes that the destination folder does not already exist. Thanks for any help,
Paul Hudgens
Denver
 
A link to what you already have, or a copy of the code would help.

I am sure this can be done with the FSO (FileSystemObject).
thread707-268381

Alternatively, a very simple approach would be to attempt to create the folder each time.
Code:
on error resume next
MkDir "C:\Temp\mytest"
on error goto 0[/code

Gavin
 
This shows a way to check for the folder's existence prior to creating the directory:

Rather than rewriting it, I think ed_metcalfe's answer there looks good. Hmm, that name looks familiar. Seems I've seen that name 'round these parts a time or three.

Yep, he's here on Tek-Tips... I'm just assuming it's the same person:
 
Thanks for the input. Here's what I have ended up with:

/code

CopyFolder = OutPutFolder & ThisWellFolder
Set Fso = Nothing
Set Fso = CreateObject("Scripting.FileSystemObject")
If Fso.FolderExists(CopyFolder) Then GoTo Nexti
Fso.CopyFolder ThisWellPath, OutPutFolder, False

Set Fso = Nothing
Set Fso = CreateObject("Scripting.FileSystemObject")
If Fso.FolderExists(ThisWellPath) Then
On Error Resume Next
Fso.deletefolder ThisWellPath & "\*"
End If
Set Fso = Nothing

Set Fso = CreateObject("Scripting.FileSystemObject")
If Fso.FolderExists(ThisWellPath) Then
On Error Resume Next
RmDir (ThisWellPath)
End If
Set Fso = Nothing

code/

The CopyFolder method works correctly. The DeleteFolder method does not. I also tried: "Kill ThisWellPath & "\*" with no luck. Anyone know why I can't get the delete to work? Thanks.

And yes - I know that this is not the correct way to post code, but damned if I can find out what the proper annotations are.
 
After further investigation, I have come to the conclusion that the FolderDelete method is useless. It will delete some of the files and sub-folders in the folder that I specifiy to be deleted, but not all, even though I created ALL of the files and folders. I can't get anything to delete all of the files (including the Kill method).
 

phudgens said:
I know that this is not the correct way to post code, but damned if I can find out what the proper annotations are.

try:
[ignore]
Code:
Code goes here
[/ignore]
you get:
Code:
Code goes here


Have fun.

---- Andy
 
You won't be able to delete any files or folders that are still in use by the system. For example, if your code creates a text file it cannot be deleted until it is first closed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top