Results 1 to 13 of 13

Thread: [RESOLVED] Directory

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Resolved [RESOLVED] Directory

    Can anyone help me?


    1. How can I get the directory / folder for desktop?
    If I want to make a program that write a .txt file directly to the desktop, how can I get the desktop folder so that when the program is copied to another computer, I won't have to change the directory again and again? (Without using commondialog control or browsing for the folder)


    2. And also, for the same case, how can I get the directory of the startup menu. I want to copy a file to the startup menu that will enable the computer to start the program every time the computer is started.

    3. How can I know (how to check) whether a file exists or not. e.g. I want to write a .txt file, but before creating (writing) the file, I want to check whether the file exists or not.


    Thanks.
    Last edited by Hack; Apr 20th, 2006 at 06:05 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark
    The only thing for the triumph of evil is for a good man to do nothing

  2. #2
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: Directory

    I'm not sure about the paths ... but you can check if the file is there or not like this:
    VB Code:
    1. If Dir(App.Path & "\file.txt") <> "" Then
    2.     MsgBox "Found: file.txt" 'display message
    3.     Kill App.Path & "\file.txt" 'delete this file
    4. End If

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Directory

    Ok, thanx.

    Yet, I'm still looking for assistance if someone knows how to get the folder for the desktop and startup menu automatically when using the program in another computer.

    Thanx.
    Last edited by steven_luck1; Apr 15th, 2006 at 10:34 AM.
    The only thing for the triumph of evil is for a good man to do nothing

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Directory

    Add the following to the General Declaration section of a Form or a Module.
    VB Code:
    1. Private Declare Function SHGetSpecialFolderPath _
    2.  Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" ( _
    3.     ByVal hWnd As Long, _
    4.     ByVal pszPath As String, _
    5.     ByVal csidl As Long, _
    6.     ByVal fCreate As Long _
    7. ) As Long
    8.  
    9. Public Function GetDesktopPath() As String
    10.     Const CSIDL_DESKTOPDIRECTORY As Long = &H10
    11.     Const MAX_PATH As Long = 260
    12.     Dim sPath As String
    13.     sPath = Space(MAX_PATH)
    14.     SHGetSpecialFolderPath(0, sPath, CSIDL_DESKTOPDIRECTORY, False)
    15.     GetDesktopPath = Left$(sPath, InStr(sPath, vbNullString) - 1)
    16. End Function
    17.  
    18. Public Function GetStartMenuPath(Optional ByVal blnAllUsers As Boolean)
    19.     Const CSIDL_COMMON_STARTMENU As Long = &H16
    20.     Const CSIDL_STARTMENU As Long = &Hb
    21.     Const MAX_PATH As Long = 260
    22.     Dim sPath As String, nFlag As Long
    23.     sPath = Space(MAX_PATH)
    24.     If blnAllUsers = True Then
    25.         nFlag = CSIDL_COMMON_STARTMENU
    26.     Else
    27.         nFlag = CSIDL_STARTMENU
    28.     End If
    29.     SHGetSpecialFolderPath(0, sPath, nFlag, False)
    30.     GetStartMenuPath = Left$(sPath, InStr(sPath, vbNullString) - 1)
    31. End Function
    Call GetDesktopPath to get the physical path of where the desktop shortcut items are stored. Call GetStartMenuPath for the Start menu Programs folder, pass True to it if you want it for all the users or pass False or nothing at all to get the path for the current user only.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Directory

    There're errors when I tried to use the code, Syntax error. I suppose:

    Private Declare Function SHGetSpecialFolderPath _
    Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" ( _
    ByVal hWnd As Long, _
    ByVal pszPath As String, _
    ByVal csidl As Long, _
    ByVal fCreate As Long _
    ) As Long

    is a function and a return value, right? Can we directly use:

    SHGetSpecialFolderPath(0, sPath, CSIDL_DESKTOPDIRECTORY, False)
    or
    SHGetSpecialFolderPath(0, sPath, nFlag, False)

    in the coding? Then it will be futile, isn't it? 'coz it's not used in the program. Can u help me with this again?

    Thanks.
    The only thing for the triumph of evil is for a good man to do nothing

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Directory

    Oh sorry. I wrote the code directly in the post... What you need to do is either remove the paranthesis or use the Call keyword.
    VB Code:
    1. Call SHGetSpecialFolderPath(0, sPath, ...)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Directory

    Actually I've tried it before. Though there's no error, still, it doesn't work. When I try to see the folder using print in the immediate window, nothing appears. If possible, I mean, if possible, can u compress and attach a simple program using the code above? And see what's wrong there. 'coz I've tried modifying it and just can't find something wrong there.

    call SHGetSpecialFolderPath(0, sPath, CSIDL_DESKTOPDIRECTORY, False)
    GetDesktopPath = Left$(sPath, InStr(sPath, vbNullString) - 1)

    when it's called in the first line, the value isn't used by any variable, and while returning the value in the "GetDesktopPath....", the function SHGetSpecialFolderPath.... is not used either, right??

    Sorry, don't really understand about coding using API.

    Thanx a lot.
    The only thing for the triumph of evil is for a good man to do nothing

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Directory

    The GetDesktopPath is a function on its own. You should call that like this:
    VB Code:
    1. MsgBox GetDesktopPath
    Or assign the return value into a variable
    VB Code:
    1. Dim sDesktop As String
    2. sDesktop = GetDesktopPath
    If you for example would call this from the click event of a CommandButton the code would look simular to this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Debug.Print GetDesktopPath
    3. End Sub
    It's true that SHGetSpecialFolderPath also is a function, the return value of this function isn't that important though since it should simply return 0 (on success) and a non-zero value if it should fail for some reason. But it would only fail if you wouldn't have a desktop folder path... which you of course have.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Directory

    It indeed returns 1 when I

    print SHGetSpecialFolderPath(0, sPath, CSIDL_DESKTOPDIRECTORY, False)

    Maybe u can help me with the attachment of the program down here.
    Attached Files Attached Files
    The only thing for the triumph of evil is for a good man to do nothing

  10. #10
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Directory

    This returns the desktop path for the current user under Win XP. Don't know about other OS's though. Joacim's solution is probably more robust...
    VB Code:
    1. Dim DesktopPath As String
    2.     DesktopPath = Environ$("UserProfile") & "\Desktop"
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Directory

    Quote Originally Posted by pnish
    This returns the desktop path for the current user under Win XP. Don't know about other OS's though. Joacim's solution is probably more robust...
    VB Code:
    1. Dim DesktopPath As String
    2.     DesktopPath = Environ$("UserProfile") & "\Desktop"
    The desktop folder might not be named "Desktop" on all localized versions of Windows.

    I've found my error... The call to SHGetSpecialFolderPath is successful (it returns TRUE = 1 on success not 0 as I first thought) the error is on the next line where we are supposed to trunc the string before the first NULL character. I used vbNullString where it should be vbNullChar, so in both functions change the last line to:
    VB Code:
    1. GetDesktopPath = Left$(sPath, InStr(sPath,[b] vbNullChar[/b]) - 1)

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Directory

    Ok, thanx very much. I've eventually got it.
    The only thing for the triumph of evil is for a good man to do nothing

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Directory

    This seems to works too with most computers. Thanx.

    Quote Originally Posted by pnish
    This returns the desktop path for the current user under Win XP. Don't know about other OS's though. Joacim's solution is probably more robust...
    VB Code:
    1. Dim DesktopPath As String
    2.     DesktopPath = Environ$("UserProfile") & "\Desktop"
    The only thing for the triumph of evil is for a good man to do nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width