Monday, February 15, 2016

Create Nested Directory in Visual Foxpro

This program allows you to create nested directories in one call. It is written completely in VFP and works in VFP 6,7,8,9. Author: Eugene Vital  Freeware 3.4K Last updated: 2005.05.26

*:Example:

* on a network share
CreateDirectoryX("\\Directory1\Directory2\Directory3")

* on a local/mapped drive
CreateDirectoryX("C:\Directory1\Directory2\Directory3\")

* on the VFP default drive
CreateDirectoryX("\Directory1\Directory2\Directory3")

*:SeeAlso:

*:Remarks:

If the user running this function doesn't have sufficient privileges the directories will NOT be created

*********
* Code    *
*********
#define VFP6 600
#define VFP7 700
#define VFP8 800
#define VFP9 900

LPARAMETERS tcDirectoryString as String

LOCAL lcPrefix as string ,;
            lcDirectoryString as String ;
            liDirectoryCount as Integer ,;
            _xx_ as Integer ,;
            lcOnError as String


    * VFP version specific   
    #if VERSION(5) < VFP7
        PRIVATE ARRAY laDirectories[1] as String
    #else
        LOCAL ARRAY laDirectories[1] as String
    #endif            



    * copy the passed parameter to working variable
    lcDirectoryString  = tcDirectoryString

    * determin what kind of path we are working with
    DO CASE
        && Local Drive  C:, D: etc...
        CASE ISALPHA(lcDirectoryString)   
          lcPrefix = LEFT(lcDirectoryString,3)
          lcDirectoryString = SUBSTR(lcDirectoryString, 4)
       
        && Network Share
        CASE LEFT(lcDirectoryString,2) = "\\"   
            lcPrefix = "\\"   
             lcDirectoryString = SUBSTR(lcDirectoryString, 3)
       
        && Current Drive, must be last case statement because a network share would match this also
        CASE LEFT(lcDirectoryString,1) = "\"   
             lcPrefix = "\"
            lcDirectoryString = SUBSTR(lcDirectoryString, 2)
    ENDCASE



    * now we should be left with a directory listing like such
    * dir1\dir2\dir3\etc....
    * now we can build an array of directories to create

    * VFP version specific   
    #if VERSION(5) = VFP6
        lcDirectoryString = STRTRAN( lcDirectoryString, "\", CHR(10))
        liDirectoryCount =  ALINES(laDirectories, lcDirectoryString)
    #else
      #if VERSION(5) < VFP9
            liDirectoryCount =  ALINES(laDirectories, lcDirectoryString, .t., "\")
        #else
            liDirectoryCount =  ALINES(laDirectories, lcDirectoryString, 5, "\")
        #endif    
    #endif

    IF liDirectoryCount  > 0
       
       
       
        lcDirectoryString = "" && reusing this variable

        FOR _xx_ = 1 TO liDirectoryCount

          IF _xx_ > 1
                lcPrefix ="\"
          ELSE
            * leave it as it was for the first directory
          ENDIF
               
          * add the prefix back to the directory
          lcDirectoryString = lcDirectoryString + lcPrefix + ALLTRIM(laDirectories[_xx_])

            * VFP version specific   
          #if VERSION(5) < VFP8
              lcOnError = ON("ERROR")
              ON ERROR *
              MD (lcDirectoryString)
              ON ERROR &lcOnError
          #else
             
              TRY
                  MD (lcDirectoryString)
              CATCH
                 * just ignore it since we aonly care if the final directory actually exists
              ENDTRY
          #endif

        NEXT _xx_
    ENDIF
RETURN DIRECTORY( tcDirectoryString ) && check to see if th original folder exists  





No comments:

Post a Comment