Create a directory(-tree)

[Autolink] Menu

 
/* ------------------------------------------------------------------ */
/* function: Create a directory(tree)                                 */
/*                                                                    */
/* call:     CreateDirectory dirToCreate                              */
/*                                                                    */
/* where:    dirToCreate - directory to create                        */
/*                                                                    */
/* example:  call dirToCreate C:\TEST1\TEST2\TEST3\TEST4              */
/*           will create the directories                              */
/*             C:\TEST1                                               */
/*             C:\TEST1\TEST2                                         */
/*             C:\TEST1\TEST2\TEST3                                   */
/*           and                                                      */
/*             C:\TEST1\TEST2\TEST3\TEST4                             */
/*                                                                    */
/* returns:  0 - okay                                                 */
/*           else OS Error                                            */
/*                                                                    */
CreateDirectory: PROCEDURE
  parse arg dirToCreate

                        /* file or device for messages                */
  prog.__LogAll = "2>NUL 1>NUL"

                        /* init the return code                       */
  thisRC = -1
                        /* check if the drive is ready                */
  SIGNAL ON NOTREADY Name CreateDirectoryError
  call stream fileSpec( "drive", dirToCreate ) || "\*"

  thisRC = 0
                        /* save the current directories               */
  curDir = directory()
  curDir1 = directory( fileSpec( "drive", dirToCreate ) )

  newDir = translate( dirToCreate, "\", "/" )

  i = pos( ":", dirToCreate )
  if i <> 0 then
  do
    parse var dirToCreate lwForTheDir ":" dirToCreate
    if directory( lwForTheDir || ":\" ) = "" then
      thisRC = 1
  end /* if i <> 0 then */

  if thisRC = 0 then
  do
    if right( dirToCreate, 1 ) <> "\" then
      dirToCreate = dirToCreate || "\"

    do until dirToCreate = "" | thisRC <> 0
      parse var dirToCreate newSubDir "\" dirToCreate
      dirToCreate = strip( dirToCreate )

      if newSubDir = '' then                                 /* v3.20 */
        iterate                                              /* v3.20 */

      if directory( newSubDir ) = "" then
      do
        '@md "' || newSubDir || '"' prog.__LogAll            /* v3.00 */
        if rc = 2 | rc = 1 then
        do
          if stream( newSubDir , "c", "QUERY EXISTS" ) <> "" then
            thisRC = rc
        end /* if rc = 2 | rc = 1 */
        else
          thisRC = rc

        if thisRC = 0 then
          call directory newSubDir
      end /* if directory( newSubDir ) = "" then */
    end /* do until dirToCreate = "" | thisRC <> 0 */
  end /* if thisRC = 0 then */

                        /* restore the current directories            */
  call directory curDir1
  call directory curDir

CreateDirectoryError:

RETURN thisRC


[Back: Work on directory trees]
[Next: Delete a directory(-tree)]