Page 1 of 2
Opening/Activating current sourcefile folder, even in Windows 11
Posted: Sat Jun 28, 2025 7:42 pm
by Blue
A small executable to use as a tool to open or simply bring to the forefront the current source file's folder in PB's IDE.
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
It works faster and more reliably than the current built-in 'Show in Folder' menu option, in that it does NOT open a new window each time it is called nor does it remain inert as one of many tabs in a multi-tabbed Win 11 Explorer window.
Enjoy !
edit 1 : added debugging lines so it can be intelligently and easily tested
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Sun Jun 29, 2025 6:19 am
by Denis
Nice tool blue.
With win 11, with the same displayed pb file, if i use several time the tool, it opens the corresponding 'explorer' window several times.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Sun Jun 29, 2025 11:10 am
by Blue
Denis wrote: Sun Jun 29, 2025 6:19 am
[...]With win 11, with the same displayed pb file, if i use several time the tool, it opens the corresponding 'explorer' window several times.
Salut Cousin Français!
I'm not sure I understand what you're saying.
I put this code together to correct the behaviour of the built-in menu option that just keeps opening a new copy of Windows Explorer EVERY time you call it. It's a slow process that keeps crowding the real estate on the screen. Are you saying that, on your Win 11 bécane, this reworked tool is doing again the same darn thing ?
The other thing this version attempted to correct (at least on my
Win 11 X64 v.24H2 build 26120.4520) is how
PB's RunProgram() with the
%PATH as its first parameter just keeps opening the correct folder in new tabs of an already opened Win 11 Explorer window, without ever bringing that window to the front so you can intercact with it.
So, similar question to you : are you seeing that same behaviour on your system ?
If you could tell me which version of Win 11 you're using, i'd appreciate...
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 1:54 am
by Blue
Ça y est ! Je crois que je viens de comprendre...
Depending on the version of the installed Windows 11, the title displayed by the Explorer window probably varies. I stupidly assumed that it would be what I see on my system, so I hard coded a search string, similar to what I saw on my Windows 11 system. But of course, it's most likely different on your machine. Therefore, the string is not found, and, as required by its internal (but defective) logic, the tool opens a new Explorer window.
So, 2 things...
1. kindly tell me the exact title that appears at the top of your Explorer window when you request a given file's folder (i.e. give me the full title of the Explorer window, and the full path of the file)
2. I have to think of a better way to isolate/extract the folder name from the Explorer window title.
Maybe simply dropping the " : " string added to the search string would be sufficient
It just dawned on me that I have 2 other win 11 machines at home, with, most likely, different versions of the system. I'll look at those.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 5:55 am
by Denis
Salut Blue,
my english is so bad .... (je parle mieux français

).
données systèmes :
Spécifications de l'appareil
Processeur Intel(R) Core(TM) i7-10700 CPIJ @ 2.90GHz (290 GHz)
Mémoire RAM installée 32 Go (31,8 Go utilisable)
Type du système Système d'exploitation 64 bits, processeur x64
Spécifications de Windows
Édition Windows 11 Professionnel
Version 24H2
Build du système d'exploitation 26100.4484
Expérience Pack d'expérience de fonctionnalités Windows 1000.26100.128.0
j'ouvre l'explorer depuis l'onglet de ton code, j'ai en haut de l'explorer (dossier tronqué) :
G:\PureBasic\Projets\Applicati
Real folder :
G:\PureBasic\Projets\Applications codeurs\Blue
voilà
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 2:16 pm
by Blue
C’est bon, ton anglais. Pas de soucis à te faire pour ça.
Si tu préfères qu’on poursuive en français pour mieux comprendre, on passera en mode privé (par respect pour le forum ENG)..
Your Windows system is the same as mine, maybe a different build version (I’m running a beta version).
The truncated folder name you’re quoting is normal, since you’re likely reading it directly from the opened Explorer window. To see the full thing, you have to hover your mouse over the miniature of that window on your taskbar. If this is not clear to you, let me know. I’ll produce and post a picture.
Better yet, download again the code from the original posting. I modified it to include more information when you run it with the PB debugger activated. You’ll get a list of all the Explorer windows titles the finds. If the window is found, the last one listed in the debugging window will be the complete title I’m interested in. Got that ? Copy it and tell me what you get.
One last thing : if you install this code as a PB tool, make sure that you surrender the %PATH parameter with double quotation marks like so >> "%PATH"
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 2:51 pm
by Axolotl
Thanks for sharing.
No offense, but I cannot see any difference to the MenuItem "Show in Folder".
Unfortunately it does not work as expected*, mabe because .....
1. The explorer uses a tabbed interface since Win 11 22H2 (or similar).
2. Many M$ stuff uses other libraries, like DirectUI.
DirectUI
DirectUI is a C++ user interface library created by Microsoft to be a WPF
like API for native applications . It is not released to the public but is
widely used throughout Microsoft products including Windows,
Microsoft Office, and Windows Live Messenger.
*) Maybe I should explain this a little better:
If Explorer is open with several open tabs, it does not matter whether the directory to be opened is included or not. In any case, a new instance of explorer is opened. My expectation would be that either a new tab is opened or the already open tab is activated.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 3:13 pm
by Blue
Axolotl wrote: Mon Jun 30, 2025 2:51 pm
[…]
No offense, but I cannot see any difference to the MenuItem "Show in Folder".
[…]
None taken, and thanks for joining this discussion.
The reason you’re not seeing any difference is most likely what I’m explaining in the 2nd post just above yours.
I’m running the same Windows 11 as you, so the malfunction has nothing to do with either of the points you’re raising.
If the proposed tool in not finding the correct Explorer title (incorrect search string), it will of course behave exactly as the built-in option, since it then does exactly the very same thing : opening a new Explorer window for each display request.
Run the tool (the latest
modified version, see opening post) in debugging mode in the PB IDE, and tell me what gets displayed in the debugging window. That will help pinpoint why you’re not getting anything better than the built-in "Show in folder" option.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 3:23 pm
by Denis
Blue wrote: Mon Jun 30, 2025 2:16 pm
Better yet, download again the code from the original posting. I modified it to include more information when you run it with the PB debugger activated. You’ll get a list of all the Explorer windows titles the finds. If the window is found, the last one listed in the debugging window will be the complete title I’m interested in. Got that ? Copy it and tell me what you get.
Result :
; folder we want : 'Blue :'
; 45. PureBasic 6.21 (x64) - current sourcefile folder.pb [51]
; 49. G:\PureBasic\Projets\Applications codeurs\Blue : Explorateur de fichiers [72]
; 66. ESET - PureBasic Forums - English - Post a reply — Mozilla Firefox [66]
; 70. G:\PureBasic\Projets\Applications codeurs\Blue : Explorateur de fichiers [72]
; 87. G:\PureBasic\Projets\Denis\ASM_Generator\ASM_Generator\ASM_Generator__Resident.pb - Notepad++ [93]
; 96. G:\PureBasic\PureBasic__Environnement_X64\PureLibraries\UserLibraries : Explorateur de fichiers [95]
; 117. Paramètres [10]
; 122. Paramètres [10]
; 127. G:\PureBasic\Projets\Denis\ASM_Generator\ASM_Generator\PureAsm\PureASM_Test\X86 : Explorateur de fichiers [105]
; 240. _SoftwareUpdateNotificationService_Window [41]
; 247. MainWindowView [14]
; 342. Program Manager [15]
; No Explorer window with this folder.
; Let's request one...
Blue wrote: Mon Jun 30, 2025 2:16 pm
One last thing : if you install this code as a PB tool, make sure that you surrender the %PATH parameter with double quotation marks like so >> "%PATH"
That's what I did.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 4:26 pm
by Blue
Excellent.
The 2nd line of your results (; 49. G:\PureBasic\Projets\Applications codeurs\Blue : Explorateur de fichiers [72]) is what we’re looking for.
So, if you run the tool again, it should (will ?) find "Blue :" and bring that window to the foreground, WITHOUT opening another one… which is the object of the exercise.
So, did you run the tool a second time, within the same debugging conditions ? Did the existing Explorer window jump to the foreground once the window with the title starting with "Blue :" was found ? If yes, then everything works as I wanted it to work, and works certainly better than the built-in tool.
If not, I’d really like to know why and I’d be even more interested than ever…
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 4:47 pm
by Axolotl
Okay,
I would say you found the position of the problem:
because the Tab is "PureBasic" only. I commented the line out and no new explorer opens.
Your Debug Message says:
Code: Select all
; The requested Explorer window exists.
; We bring it to the foreground...
But nothing happens to the explorer window so far.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 4:54 pm
by Axolotl
Maybe I would do it this way. Looking for the classname.
However, I have not yet found out how to change the tabs (except with Cntrl+Tab or Cntrl+Shift+Tab)
Code: Select all
EnableExplicit
Structure TData
handle.i
Text$
EndStructure
Global NewList OpenFolders.TData()
Procedure EnumFunc(hwnd, LParam)
Protected tmp$, cn${#MAX_PATH}, wt${#MAX_PATH}
CompilerIf #PB_Compiler_Debugger And 0 ; <= activate the debug output -- you know how.
tmp$ = " "
If GetClassName_(hwnd, @cn$, #MAX_PATH)
tmp$ + LSet(#DQUOTE$ + cn$ + #DQUOTE$, 50)
Else
tmp$ + LSet(#DQUOTE$ + #DQUOTE$, 50)
EndIf
If GetWindowText_(hwnd, @wt$, #MAX_PATH)
tmp$ + #DQUOTE$ + wt$ + #DQUOTE$
Else
tmp$ + #DQUOTE$ + #DQUOTE$
EndIf
Debug tmp$
CompilerEndIf
If GetClassName_(hwnd, @cn$, #MAX_PATH) And cn$ = "ShellTabWindowClass"
If GetWindowText_(hwnd, @wt$, #MAX_PATH)
AddElement(OpenFolders())
OpenFolders()\handle = hwnd
OpenFolders()\Text$ = wt$
EndIf
EndIf
ProcedureReturn #True ; continue enumeration
EndProcedure
; ---------------------------------------------------------------------------------------------------------------------
Procedure BrowseToFile(FileName$)
Protected hwnd
hWnd = FindWindow_("CabinetWClass", 0)
If hWnd
Debug "Exploerer found."
EnumChildWindows_(hWnd, @EnumFunc(), 0)
FileName$ = RTrim(FileName$, #PS$)
FileName$ = StringField(FileName$, CountString(FileName$, #PS$) + 1, #PS$)
ForEach OpenFolders()
Debug ">> " + OpenFolders()\Text$
If FileName$ = OpenFolders()\Text$
Debug "###### found " + FileName$
SetForegroundWindow_(OpenFolders()\handle)
EndIf
Next
; SetForegroundWindow_(hwnd)
EndIf
EndProcedure
BrowseToFile(GetCurrentDirectory())
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Mon Jun 30, 2025 4:59 pm
by Blue
Axolotl wrote: Mon Jun 30, 2025 4:47 pm
Okay,
I would say you found the position of the problem:
[...]
I don't follow.
It shouldn't say that at all, since the first time you run the tool (
again : test with the
debugger activated in the IDE), an
independent Explorer window would
NOT be found, resulting in a NEW independent
Explorer window being opened (
same as built-in option) and being forced to the foreground (
exactly how the built-in option works).
Then, run the tool another time. The
INDEPENDENT Explorer window is then found (
NOT a tabbed window) and pushed to the foreground (
NOT a new window, contrary to the built-in).
Just forget tabbed windows. My tool was never intended to intercact with them. The seemingly erroneous string you quote has nothing to do with what I coded, or what i'm trying to do here.
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Tue Jul 01, 2025 6:20 am
by Denis
Blue wrote: Mon Jun 30, 2025 4:26 pm
Excellent.
The 2nd line of your results (
; 49. G:\PureBasic\Projets\Applications codeurs\Blue : Explorateur de fichiers [72]) is what we’re looking for.
So, if you run the tool again, it should (will ?) find "
Blue :" and bring that window to the foreground, WITHOUT opening another one… which is the object of the exercise.
So, did you run the tool a second time, within the same debugging conditions ? Did the existing Explorer window jump to the foreground once the window with the title starting with "
Blue :" was found ? If yes, then everything works as I wanted it to work, and works certainly better than the built-in tool.
firstly - hwin is alway equal to 0, so test 'If hWin is never true.
secondly - I do prefer use API ShellExecute_() instead of RunProgram.
If you change
to
Code: Select all
ShellExecute_(#Null, "open", param$, #Null, #Null, #SW_MAXIMIZE))
, it works correctly, but it doesn't answer why hwin is always null.
with the following code, the result is correct on my system (always only 1 instance opened).
EnableExplicit
Define 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
ShellExecute_(#Null, "open", param$, #Null, #Null, #SW_MAXIMIZE)
End
Re: Opening/Activating current sourcefile folder, even in Windows 11
Posted: Tue Jul 01, 2025 2:15 pm
by AZJIO