Check if a folder is a symlink

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Check if a folder is a symlink

Post by Lebostein »

On macos I get "-2" if I check a folder with FileSize() which is actually a symlink to another directory. I suppose it is similar under Windows.
How I can check if a folder is a real folder or a symlink?
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Re: Check if a folder is a symlink

Post by Lebostein »

ChatGPT has helped me successfully: :D

Code: Select all

ImportC ""
  lstat(path.p-utf8, *buf)
EndImport

Structure stat
  st_dev.q
  st_mode.l
  st_nlink.l
  st_ino.q
  st_uid.l
  st_gid.l
  st_rdev.q
  st_atimespec.q[2]
  st_mtimespec.q[2]
  st_ctimespec.q[2]
  st_birthtimespec.q[2]
  st_size.q
  st_blocks.q
  st_blksize.l
  st_flags.l
  st_gen.l
  st_lspare.l
  st_qspare.q[2]
EndStructure

#S_IFMT  = $F000
#S_IFLNK = $A000

Procedure IsSymlink(Path$)
  Protected statbuf.stat
  If lstat(Path$, @statbuf) = 0
    If (statbuf\st_mode & #S_IFMT) = #S_IFLNK
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure

; Beispiel:
Define Pfad$ = "/Users/lebostein/Documents/Test"
If IsSymlink(Pfad$)
  Debug Pfad$ + " is Symlink."
Else
  Debug Pfad$ + " no Symlink."
EndIf
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: Check if a folder is a symlink

Post by BarryG »

Lebostein wrote: Fri Jun 13, 2025 2:55 pmChatGPT has helped me successfully
This is what I get with the ChatGPT code you posted:

Image
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Re: Check if a folder is a symlink

Post by Lebostein »

For me it works
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Check if a folder is a symlink

Post by Mindphazer »

BarryG wrote: Sat Jun 14, 2025 1:37 am
Lebostein wrote: Fri Jun 13, 2025 2:55 pmChatGPT has helped me successfully
This is what I get with the ChatGPT code you posted:

Image
Barry, the code is for MacOS (and probably Linux)
It doesn't work on Windows
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
BarryG
Addict
Addict
Posts: 4118
Joined: Thu Apr 18, 2019 8:17 am

Re: Check if a folder is a symlink

Post by BarryG »

Oh, I see.
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Re: Check if a folder is a symlink

Post by Lebostein »

But it seems only work if I use the x64 version of PB. With PB arm64 version it dont work correctly...
Post Reply