Saturday, December 28, 2013

MKDirs - Create Folder and Missing Sub-Folders in one go!

Well I posted this inside foxite last time as a fellow member is wondering how it can be done.   Suppose you want to create a folder and several sub-folders like this?

c:\mine\sub1\sub2\my sub3\sub4\sub sub\ too deep or not \how.about now\maybe this time

MD will fail in some of those sub-folders with spaces or dots, or in other words it will be able to create sub-folders only up to a certain extent.

This simple function I named MKDirs fixes that problem using only stock VFP commands and functions.

* Author: Jun Tangunan
* Date: December 19, 2013
Function MKDirs(lcFolder,llUNC)
Local lnSub, lnLoop, lcDir
lnSub = Getwordcount(m.lcFolder,'\')
lcDir = ''
For lnLoop = 1 To m.lnSub
      lcDir = m.lcDir + Getwordnum(m.lcFolder,m.lnLoop,'\')+'\'
      If m.llUNC
            lcDir = '\\'+lcDir
            llUNC = .F.
            Mkdir (m.lcDir)

To use this is something like this:

Local lcFolder
lcDir = 'c:\one\two\three\ sample \with. space'
If !Directory(m.lcFolder,1)

Or for a UNC path, pass .T. to the second parameter

Local lcFolder
lcDir = '\\shornsrvr\fcms\one\two\three\ sample \with. space'
If !Directory(m.lcFolder,1)



  1. Hello, Jun. I don't have any problem with MD or MkDir commands. I am using VFP9 SP2.

    Belated Merry Christmas and a Happier New Year to you and to your family.

    - Dale Dedoroy

  2. Hello Dale, I am using VFP with no SP, so I don't know if this is a SP issue. Try these on your end:

    MD c:\one\two\three\ sample \with. space
    MD 'c:\one\two\three\ sample \with. space'
    In my end, that creates only up to sub-folder three. Now try that using the function MKDirs I shared here. It creates all those sub-folders

    Happy New Year to you and your family too!

    - Jun