Create Full Path Procedure

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1277
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Create Full Path Procedure

Post by Randy Walker »

You'll have to uncomment the CreateDirectory line for it to actually work, but run as is in Debugger to see how it would work in action. Of course, IF the drive exists it will fail to create the root directory. To be expected:

Code: Select all

Procedure Makepath(Sample$)
Sample$ = Trim(Sample$, "\")
Path$ = ""
For D = 1 To CountString(Sample$, "\") + 1
  Path$ + StringField(Sample$, D, "\") + "\"
  If FileSize(Path$) = -1
    Debug Path$
;   CreateDirectory(Path$)
  Else
    Debug Path$+" already exists"
  EndIf
Next D
EndProcedure

;Examples with and without trailing \
Makepath("D:\Eat\At\Joes\Bar\And\Grill\")
Delay(3000)
Makepath("D:\Eat\At\Joes\Bar\And\Grill")
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist. :mrgreen:
BarryG
Addict
Addict
Posts: 4343
Joined: Thu Apr 18, 2019 8:17 am

Re: Create Full Path Procedure

Post by BarryG »

For Windows only, it's just one API command:

Code: Select all

SHCreateDirectory_(0,"D:\Eat\At\Joes\Bar\And\Grill\")
Randy Walker
Addict
Addict
Posts: 1277
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Re: Create Full Path Procedure

Post by Randy Walker »

BarryG wrote: Tue Mar 10, 2026 8:39 am For Windows only, it's just one API command:

Code: Select all

SHCreateDirectory_(0,"D:\Eat\At\Joes\Bar\And\Grill\")
Good to know. THANKS again BarryG !!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist. :mrgreen:
BarryG
Addict
Addict
Posts: 4343
Joined: Thu Apr 18, 2019 8:17 am

Re: Create Full Path Procedure

Post by BarryG »

I've been using that forever. :)
Randy Walker
Addict
Addict
Posts: 1277
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA
Contact:

Re: Create Full Path Procedure

Post by Randy Walker »

I'm, guessing change backslash to forward slash for Linux or MAC in my procedure to get it to work there. Cannot test here.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist. :mrgreen:
User avatar
Piero
Addict
Addict
Posts: 1226
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Create Full Path Procedure

Post by Piero »

Randy Walker wrote: Tue Mar 10, 2026 9:08 am I'm, guessing change backslash to forward slash for Linux or MAC in my procedure to get it to work there. Cannot test here.

Code: Select all

Procedure Makepath(Sample$)
   Sample$ = Trim(Sample$, #PS$)
   If #PS$ = "/" : PS$ = #PS$ : EndIf
   For D = 1 To CountString(Sample$, #PS$) + 1
      Path2$ + StringField(Sample$, D, #PS$) + #PS$
      Path$ = PS$ + Path2$
      If FileSize(Path$) = -1
         Debug Path$
         ;   CreateDirectory(Path$)
      Else
         Debug Path$+" already exists"
      EndIf
   Next D
   Debug""
EndProcedure

;Examples with and without trailing #PS$
Makepath("D:\Eat\At\Joes\Bar\And\Grill\")
Makepath("D:\Eat\At\Joes\Bar\And\Grill")
Makepath("/Eat/At/Joes/Bar/And/Grill/")
Makepath("/Eat/At/Joes/Bar/And/Grill")
Axolotl
Addict
Addict
Posts: 955
Joined: Wed Dec 31, 2008 3:36 pm

Re: Create Full Path Procedure

Post by Axolotl »

BarryG wrote: Tue Mar 10, 2026 8:39 am For Windows only, it's just one API command:

Code: Select all

SHCreateDirectory_(0,"D:\Eat\At\Joes\Bar\And\Grill\")
Fun Fact: It's on the Deprecated APi Section since more than 10 years now. :oops: :mrgreen:
SHCreateDirectory is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
HeX0R
Addict
Addict
Posts: 1258
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Create Full Path Procedure

Post by HeX0R »

better use

Code: Select all

If FileSize(Path$) <> -2
you might have files looking like directories
User avatar
Piero
Addict
Addict
Posts: 1226
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Create Full Path Procedure

Post by Piero »

HeX0R wrote: Tue Mar 10, 2026 5:47 pm better use

Code: Select all

If FileSize(Path$) <> -2
you might have files looking like directories
Not on Mac: you cannot create a dir if there's a file with the same name (at least not on Finder… don't wanna risk to try with PB and nuke my filesystem :mrgreen: )
User avatar
Piero
Addict
Addict
Posts: 1226
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Create Full Path Procedure

Post by Piero »

:mrgreen:

Code: Select all

Procedure Makepath(Sample$)
   Protected PS$, Path$, Path2$, cs, D, fs
   Sample$ = Trim(Sample$, #PS$)
   If #PS$ = "/" : PS$ = #PS$ : EndIf
   cs = CountString(Sample$, #PS$) + 1
   For D = 1 To cs
      Path2$ + StringField(Sample$, D, #PS$) + #PS$
      Path$ = PS$ + Path2$
      fs = FileSize(Path$)
      If fs = -1
         ;  If Not CreateDirectory(Path$) : Debug Path$+" - Error!" : Break : EndIf
         Debug Path$
      ElseIf fs = -2
         Debug Path$+" - already exists"
      EndIf
   Next D
   Debug""
EndProcedure
ebs
Enthusiast
Enthusiast
Posts: 567
Joined: Fri Apr 25, 2003 11:08 pm

Re: Create Full Path Procedure

Post by ebs »

I use MakeSureDirectoryPathExists for the same purpose:

Code: Select all

MakeSureDirectoryPathExists_("D:\Eat\At\Joes\Bar\And\Grill\")
Axolotl wrote: Tue Mar 10, 2026 2:30 pm
BarryG wrote: Tue Mar 10, 2026 8:39 am For Windows only, it's just one API command:

Code: Select all

SHCreateDirectory_(0,"D:\Eat\At\Joes\Bar\And\Grill\")
Fun Fact: It's on the Deprecated APi Section since more than 10 years now. :oops: :mrgreen:
SHCreateDirectory is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions.
Post Reply