Page 1 of 1

Check if a folder is a symlink

Posted: Thu Jun 12, 2025 9:49 pm
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?

Re: Check if a folder is a symlink

Posted: Fri Jun 13, 2025 2:55 pm
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

Re: Check if a folder is a symlink

Posted: Sat Jun 14, 2025 1:37 am
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

Re: Check if a folder is a symlink

Posted: Sat Jun 14, 2025 8:28 am
by Lebostein
For me it works

Re: Check if a folder is a symlink

Posted: Sat Jun 14, 2025 9:06 am
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

Re: Check if a folder is a symlink

Posted: Sat Jun 14, 2025 9:13 am
by BarryG
Oh, I see.

Re: Check if a folder is a symlink

Posted: Sat Jun 21, 2025 3:52 am
by Lebostein
But it seems only work if I use the x64 version of PB. With PB arm64 version it dont work correctly...