How to get the Documents folder of the current Windows user.

New to FreeBASIC? Post your questions here.
TheAlmightyGuru
Posts: 20
Joined: Feb 18, 2008 21:35
Location: Grand Blanc, MI 48439
Contact:

How to get the Documents folder of the current Windows user.

Postby TheAlmightyGuru » Feb 14, 2020 21:26

I'm trying to save a file into the user's documents folder. Is there any way to get the Documents folder of the currently logged in user in Windows?

This is usually stored in C:\Users\[user name]\Documents.

Likewise for folders like Desktop, Pictures, Music, etc.

Thanks!
Josep Roca
Posts: 466
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: How to get the Documents folder of the current Windows user.

Postby Josep Roca » Feb 14, 2020 21:48

See: SHGetKnownFolderIDList function
https://docs.microsoft.com/en-us/window ... lderidlist
adeyblue
Posts: 3
Joined: Nov 07, 2019 20:08

Re: How to get the Documents folder of the current Windows user.

Postby adeyblue » Feb 14, 2020 22:19

You want the path one, the pidl one adds an unneccesary intermediate step just for this. This one will add any extra bits on to the path for you so you don't have to do it yourself, not that that's a big deal.

CSIDL_MYDOCUMENTS is the user 'my documents' path

https://docs.microsoft.com/en-us/window ... andsubdira
TheAlmightyGuru
Posts: 20
Joined: Feb 18, 2008 21:35
Location: Grand Blanc, MI 48439
Contact:

Re: How to get the Documents folder of the current Windows user.

Postby TheAlmightyGuru » Feb 18, 2020 2:49

Thank you both for the suggestions. I've been trying to figure out how to convert this Windows API call to the FreeBASIC syntax. This is what I've got so far from looking at FreeBASIC examples, but I'm getting the error, 59: Illegal specification, at parameter 1. Any chance you could either point me to a guide on how to do the conversion or give me some tips in the right direction? Thanks!

Code: Select all

Declare Function SHGetFolderPathAndSubDirA (ByVal hwnd As HWnd, ByVal csidl As Int, ByVal hToken As HANDLE, ByVal dwFlags As DWORD, ByVal pszSubDir As LPCSTR, ByVal pszPath As LPSTR) As HWnd
jj2007
Posts: 1319
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to get the Documents folder of the current Windows user.

Postby jj2007 » Feb 18, 2020 9:53

You post two lines of code. What do you expect members of this forum to do?

Knowing immediately why you got an error? (won't work, almighty gurus are very rare)
Guess what is wrong?
Provide you with suggestions that might work?

Or do you want them to test your code, and their own ideas? In that case, you'll get no answers, because nobody has the time to construct the missing stuff around your two lines.

In short: Post a complete example, and you may get tested answers.
SARG
Posts: 950
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How to get the Documents folder of the current Windows user.

Postby SARG » Feb 18, 2020 11:04

TheAlmightyGuru wrote:Thank you both for the suggestions. I've been trying to figure out how to convert this Windows API call to the FreeBASIC syntax. This is what I've got so far from looking at FreeBASIC examples, but I'm getting the error, 59: Illegal specification, at parameter 1. Any chance you could either point me to a guide on how to do the conversion or give me some tips in the right direction? Thanks!

Code: Select all

Declare Function SHGetFolderPathAndSubDirA (ByVal hwnd As HWnd, ByVal csidl As Int, ByVal hToken As HANDLE, ByVal dwFlags As DWORD, ByVal pszSubDir As LPCSTR, ByVal pszPath As LPSTR) As HWnd


Add

Code: Select all

#include "windows.bi"

Then change

Code: Select all

 ByVal csidl As int
by

Code: Select all

ByVal csidl As long

Now the declare line doesn't throw errors.
However, as jj2007 said, more information/code would help....
PaulSquires
Posts: 807
Joined: Jul 14, 2005 23:41
Contact:

Re: How to get the Documents folder of the current Windows user.

Postby PaulSquires » Feb 18, 2020 11:40

For Windows programmers, make your life a hell of a lot easier by downloading Jose Roca's WinFBX library of functions from GitHub. It is 100% source code and includes just about everything you need to interface to the Windows api.

Get WinFBX from:
https://github.com/JoseRoca/WinFBX

Support for WinFBX can be obtained from:
https://www.planetsquires.com/protect/f ... board=39.0
dodicat
Posts: 6155
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to get the Documents folder of the current Windows user.

Postby dodicat » Feb 18, 2020 12:07

Get all instances of a folder.
(takes about 20 seconds here to look everywhere)
File Not Found sometimes shows, just ignore it and wait until done shows.

Code: Select all

function findfolder(folder as string) As String
    dim as string s=" dir cd \  /ad /b /S"
    Var f=Freefile
    Dim As String tmp
    Open Pipe s For Input As #f
    s=""
    cls
    print "Please wait . . ."
    Do Until Eof(f)
        Line Input #f,tmp
        var p=instrrev(tmp,"\")
        if lcase( mid(tmp,p+1))=lcase(folder) then print tmp:s+=tmp+Chr(10)
    Loop
    Close #f
    Return s
End Function


 findfolder("documents")
print "done"
sleep
 
hhr
Posts: 1
Joined: Nov 29, 2019 10:41

Re: How to get the Documents folder of the current Windows user.

Postby hhr » Feb 18, 2020 20:21

Code: Select all

Print Environ("userprofile") 'Home directory in Windows
Print Environ("userprofile") & "\Documents" 'Append the name of the folder
Sleep

Code: Select all

#include once "win/shlobj.bi"
Dim As Zstring*MAX_PATH pszPath,pszSubDir=""
SHGetFolderPathAndSubDirA(0,CSIDL_PERSONAL Or CSIDL_FLAG_CREATE,0,SHGFP_TYPE_CURRENT,pszSubDir,pszPath)
Print pszPath
Sleep

AfxWin.inc is part of WinFBX.
WinFBE contains FreeBASIC and WinFBX, so the following lines will run in WinFBE.

Code: Select all

#include once "Afx\AfxWin.inc"
Print AfxGetSpecialFolderLocation(CSIDL_PERSONAL)
Sleep
srvaldez
Posts: 2231
Joined: Sep 25, 2005 21:54

Re: How to get the Documents folder of the current Windows user.

Postby srvaldez » Feb 18, 2020 21:38

Hi hhr
I like your Environ solution :-)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest