Force a file creation in a given path ...

Share your advanced PureBasic knowledge/code with the community.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Force a file creation in a given path ...

Post 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
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

very usefull :)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post 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.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

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

Post 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?
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

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

Post by Shield »

FileSize() ;) Check the help.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

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

Post 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...
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply