|
-
Jan 10th, 2007, 09:41 AM
#1
Thread Starter
Addicted Member
auto create missing folder when moving file
here's my problem
i want to move the file
"c:\test\myfolder\myfile.txt" to "d:\test\myfolder\myfile.txt"
but the folder "d:\test\myfolder\" doesn't exist...
-
Jan 10th, 2007, 09:46 AM
#2
Re: auto create missing folder when moving file
Use this API before your copy
vb Code:
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
(ByVal lpPath As String) As Long
Private Sub cmdCopyFiles_Click
MakeSureDirectoryPathExists "d:\test\myfolder\"
FileCopy "c:\test\myfolder\myfile.txt", "d:\test\myfolder\myfile.txt"
End Sub
The API will create the folder if it doesn't already exist, and do nothing if it does.
Last edited by Hack; Feb 28th, 2007 at 02:54 PM.
-
Jan 10th, 2007, 09:49 AM
#3
Thread Starter
Addicted Member
Re: auto create missing folder when moving file
wow cool!!! thanks very much and very quick reply!!!
edit:
it doesn't seem to work... the missing folders are not created.... 
edit2:
ok just saw ur post below it works great with "\" at the end !!! thanks again!
Last edited by lplover2k; Jan 10th, 2007 at 10:08 AM.
-
Jan 10th, 2007, 09:53 AM
#4
Re: auto create missing folder when moving file
No problem.
One last comment. Even though I've used that API about a zillion times I still get caught in a "gotcha" with it. If you don't include the last, trailing, backslash, the API won't work.
In other words "d:\test\myfolder\" will result in the folder being created, if it doesn't exist, or ignored if it does. However, "d:\test\myfolder" will result in the folder NOT being created, but, you won't know it until your copy blows up. Without the trailing backslash the folder won't be created, and you won't receive an error message.
Also, If you consider this resolved, would you please pull down the Thread Tools menu and click the Mark Thread Resolved menu item? That will let everyone know that you have your answer.
Thank you.
-
Feb 28th, 2007, 02:10 PM
#5
Frenzied Member
Re: auto create missing folder when moving file
I have used the code Hack suggested here...
vb Code:
MakeSureDirectoryPathExists App.Path & "\" & tvwCode.SelectedItem
but it doesn't create a new folder when the tvwCode.SelectedItem folder does not exist.
Anybody see why?
Thanks
-
Feb 28th, 2007, 02:13 PM
#6
Hyperactive Member
Re: auto create missing folder when moving file
Try it like this:
vb Code:
MakeSureDirectoryPathExists App.Path & "\" & tvwCode.SelectedItem & "\"
-
Feb 28th, 2007, 02:13 PM
#7
Re: auto create missing folder when moving file
 Originally Posted by aikidokid
Anybody see why?
....
 Originally Posted by Hack
One last comment. Even though I've used that API about a zillion times I still get caught in a "gotcha" with it. If you don't include the last, trailing, backslash, the API won't work.
In other words "d:\test\myfolder\" will result in the folder being created, if it doesn't exist, or ignored if it does. However, "d:\test\myfolder" will result in the folder NOT being created, but, you won't know it until your copy blows up. Without the trailing backslash the folder won't be created, and you won't receive an error message.
-
Feb 28th, 2007, 02:15 PM
#8
Frenzied Member
-
Mar 19th, 2007, 10:42 PM
#9
Hyperactive Member
Re: auto create missing folder when moving file
Hack, is there a quick way to use this and intercept whether the path did not exist and do a yes/no message box to approve the creation?
used this in my current project and really saved time.
If you find information helpful from any member, please take a second and rate their post. Its a nice gesture of your appreciation.
"I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
Do illiterate people get the full effect of Alphabet Soup?
ADO FAQ 2005-2008 Masked Textbox Patch FoxPro Date MZ Tools Great Free Tool
-
Mar 20th, 2007, 07:46 AM
#10
Re: auto create missing folder when moving file
Sure....
Code:
Dim mbResponse As VbMsgBoxResult
If Dir$("d:\test\myfolder", vbDirectory) = vbNullString Then
mbResponse = MsgBox("The folder does not exist. Would you like me to create it for you?", vbYesNo + vbQuestion, "Create Folder")
If mbResponse = vbYes Then
MakeSureDirectoryPathExists "d:\test\myfolder\"
End If
End If
Last edited by Hack; Mar 20th, 2007 at 08:00 AM.
Reason: Forgot the part about checking for the existence of the folder.
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
|