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: 501
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: 25
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: 1726
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: 1161
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: 883
Joined: Jul 14, 2005 23:41

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: 6726
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: 10
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: 2545
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 :-)
marcov
Posts: 3020
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

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

Postby marcov » Feb 27, 2020 11:41

(this function seems to be declared as hwnd as both type and name of the first parameter. It might be that case sensitivity rescues FB there, but it is something different from the other params, and the errormsg points to parameter 1)
dodicat
Posts: 6726
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Postby dodicat » Feb 27, 2020 12:50

What function is (this function) Marcov?
You've got me there.

environ only seems to catch some of windows environment variables.

Code: Select all

dim as string s(1 to 31)

s(1)="ALLUSERSPROFILE   Local returns the location of the All Users Profile."
s(2)="APPDATA   Local returns the location where applications store data by default."
s(3)="CD   Local returns the current directory string."
s(4)="CMDCMDLINE   Local returns the exact command line used to start the current cmd.exe."
s(5)="CMDEXTVERSION   System returns the version number of the current Command Processor Extensions."
s(6)="COMPUTERNAME   System returns the name of the computer."
s(7)="COMSPEC   System returns the exact path to the command shell executable."
s(8)="DATE   System returns the current date. This variable uses the same format as the date /t command. Cmd.exe generates this variable. For more information about the date command, see the Date command."
s(9)="ERRORLEVEL   System returns the error code of the most recently used command. A non-0 value usually indicates an error."
s(10)="HOMEDRIVE   System returns which local workstation drive letter is connected to the user's home directory. This variable is set based on the value of the home directory. The user's home directory is specified in Local Users and Groups."
s(11)="HOMEPATH   System returns the full path of the user's home directory. This variable is set based on the value of the home directory. The user's home directory is specified in Local Users and Groups."
s(12)="HOMESHARE   System returns the network path to the user's shared home directory. This variable is set based on the value of the home directory. The user's home directory is specified in Local Users and Groups."
s(13)="LOGONSEVER   Local returns the name of the domain controller that validated the current logon session."
s(14)="NUMBER_OF_PROCESSORS   System specifies the number of processors installed on the computer."
s(15)="OS   System returns the OS name. Windows XP and Windows 2000 display the OS as Windows_NT."
s(16)="PATH   System specifies the search path for executable files."
s(17)="PATHEXT   System returns a list of the file extensions that the OS considers to be executable."
s(18)="PROCESSOR_ARCHITECTURE   System returns the processor's chip architecture. Values: x86, IA64."
s(19)="PROCESSOR_IDENTFIER   System returns a description of the processor."
s(20)="PROCESSOR_LEVEL   System returns the model number of the computer's processor."
s(21)="PROCESSOR_REVISION   System returns the revision number of the processor."
s(22)="PROMPT   Local returns the command-prompt settings for the current interpreter. Cmd.exe generates this variable."
s(23)="RANDOM   System returns a random decimal number between 0 and 32767. Cmd.exe generates this variable."
s(24)="SYSTEMDRIVE   System returns the drive containing the Windows root directory (i.e., the system root)."
s(25)="SYSTEMROOT   System returns the location of the Windows root directory."
s(26)="TEMP or TMP   System and User return the default temporary directories for applications that are available to users who are currently logged on. Some applications require TEMP and others require TMP."
s(27)="TIME   System returns the current time. This variable uses the same format as the time /t command. Cmd.exe generates this variable. For more information about the time command, see the Time command."
s(28)="USERDOMAIN   Local returns the name of the domain that contains the user's account."
s(29)="USERNAME   Local returns the name of the user currently logged on."
s(30)="USERPROFILE   Local returns the location of the profile for the current user."
s(31)="WINDIR   System returns the location of the OS directory."

Function StringSplit(s_in As String,chars As String,result() As String) As Long
    Dim As Long ctr,ctr2,k,n,LC=len(chars)
    dim As boolean tally(Len(s_in))
    #macro check_instring()
        n=0
        while n<Lc
        If chars[n]=s_in[k] Then
        tally(k)=true
        If (ctr2-1) Then ctr+=1
        ctr2=0
        exit while
        end if
        n+=1
       wend
    #endmacro
   
    #macro split()
    If tally(k) Then
        If (ctr2-1) Then ctr+=1:result(ctr)=Mid(s_in,k+2-ctr2,ctr2-1)
        ctr2=0
    End If
    #endmacro
    '==================  LOOP TWICE =======================
    For k  =0 To Len(s_in)-1
        ctr2+=1:check_instring()
    Next k
    if ctr=0 then
         if len(s_in) andalso instr(chars,chr(s_in[0])) then ctr=1
         end if
    If ctr Then Redim result(1 To ctr): ctr=0:ctr2=0 Else  Return 0
    For k  =0 To Len(s_in)-1
        ctr2+=1:split()
    Next k
    '===================== Last one ========================
    If ctr2>0 Then
        Redim Preserve result(1 To ctr+1)
        result(ctr+1)=Mid(s_in,k+1-ctr2,ctr2)
    End If
    Return Ubound(result)
End Function

redim as string g()
for n as long=1 to 31
    StringSplit(s(n),chr(9,32),g())
    print "Environment variable = ";g(1)
    shell("echo "+"%"+g(1)+"%")  'gets those listed
  ' print environ(g(1))          'gets some of those listed
    print"______________________"
next
print "Done . . ."
sleep

 

There are some differences here between 32 bits and 64 bits
For instance in 32 bits my PROCESSOR_ARCHITECTURE is x86
on 64 bits it is AMD64
jj2007
Posts: 1726
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 27, 2020 16:02

dodicat wrote:environ only seems to catch some of windows environment variables.
It depends on where you are looking:

Code: Select all

HKCU\Environment
HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment
... and on who's looking, an administrator or an ordinary user. Besides, 32-bit code may not see the same variables as 64-bit executable.
dodicat
Posts: 6726
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Postby dodicat » Feb 27, 2020 20:23

jj2007
Just noting that here:
shell gets all the listed environment variables.
environ only gets a few.

Maybe other boxes are different, I don't know.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests