Compile it, add it as a Tool to the IDE with %PATH as its parameter and then add it to your toolbar with an icon of your choice, so you can call it quickly.
Code: Select all
; :: -------------------------- ::
; :: Get current source file folder ::
; :: Blue june 2025 ::
; :: -------------------------- ::
; opens the current sourcefile's directory in Windows File Explorer
; brings it to the foreground if it's already open
EnableExplicit
#buffer = 256
Define hWin, app$, param$
CompilerIf #PB_Compiler_Debugger
param$ = GetCurrentDirectory() ; standard PB path function
CompilerElse
param$ = ProgramParameter(0) ; %PATH provided by PB tools
CompilerEndIf
If param$ =""
End
EndIf
Procedure Fenetre_Explorer(dossier$)
; renvoie l'ID de la fenêtre recherchée si le dossier est ouvert dans Windows Explorer
;
; inspiré de
; http://www.purebasic.fr/english/viewtopic.php?t=25489
;
Protected n,hWin, nom$
CompilerIf #PB_Compiler_Debugger
Protected i
CompilerEndIf
; 1. se débarasser du \ final dans le nom complet du dossier
If Right(dossier$,1) = "\"
dossier$ = Left(dossier$, Len(dossier$)-1)
EndIf
; 2. déterminer la position du dernier \ dans le nom inversé
n = FindString(ReverseString(dossier$),"\")
; 3. récupérer le nom du dossier à la fin de la chaîne
dossier$ = Right(dossier$,n-1)
; 4. Windows 11 displays "[folder name] : Explorateur de fichiers" on FR-CA system
; so we keep only the colon
dossier$ + " :" ;; comment out this line if the tool keeps opening a new Explorer window for each call
; dossier$ + " " ;; that's a single space — comment out this line if the tool keeps opening many Explorer windows
;; but normally something specific is required to unambiguously identify the proper end of the string
Debug "; folder we want : '" + dossier$ +"'"
n = Len(dossier$)
hWin = GetWindow_(GetDesktopWindow_(),#GW_CHILD)
While hWin
hWin = GetWindow_(hWin,#GW_HWNDNEXT)
CompilerIf #PB_Compiler_Debugger
i + 1
CompilerEndIf
If 0 = IsWindowVisible_(hWin)
Continue
EndIf
nom$ = Space(#buffer)
GetWindowText_(hWin, nom$, #buffer)
If nom$
Debug "; " + i + ". " + nom$ + " [" + Len(nom$)+"]"
If Left(nom$,n) = dossier$
; on a trouvé !
ProcedureReturn hWin
EndIf
EndIf
Wend
ProcedureReturn 0
EndProcedure
hWin = Fenetre_Explorer(param$)
If hWin
; this window is already opened; we bring it to the front
Debug Chr(10) + "; The requested Explorer window exists."
Debug "; We bring it to the foreground..."
ShowWindow_(hWin, #SW_RESTORE)
SetForegroundWindow_(hWin)
Else
Debug Chr(10) + "; No Explorer window with this folder."
Debug "; Let's request one..."
; no such window opened; we create it
; to make sure this works even in Windows 11, we call Windows File Explorer nominally
app$ = GetEnvironmentVariable("windir") + "\explorer.exe"
RunProgram(app$, param$,"")
EndIf
End
Enjoy !
edit 1 : added debugging lines so it can be intelligently and easily tested