[RESOLVED] Obtain file location from Window caption
Hi,
I'm wondering how to get the file location when I give the program a certain caption.
So for example, I can create test.txt at:
C:\test.txt
I'll open the txt document, and the caption will give me
test.txt - Notepad
My question is, how would I go backwards from the "test.txt - Notepad" caption to obtain that window's actual location, which in this case is C:\test.txt?
I did manage to find an example in Delphi, but no luck finding one in vb. In this example, you give the window's handle:
http://www.delphitricks.com/source-c...ow_handle.html
Re: Obtain file location from Window caption
Store the files full path when you open it.. then just show what you want:
Code:
Dim strFullFilePath As String
strFullFilePath = OpenFile("C:\test.txt.")
Me.Caption = GetFileName(strFullFilePath)
' do something with strFullFilePath
chem
Re: Obtain file location from Window caption
The problem is that I don't have the given file location to use your code with.
I don't know where the file is stored, thats why I'm trying to find it based off the window's caption.
Re: Obtain file location from Window caption
So.. you want to search a hard-drive for files named like the caption?
chem
Re: Obtain file location from Window caption
No,
Lets say I have any EXE open. Lets say the caption of it is "The program"
Is it possible to take that caption (I can retrieve the window handle) and get that file's actual location?
I would like to get a file's path from the window's handle, basically.
Thanks
Re: Obtain file location from Window caption
Window handles and their paths have no correlation. Unless you have physical access to the programs code.. there is no way to get its path.
You can however, just search the harddrive for, lets say, "Notepad". But then, it won't always work, because you have things like "MSN Messenger" that are called "msnmsgr.exe".
chem
Re: Obtain file location from Window caption
I've actually found the code for it in the codebank:
http://www.vbforums.com/showthread.php?t=241198
but one small problem. Lets say I want to find the location of a textfile, named C:\file.txt. The problem is, when I use that open text file's hWND, I will get returned notepad.exe's location instead of C:\file.txt.
Any way around this?
Re: Obtain file location from Window caption
Quote:
Originally Posted by k0zz
I've actually found the code for it in the codebank:
http://www.vbforums.com/showthread.php?t=241198
but one small problem. Lets say I want to find the location of a textfile, named C:\file.txt. The problem is, when I use that open text file's hWND, I will get returned notepad.exe's location instead of C:\file.txt.
Any way around this?
That code uses a process's information to get what it needs. A file handle isn't a process, therefore, in theory, its not possible.
There would be a way to do it.. but nothing springs to mind that would be easy/worth doing.
Why is it you need this?
chem
Re: Obtain file location from Window caption
Well, im making this for a user that has some annoying program on his computer that he can only recognize by its caption. I can have my program find the caption then end it, but it has a nasty habit of coming back up at some later time. I wanted to be able to find the the EXE's filepath so I can delete it. It also seems to be creating random log files and other kinds of txt documents, and shelling them, and ending them, at random. I wanted to see where it was storing them.
So, its actually possible to get its value? In theory, I guess I could just take the meaningful part of the .txt document's caption and do a search for it on the harddrive, but is there any easier way of doing it?
Re: Obtain file location from Window caption
Found this, might be a start? I don't know much about this sort of code.
Code:
Private Sub Command1_Click()
Dim objServices, objProcessSet, Process
Dim results
Set objServices = GetObject("winmgmts:")
Set objProcessSet = objServices.ExecQuery("Select * from Win32_Process")
For Each Process In objProcessSet
If (UCase("Notepad.exe") = UCase(Process.Caption)) Then
results = Split(Process.commandline, """ ", , vbTextCompare)
Debug.Print Trim(results(1))
End If
Next
End Sub
Re: Obtain file location from Window caption
Woah, Edgemeal your code works perfectly.
Re: Obtain file location from Window caption
I just found the code somewhere and just changed it to notepad.exe,
Probably want to add this before the end sub,
Set objServices = Nothing
Set objProcessSet = Nothing
Though I'm not at all knowledgeable in the "winmgmts:" dept. ;P
Good luck!