Announcement

Collapse
No announcement yet.

create a new folder if not exist

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • create a new folder if not exist

    Hallo

    I'm new to PB, I want to do the following :

    1. Check if folder c:\ASAP\xxtsys exist or not
    2. if not exist then create this new folder c:\ASAP\xxtsys
    3. if exist then continue with the rest of program

    how to program these steps?

  • #2
    You might want to look at the ISFOLDER command to determine if a folder exists, but depending on the depth you might need to ensure each parent exists until you get to the child.

    Better bet, just use the MakeSureDirectoryPathExists API call. Here is an example...

    Code:
    DECLARE FUNCTION MakeSureDirectoryPathExists LIB "IMAGEHLP.DLL" ALIAS "MakeSureDirectoryPathExists" (DirPath AS ASCIIZ) AS LONG
    
    '---------------------------------------------------------------------------(')
    
    FUNCTION PBMAIN( )
      LOCAL v_strFolderPath AS STRING
      v_strFolderPath = "c:\ASAP\xxtsys\" ' NOTE:Must end in a "\" for it to work!
      MakeSureDirectoryPathExists BYCOPY v_strFolderPath
    END FUNCTION
     
    I did not include any error checking in my brief example but, considering you are writing to the ROOT, you definitely will want to check the return value so you can determine if the directory creation actually happen. This is especially important if the rights of the person running your application may not have the appropriate privileges to write to the root.
    Last edited by George Bleck; 20 Mar 2017, 02:09 AM.
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

    Comment


    • #3
      Greetings ....

      About 10 years ago, I asked the same question and got the code shown below as a response
      It works in PBCC 4.xx and above. It does not require a special API call, at least not in my
      experience.Try it in different scenarios and see what happens.

      I concede this might be a little over-the-top but it is what I've been using for years,

      UPDATE -- If you are using the most recent versions of PBCC (6.04) or PBWIN (10.04) then follow advice of other posters

      Thanx-A-Lot and Good Luck, Frank

      Code:
       SUB FolderTest (Foldz AS LONG,ChekName AS STRING)
           LOCAL Test AS STRING
          ' See also FUNCTION FolderExist
      
           SELECT CASE Foldz
             CASE 1
                  Test="C:\Temp"
                  ' Check for existence of folder (Directory) TEMP on Drive C. Only need the one \ here
                  ' This is called from elsewhere in the program with CALL FolderTest(1,"") because
                  ' C:\Temp is a pre-set default here
      
             CASE 2
                  Test=ChekName
                  ' A user-created folder (directory) on the drive of their choice
                  ' The user types in the drive and directory elsewhere as a LINE INPUT string
                  ' and the call to here is CALL FolderTest(2,YourLineInputString)
                  ' Input example F:\MarkTyme (drive F:  folder MarkTyme)
      
                  IF LEN(Test)<6 THEN EXIT SUB:' foldername (following X:\  where X is drive)  to be at least 3 characters
      
             CASE ELSE: EXIT SUB
           END SELECT
      
           IF FolderExist(Test)=-1 THEN
           ' -1 is YES, it exists - no further action needed
           ELSEIF FolderExist(Test)=0 THEN
              MKDIR Test
           ' 0 is no, it does not exist, so create it
           END IF
      
       END SUB
      
      
       FUNCTION FolderExist(File$) AS LONG
         ' checks for existence of a folder on a specified drive.
         ' See also SUB FolderTest
      
         LOCAL Dummy&
         Dummy& = GETATTR(File$)
         FUNCTION = (ERRCLEAR = 0)
       END FUNCTION
      Last edited by Frank Ferrell; 20 Mar 2017, 02:44 PM.

      Comment


      • #4
        Try this


        Code:
        FolderName = "C:\ASAP\xxtsys\"
        IF ISFALSE ISFOLDER(FolderName) THEN
        MKDIR FolderName
        END IF
        SLEEP(10)

        Comment


        • #5
          Originally posted by Anne Wilson View Post
          Try this


          Code:
          FolderName = "C:\ASAP\xxtsys\"
          IF ISFALSE ISFOLDER(FolderName) THEN
          MKDIR FolderName
          END IF
          SLEEP(10)
          What happens if C:\ASAP\ is not a folder either?

          MKDIR:
          "
          If path$ includes an parent folder that does not exist, a run-time Error 76 occurs ("Path not found")."
          --
          [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
          PNG Domain Hosting[/URL]

          Comment


          • #6
            Hence my suggestion of MakeSureDirectoryPathExists in post #2... it creates the entire path (if able) or returns an error.
            <b>George W. Bleck</b>
            <img src='http://www.blecktech.com/myemail.gif'>

            Comment


            • #7
              Hello Chris!

              I highly recommend looking into the API MakeSureDirectoryPathExists that George shared above. I have been using for years now and it has worked without any problems.
              ... .... . ... . . ... ... .... . .. ... .... .. ... .... .. .... ..

              n6jah @ yahoo.com

              Comment


              • #8
                thanxx folks, your advice are really good

                Comment

                Working...
                X