Announcement

Collapse
No announcement yet.

making a directory tree

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

  • Guy Dombrowski
    replied
    Mel,

    My code can make a multi level directory as needed without any operator input.
    It will simply add the missing upper level if the lower one is already there.
    The logic can be scaled for more levels if necessary.
    As the NUL file is always true the directory test is always valid.
    You could also write a recursive loop to handle more sophisticated tree.

    Leave a comment:


  • Mel Bishop
    replied
    Wouldn't it somewhat easier and simpler to use something along the lines of:
    Code:
    FUNCTION PBMAIN () AS LONG
        COLOR 14,1
        CLS
        fi$ = "c:\TestDir"
        te$ = DIR$(fi$,16)
        PRINT;te$;ERR
        IF te$ = "" then MKDIR fi$
        WAITKEY$
    END FUNCTION

    Leave a comment:


  • Guy Dombrowski
    replied
    (Also, if path$ contains more than one folder name node, MKDIR fails)
    You are right Michael,
    So the proper code would look like this :

    Code:
    TRY
       OPEN path1$\path2$\NUL for input as #1: close #1: EXIT TRY ' Directory exist
    CATCH
       TRY
           MKDIR path1$: MKDIR path1$\path2$: EXIT TRY ' Path 1 or 2 or both was missing
       CATCH
           MKDIR path1$\path2$ ' Path 2 was missing
       END TRY
    END TRY
    The MKDIR command can make only one node at a time.
    Last edited by Guy Dombrowski; 23 Oct 2009, 12:33 PM. Reason: Clean presentation and add comments

    Leave a comment:


  • Michael Mattias
    replied
    Code:
    TRY
      OPEN path$\NUL for input as #1
    CATCH
      MKDIR path$
    END TRY
    CLOSE #1
    ==>
    Code:
    TRY
      OPEN path$\NUL for input as #1
      CLOSE #1
    CATCH
      MKDIR path$
    END TRY
    ??????

    (Do not attempt CLOSE#1 if OPEN failed. Definitely more style points)

    (Also, if path$ contains more than one folder name node, MKDIR fails)

    MCM

    Leave a comment:


  • Guy Dombrowski
    replied
    A simple way to check if a directory exist and create it if not
    Code:
    TRY
      OPEN path$\NUL for input as #1
    CATCH
      MKDIR path$
    END TRI
      CLOSE #1

    Leave a comment:


  • George Bleck
    replied
    This of any help?

    MakeSureDirectoryPathExists


    Syntax

    C++

    BOOL WINAPI MakeSureDirectoryPathExists( __in PCSTR DirPath);

    Parameters

    DirPath [in] A valid path name. If the final component of the path is a directory, not a file name, the string must end with a backslash (\) character.
    Return Value

    If the function succeeds, the return value is TRUE.
    If the function fails, the return value is FALSE. To retrieve extended error information, call GetLastError.
    Remarks

    Each directory specified is created, if it does not already exist. If only some of the directories are created, the function will return FALSE.
    This function does not support Unicode strings. To specify a Unicode path, use the SHCreateDirectoryEx function.
    All DbgHelp functions, such as this one, are single threaded. Therefore, calls from more than one thread to this function will likely result in unexpected behavior or memory corruption. To avoid this, you must synchronize all concurrent calls from more than one thread to this function.

    Leave a comment:


  • Eduardo Miranda Alvarez
    replied
    MakeDir

    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"


    '============================================================================
    ' MakeDir - Create the specified directory if it does not exist. Will create
    ' nested directories if they do not exist.
    '
    ' Directory - Full pathspec for the directory to create
    '


    FUNCTION MakeDir (BYVAL Rep AS STRING) EXPORT AS LONG
    IF LEFT$(Rep, 2) = "\\" THEN EXIT FUNCTION

    LOCAL Directory AS STRING
    LOCAL Chm AS STRING
    LOCAL ErrCode AS LONG
    LOCAL P1 AS LONG
    LOCAL P2 AS LONG
    LOCAL Done AS LONG
    LOCAL FileData AS DIRDATA, sResult AS STRING
    ' If the directory already exists then simply exit
    ' If Dir$(Rep) > "" Then Exit Function

    sResult = DIR$( Rep, %SUBDIR, TO FileData) 'new
    IF sResult >"" THEN MSGBOX "Exist No More ",64,FUNCNAME$ :EXIT FUNCTION 'new


    ON ERROR GOTO ErrCredir
    Directory = RTRIM$(Rep, "\")
    IF LEN(Directory) THEN
    P1 = 1
    DO
    P2 = INSTR(P1, Directory, "\")
    IF P2 THEN
    Chm = MID$(Directory, 1, P2 - 1): P1 = P2 + 1
    ELSE
    Chm = Directory
    END IF
    IF LEN(DIR$(Chm, 16)) = 0 THEN MKDIR Chm
    LOOP UNTIL P2 = 0
    END IF
    ErrCredir:
    ErrCode = ERRCLEAR
    IF ErrCode AND Done = 0 THEN Done = ErrCode: RESUME ErrCredir
    IF Done THEN ErrCode = Done
    FUNCTION = ErrCode
    END FUNCTION




    FUNCTION PBMAIN () AS LONG
    LOCAL ERRORcODE AS LONG
    MakeDir "C:\NewFolder" TO ERRORcODE
    MSGBOX STR$(ERRORcODE)

    END FUNCTION

    Leave a comment:


  • Michael Mattias
    replied
    ShCreateDirectory(), OPEN, GET, CLOSE, OPEN, PUT, CLOSE, KILL, then.

    Leave a comment:


  • Erich Schulman
    replied
    ShFileOperation looks good, but it might not be right for this situation: http://stackoverflow.com/questions/2...indows-service

    Leave a comment:


  • Paul Squires
    replied
    Not sure where I got this code from (maybe from this forum, maybe not).

    Code:
    '============================================================================
    ' MakeDir - Create the specified directory if it does not exist.  Will create
    '           nested directories if they do not exist.
    '
    ' Directory - Full pathspec for the directory to create
    '
    Function MakeDir (ByVal Rep As String) Export As Long 
        If Left$(Rep, 2) = "\\" Then Exit Function
        
        Local Directory As String
        Local Chm       As String
        Local ErrCode   As Long
        Local P1        As Long
        Local P2        As Long
        Local Done      As Long
        
        ' If the directory already exists then simply exit
        If Dir$(Rep) > "" Then Exit Function
        
        On Error GoTo ErrCredir 
        Directory = RTrim$(Rep, "\")
        If Len(Directory) Then
           P1 = 1
           Do 
              P2 = InStr(P1, Directory, "\")
              If P2 Then
                 Chm = Mid$(Directory, 1, P2 - 1): P1 = P2 + 1
              Else
                 Chm = Directory
              End If
              If Len(Dir$(Chm, 16)) = 0 Then MkDir Chm
            Loop Until P2 = 0
        End If 
     ErrCredir:
        ErrCode = ErrClear
        If ErrCode And Done = 0 Then Done = ErrCode: Resume ErrCredir
        If Done Then ErrCode = Done 
        Function = ErrCode 
     End Function

    Leave a comment:


  • Michael Mattias
    replied
    ShFileOperation() handles the making of required directories and subdirectories automatically (if you tell it to do so).

    No MKDIR, no NAME required. Just do a "MOVE" (%FO_MOVE) operation after setting up your source and destination file names.

    MCM

    Leave a comment:


  • Erich Schulman
    started a topic making a directory tree

    making a directory tree

    I need to write a Windows service that will automatically move files from an incoming directory to a time-based directory structure. It will need to make new directories as needed.

    One possibility is:
    Code:
    ON ERROR RESUME NEXT 'don't fail if any dir exists
    'unconditionally attempt to create each directory
    MKDIR TRIM$(STR$(year))
    MKDIR TRIM$(STR$(month))
    MKDIR TRIM$(STR$(day))
    MKDIR TRIM$(STR$(hour))
    ...
    NAME IncomingFile AS RootDir+"\"+TRIM$(STR$(year))+"\"+TRIM$(STR$(month))+"\"+TRIM$(STR$(day))+"\"+TRIM$(STR$(hour))+"\"+IncomingFile
    Is this way adequate, or is there a better way?
Working...
X