Page 1 of 1

Force a file creation in a given path ...

Posted: Wed Jun 09, 2004 7:51 am
by fweil
When creating a new file, it is necessary to check if the path to the desired file exists.

This is not a huge work but can overdo your code and mind ..

Here is a code I use :

Code: Select all

;
; Create a file at a given path location creating sub-directories if necessary
;
Procedure DoCreateFile(n.l, FileName.s)
  PathPart.s = ""
  FilePart.s = GetFilePart(FileName)
  If GetPathPart(FileName) <> ""
      i = 1
      Repeat
        Node.s = StringField(FileName, i, "\")
        If Node = FilePart
            Break
        EndIf
        PathPart + Node + "\"
        If FileSize(PathPart) = -2
          Else
            CreateDirectory(PathPart)
        EndIf
        i + 1
      Until Node = ""
  EndIf
  ProcedureReturn CreateFile(n, FileName)
EndProcedure

;
; Test
;
  If DoCreateFile(0, "toto\dudule\machin.txt")
      CloseFile(0)
  EndIf

  If DoCreateFile(0, "machin.txt")
      CloseFile(0)
  EndIf
End

Posted: Wed Jun 09, 2004 8:38 am
by blueznl
a variant would be to split it into two functions...

IsFolder() would test if a path is valid, and...

CreateFolder() would create the folder structure

actually creating a file i would do directly, with OpenFile or whatever

Posted: Wed Jun 09, 2004 10:09 am
by PolyVector
very usefull :)

Posted: Wed Jun 09, 2004 10:32 am
by gnozal
MakeSureDirectoryPathExists (Win32.hlp) :wink:
The MakeSureDirectoryPathExists function creates all the directories in the specified DirPath.

BOOL MakeSureDirectoryPathExists(
IN LPSTR DirPath
);

Parameters

DirPath
A pointer to an ASCII string that contains a valid path name.

Return Values

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

Remarks

The DirPath is parsed and each directory, beginning at the root, is created, if it does not already exist. If only some of the directories are created, the function will return FALSE.

Re: Force a file creation in a given path ...

Posted: Sun Jan 27, 2013 11:55 pm
by WilliamL
Hmm, old thread...

I need to check to see if the folder exists - e.g. IsFolder(folderpath$)

fweil appears to be creating a file with the 'folderpath$' and seeing if it is successful. If you create a 'dummy' file in the folder then I suppose you would have to 'Delete' the dummy file.

Any ideas for a IsFolder() checking that is not API?

Re: Force a file creation in a given path ...

Posted: Mon Jan 28, 2013 12:05 am
by Shield
FileSize() ;) Check the help.

Re: Force a file creation in a given path ...

Posted: Mon Jan 28, 2013 12:08 am
by WilliamL
Yes, yes ,yes

It just occurred to me that FileSize will return a folder search

Code: Select all

foldername$=RsrcPath+foldername$+"/"
If FileSize(foldername$)=-1: MessageRequester("Folder missing!",foldername$) : EndIf
and I get -2 if the folder is there

so this seems to work... dumb question...