Hi everyone,
Can you tell me a way to get the file path (filename) of a file when I "open with" it with my program. I mean that if I open a certain file with my software, I should get the file path of the file. Please Help:)
daniwaber 0 Light Poster
SpiritualMadMan 39 Junior Poster in Training
What method are you using to "Open" the file in Question?
I have a Form I copy into my project space when I want to access files...
Copy the code below into a text file and then rename it FN.frm
Because the FN form can be thought of as a separate Class or Object anything on the form can be accessed using FN. until the form is unloaded.
Let me know if it helps. :)
VERSION 5.00
Begin VB.Form FN
AutoRedraw = -1 'True
Caption = "Select Files"
ClientHeight = 7950
ClientLeft = 60
ClientTop = 345
ClientWidth = 4365
Icon = "FN.frx":0000
LinkTopic = "Form2"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 7950
ScaleWidth = 4365
StartUpPosition = 2 'CenterScreen
Begin VB.TextBox txtNewFile
Height = 435
Left = 105
TabIndex = 8
Text = "Text1"
Top = 3570
Width = 4110
End
Begin VB.ComboBox cmboExts
Height = 315
Left = 2205
TabIndex = 5
Text = "Combo1"
Top = 3000
Width = 1995
End
Begin VB.FileListBox File1
Height = 2820
Left = 2200
TabIndex = 2
Top = 90
Width = 2000
End
Begin VB.DirListBox Dir1
Height = 2790
Left = 120
TabIndex = 1
Top = 100
Width = 2000
End
Begin VB.DriveListBox Drive1
Height = 315
Left = 100
TabIndex = 0
Top = 2940
Width = 2000
End
Begin VB.Label Mode
AutoSize = -1 'True
Caption = "0"
Height = 195
Left = 1050
TabIndex = 9
Top = 6720
Visible = 0 'False
Width = 90
End
Begin VB.Label FileName
AutoSize = -1 'True
Height = 195
Left = 105
TabIndex = 7
Top = 6015
Width = 45
End
Begin VB.Label DelimChar
AutoSize = -1 'True
Height = 195
Left = 100
TabIndex = 6
Top = 5580
Width = 45
End
Begin VB.Label lblSizeIt
AutoSize = -1 'True
Height = 195
Left = 105
TabIndex = 4
Top = 6435
Width = 45
End
Begin VB.Label Mask
AutoSize = -1 'True
Height = 195
Left = 105
TabIndex = 3
Top = 6870
Width = 45
End
End
Attribute VB_Name = "FN"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Must be used Modal
' On Entry the Drive1 and Dir1 will return a correct value. But, the File1 will be empty
' This will require a check to see if File1 is empty or zero length
' On exit:
' if File1 was double-clicked to select a filename File1 will hold that filename.
' if exit was due to using the Close "X" then File1 is indeterminate and may be empty.
' Dir1 will include current Drive and Path WITHOUT a trailing "\"
Option Explicit
Private Sub cmboexts_Change()
'If Not Me.Visible Then Exit Sub
FN.File1.Pattern = "*" & Me.cmboExts.Text
FN.File1.Refresh
End Sub
Private Sub Dir1_Change()
FN.File1.Path = FN.Dir1.Path
End Sub
Private Sub Dir1_Click()
FN.File1.Path = FN.Dir1.Path
End Sub
Private Sub Drive1_Change()
On Error GoTo usrerror
FN.Dir1.Path = FN.Drive1.Drive
FN.File1.Path = FN.Dir1.Path
On Error GoTo 0
Exit Sub
usrerror:
Dim UsrMsg As String
Dim PigeonHole As String
Select Case Err.Number
Case 68 'Device Unavailable - Ignore
Case Else
UsrMsg = "Error: " & Err.Number & " " & Err.Description
End Select
PigeonHole = MsgBox(UsrMsg, vbOKOnly, "Error!")
Resume Next
End Sub
Private Sub File1_DblClick()
Me.FileName = Me.File1
' Debug.Print "Double-Clicked"
' Debug.Print "Drive: " & Me.Drive1
' Debug.Print "Directory: " & Me.Dir1
' Debug.Print "File: " & Me.FileName
' Debug.Print " "
Me.Hide
End Sub
Private Sub Form_Activate()
Dim xStr As String
Dim n As Long
File1.Pattern = FN.cmboExts.Text
FN.Dir1.Refresh
FN.File1.Refresh
Call Form_Resize
End Sub
Private Sub Form_Load()
Me.cmboExts = "*.*"
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' Debug.Print "Query Unload"
' Debug.Print "Drive: " & Me.Drive1
' Debug.Print "Directory: " & Me.Dir1
' Debug.Print "File: " & Me.FileName
' Debug.Print " "
End Sub
Private Sub Form_Resize()
If Me.Width < 5000 Then Me.Width = 5000
If Me.Height < 4000 Then Me.Height = 4000
Me.Dir1.Top = 100
Me.File1.Top = 100
Me.Dir1.Width = (Me.Width - 400) / 2
Me.File1.Width = Me.Dir1.Width
Me.Dir1.Left = 100
Me.File1.Left = Me.Dir1.Width + 200
If FN.Mode = "1" Then 'New File Mode
FN.txtNewFile.Visible = True
Me.Dir1.Height = Me.Height - 1400
Else 'Open File Mode
FN.txtNewFile.Visible = False
Me.Dir1.Height = Me.Height - 800
End If
Me.File1.Height = Me.Dir1.Height
Me.Drive1.Top = Me.Dir1.Height + Me.Dir1.Top + 50
Me.Drive1.Width = Me.Dir1.Width
Me.cmboExts.Left = Me.File1.Left
Me.cmboExts.Top = Me.Drive1.Top
Me.cmboExts.Width = Me.File1.Width
Me.txtNewFile.Top = Me.Drive1.Top + 400
Me.txtNewFile.Left = 100
End Sub
Private Sub txtNewFile_KeyPress(KeyAscii As Integer)
Dim tStr As String
If KeyAscii = 13 Then
StartAt = 1
'RetStr = Right(Me.cmboExts, 3)
RetStr = "."
uIn = Me.txtNewFile
tStr = Scan_To
If RetStr = "Not Found" Then
FN.FileName = txtNewFile & "." & Right(Me.cmboExts, 3)
Else
If UCase(Right(Me.txtNewFile, 3)) <> UCase(Right(Me.cmboExts, 3)) Then
FN.FileName = Left(txtNewFile, StoppedAt) & Right(Me.cmboExts, 3)
End If
End If
Me.Hide
End If
End Sub
daniwaber 0 Light Poster
I actually mean that I open "My Computer" and right click a file, the click "Open With" and select my program. Then my program should get the file's path.
hkdani 39 Posting Pro in Training
You can use a Common Dialog Control. Add the Microsoft Common Dialog Control component to your project.
This control will return a Path to a file. Or you can use API's to do same. Little bit more flexibility.
But really. What you're requesting is not very clear at all. And I'm able to make little sense of what you really want.
If you have already opened the program, you should already know the path. Until you explain clearly what you really want to do no one can really explain to you how to do what you wish.
Here's an example of using the CommonDialog API's to return a Path.
Option Explicit
Private Const BIF_RETURNONLYFSDIRS = &H1 ' // For finding a folder to start document searching
Private Const BIF_DONTGOBELOWDOMAIN = &H2 ' // For starting the Find Computer
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_RETURNFSANCESTORS = &H8
Private Const BIF_EDITBOX = &H10
Private Const BIF_VALIDATE = &H20 ' // insist on valid result (or CANCEL)
Private Const BIF_BROWSEFORCOMPUTER = &H1000 ' // Browsing for Computers.
Private Const BIF_BROWSEFORPRINTER = &H2000 ' // Browsing for Printers
Private Const BIF_BROWSEINCLUDEFILES = &H4000 ' // Browsing for Everything
Private Const OFN_READONLY = &H1
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_NOCHANGEDIR = &H8
Private Const OFN_SHOWHELP = &H10
Private Const OFN_ENABLEHOOK = &H20
Private Const OFN_ENABLETEMPLATE = &H40
Private Const OFN_ENABLETEMPLATEHANDLE = &H80
Private Const OFN_NOVALIDATE = &H100
Private Const OFN_ALLOWMULTISELECT = &H200
Private Const OFN_EXTENSIONDIFFERENT = &H400
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_CREATEPROMPT = &H2000
Private Const OFN_SHAREAWARE = &H4000
Private Const OFN_NOREADONLYRETURN = &H8000
Private Const OFN_NOTESTFILECREATE = &H10000
Private Const OFN_NONETWORKBUTTON = &H20000
Private Const OFN_NOLONGNAMES = &H40000 ' // force no long names for 4.x modules
' #if(WINVER >= 0x0400)
Private Const OFN_EXPLORER = &H80000 ' // new look commdlg
Private Const OFN_NODEREFERENCELINKS = &H100000
Private Const OFN_LONGNAMES = &H200000 ' // force long names for 3.x modules
Private Const OFN_ENABLEINCLUDENOTIFY = &H400000 ' // send include message to callback
Private Const OFN_ENABLESIZING = &H800000
Private Type SHITEMID
cb As Long
abID(1) As Long
End Type
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Declare Function GetOpenFileName Lib "comdlg32" Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32" Alias "GetSaveFileNameA" (lpofn As OPENFILENAME) As Long
Private Type BROWSEINFO
hwndOwner As Long
pidlRoot As ITEMIDLIST
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private shl As Shell
Private Sub Command1_Click()
On Error GoTo Path_ERR
Set shl = New Shell
Dim strPathReturned As String
Dim fFileSearch As Boolean
Dim strBuffer As String * 255
Dim ofn As OPENFILENAME
With ofn
.flags = OFN_EXPLORER
.hwndOwner = Me.hWnd
.hInstance = 0
.lpstrInitialDir = shl.NameSpace(ssfDRIVES).Items.Item.Path
.lStructSize = Len(ofn)
.lpstrFile = String(255, Chr$(0))
.nMaxFileTitle = 255
.lpstrFileTitle = strBuffer
.nMaxFile = 256
End With
fFileSearch = CBool(GetOpenFileName(ofn))
strPathReturned = ofn.lpstrFile
strPathReturned = Left(strPathReturned, InStr(1, strPathReturned, Chr$(0), vbTextCompare) - 1)
Debug.Print strPathReturned
Exit Sub
Path_ERR:
Debug.Print Err.Description, Err.Number
End Sub
Private Sub Form_Load()
Set shl = New Shell
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set shl = Nothing
End Sub
SpiritualMadMan 39 Junior Poster in Training
Another thought...
When I expect more than one file to be opened. For instance I have a Test Procedure File (.prc) and an INI file I will put all the handler routines in a module an (shudder) use Global variables (if needed, due to Multiple Forms and Multiple Module Sturcture) for the Filenames and File Handles.
If a File Handle equals zero then the file hasn't been opened.
I use the same FN form and take the FN.Filename and FN.Dir to assisn the Path and Filename to the variable and when I open the file I first get INI_NUM = Freefile and use that handle throughout the project to access the file.
daniwaber 0 Light Poster
I am actually making a notepad-like program. So I need to open the txt files in it like we normally open the files by double clicking it in My Computer. Any Suggestions?
Drycola 10 Junior Poster in Training
It had been a long time since I used VB6. But I remember I did such a thing long time ago.
I think you can use the variable "Command" to get the file name.
Dim Filename As String
Filename = Command
Open Filename For Binary Access Read As #1 'or use your Open code instead ;)
SpiritualMadMan 39 Junior Poster in Training
I actually mean that I open "My Computer" and right click a file, the click "Open With" and select my program. Then my program should get the file's path.
That would be SomeVarName = App.Path
This gives you the Path where your Application is at...
Specific FileNames would be SomeVarName = App.Path & "\" & YourAppsFileName
Edited by SpiritualMadMan because: n/a
daniwaber 0 Light Poster
It had been a long time since I used VB6. But I remember I did such a thing long time ago.
I think you can use the variable "Command" to get the file name.Dim Filename As String Filename = Command Open Filename For Binary Access Read As #1 'or use your Open code instead ;)
Thanks, it solved my problem:)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.