path + file name

pumkindrvr
pumkindrvr used Ask the Experts™
on
How do I combine the path returned by user with file name?
[code]
Name "faq.txt" As "qaf.txt"
[/code]
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Dim Pth As String
Pth = "C:\" ' Path from user
If Right(Pth, 1) <> "\" Then
    Pth = Pth & "\"
End If
Name Pth & "faq.txt" As Pth & "qaf.txt"

Author

Commented:
That looks good, but I must be doing it wrong.

[code]
Option Explicit

Private Sub Command1_Click()
Dim bi As BROWSEINFO
  Dim pidl As Long
  Dim path As String
  Dim pos As Integer
Label1.Caption = ""
bi.hOwner = Me.hWnd
 bi.pidlRoot = 0&
 bi.lpszTitle = "Select your Windows\System\ directory"
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space$(MAX_PATH)
    If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
     pos = InStr(path, Chr$(0))
     Label1.Caption = Left(path, pos - 1)
  End If
Call CoTaskMemFree(pidl)
  Dim Pth As String
Pth = "C:\" ' Path from user
If Right(Pth, 1) <> "\" Then
   Pth = Pth & "\"
End If
Name Pth & "1\1.txt" As Pth & "qaf.txt"

  End Sub
Private Sub Command2_Click()
Dim bi As BROWSEINFO
  Dim pidl As Long
  Dim path As String
  Dim pos As Integer
    Label2.Caption = ""
bi.hOwner = Me.hWnd
bi.pidlRoot = 0&
bi.lpszTitle = "Select your Windows\System\ directory"
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
     pos = InStr(path, Chr$(0))
     Label2.Caption = Left(path, pos - 1)
  End If
Call CoTaskMemFree(pidl)
  End Sub
[/code]

Author

Commented:
That looks good, but I must be doing it wrong.

[code]
Option Explicit

Private Sub Command1_Click()
Dim bi As BROWSEINFO
  Dim pidl As Long
  Dim path As String
  Dim pos As Integer
Label1.Caption = ""
bi.hOwner = Me.hWnd
 bi.pidlRoot = 0&
 bi.lpszTitle = "Select your Windows\System\ directory"
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space$(MAX_PATH)
    If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
     pos = InStr(path, Chr$(0))
     Label1.Caption = Left(path, pos - 1)
  End If
Call CoTaskMemFree(pidl)
  Dim Pth As String
Pth = "C:\" ' Path from user
If Right(Pth, 1) <> "\" Then
   Pth = Pth & "\"
End If
Name Pth & "1\1.txt" As Pth & "qaf.txt"

  End Sub
Private Sub Command2_Click()
Dim bi As BROWSEINFO
  Dim pidl As Long
  Dim path As String
  Dim pos As Integer
    Label2.Caption = ""
bi.hOwner = Me.hWnd
bi.pidlRoot = 0&
bi.lpszTitle = "Select your Windows\System\ directory"
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
     pos = InStr(path, Chr$(0))
     Label2.Caption = Left(path, pos - 1)
  End If
Call CoTaskMemFree(pidl)
  End Sub
[/code]
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Yes...you added a "1" in the quotes in this line:

Name Pth & "1\1.txt" As Pth & "qaf.txt"

it should be:

Name Pth & "\1.txt" As Pth & "qaf.txt"

--
Also, does the path from the user include a file name, or just the folder?  When you added a "1", where you assuming that whatever the user picked had a "1" at the end of the path?

Author

Commented:
The user's path is only the directory. I already have the file names.

Commented:
OK, so why were you adding a "1" to the user's path?  Was it just a slip of the fingers when typing?

Author

Commented:
yes, guess I need thicker glasses. lol

Commented:
Well, points to vinnyd79 for offering correct code first.

Author

Commented:
But it still doesn't work. I get error at the name line.

Option Explicit

Private Sub Command1_Click()
Dim bi As BROWSEINFO
 Dim pidl As Long
 Dim path As String
 Dim pos As Integer
Label1.Caption = ""
bi.hOwner = Me.hWnd
bi.pidlRoot = 0&
bi.lpszTitle = "Select your Windows\System\ directory"
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space$(MAX_PATH)
   If SHGetPathFromIDList(ByVal pidl, ByVal path) Then
    pos = InStr(path, Chr$(0))
    Label1.Caption = Left(path, pos - 1)
 End If
Call CoTaskMemFree(pidl)
Name Path & "faq.txt" As Pth & "qaf.txt"

 

Commented:
OK...but what happened to vinnyd79's code to add the "\"?  And why isn't the variable pth defined?

Name Path & "\faq.txt" As Pth & "\qaf.txt"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial