Results 1 to 10 of 10

Thread: auto create missing folder when moving file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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...

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: auto create missing folder when moving file

    Use this API before your copy
    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
    4. (ByVal lpPath As String) As Long
    5.  
    6. Private Sub cmdCopyFiles_Click
    7. MakeSureDirectoryPathExists "d:\test\myfolder\"
    8. FileCopy "c:\test\myfolder\myfile.txt", "d:\test\myfolder\myfile.txt"
    9. 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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: auto create missing folder when moving file

    I have used the code Hack suggested here...
    vb Code:
    1. 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
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  6. #6
    Hyperactive Member Capp's Avatar
    Join Date
    May 2005
    Location
    Texas
    Posts
    409

    Re: auto create missing folder when moving file

    Try it like this:

    vb Code:
    1. MakeSureDirectoryPathExists App.Path & "\" & tvwCode.SelectedItem & "\"
    AmazingAntivirus.com
    Remote Data Backups


    Please Mark your Thread "Resolved", if the query is solved...

    If a post has helped you then Please Rate it!

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: auto create missing folder when moving file

    Quote Originally Posted by aikidokid
    Anybody see why?
    ....
    Quote 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.

  8. #8
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: auto create missing folder when moving file

    And I just read that as well!

    Time for a break me thinks
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  9. #9
    Hyperactive Member Always_Confused's Avatar
    Join Date
    Jun 2006
    Location
    Alabama USA
    Posts
    417

    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

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width