Sign in to follow this  
Followers 0
scsnake

how to get full path of a file

19 posts in this topic

Is there any method to get the full path of a file?

ie. There's a Office Word file opened, and I want to get its full path in the computer.

If impossible, any idea not using autoit?

Share this post


Link to post
Share on other sites



Well generally you already know the full path of a file you opened.

Some programs load a file into memory and leave a handle open to the file (Adobe Acrobat Professional), whereas others read in the file and release the handle (Notepad).

You may be able to get a list of open file handles, which would include every single file you don't "see" running. There is no generic way of seeing what documents you have open, you need to narrow it down to the exact programs you want to check because the method will probably differ.

Share this post


Link to post
Share on other sites

thx for your reply

I am managing with M$ Office documents.... to be specific, it is M$ PowerPoint 2003

any hints would be appreciated :D

Share this post


Link to post
Share on other sites

scsnake

You need this?

$pid = ProcessExists("notepad.exe") 
MsgBox(0, '', _ProcessGetLocation($pid)) 
 
Func _ProcessGetLocation($iPID) 
    Local $aProc = DllCall('kernel32.dll', 'hwnd', 'OpenProcess', 'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $iPID) 
    If $aProc[0] = 0 Then Return SetError(1, 0, '') 
    
    Local $vStruct = DllStructCreate('int[1024]') 
    
    DllCall('psapi.dll', 'int', 'EnumProcessModules', 'hwnd', $aProc[0], 'ptr', DllStructGetPtr($vStruct), _
            'int', DllStructGetSize($vStruct), 'int*', 0) 
    
    Local $aReturn = DllCall('psapi.dll', 'int', 'GetModuleFileNameEx', 'hwnd', $aProc[0], _
                             'int', DllStructGetData($vStruct, 1), 'str', '', 'int', 2048) 
    
    If StringLen($aReturn[3]) = 0 Then Return SetError(2, 0, '') 
    
    Return $aReturn[3] 
EndFunc

Share this post


Link to post
Share on other sites

He wants the path to the actual document that is open, not the program.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

$oWord = ObjGet("","Word.Application")
$oDocs = $oWord.Documents
For $o In $oDocs
    ConsoleWrite($o.FullName & @CRLF)
Next

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

$oWord = ObjGet("","Word.Application")
$oDocs = $oWord.Documents
For $o In $oDocs
    ConsoleWrite($o.FullName & @CRLF)
Next
I'm not familiar with COM >"<

can you tell me how to use it .... what to do after "Next" ??

Share this post


Link to post
Share on other sites

hi,

i'd like to Bump this thread, because i'm looking for the same thing: Getting the full path of an opened file in an application, such as MS word, or MS Excel.

everything i fould returns the path of the application, not of the opened file.

Thanks in advance

Share this post


Link to post
Share on other sites

One way you could do it is to find the documents that are open by looking at the Window title for all the Word processes open, then searching the registry under the HKCU\Software\Microsoft\Office\12.0\Word\File MRU key for the open document. The File MRU key has the full paths of all open documents for Word/Excel/etc. By the way, the 12.0 value is only valid for Office 2007, Office 2003 would be under 11.0 as an example.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

For me Siaos example above works fine.

$oWord = ObjGet("","Word.Application")
; http://msdn.microsoft.com/de-de/library/ms265834%28v=Office.11%29.aspx
$oDocs = $oWord.Documents
For $o In $oDocs
    ConsoleWrite($o.Path & @CRLF)
    ConsoleWrite($o.FullName & @CRLF)
Next

Additionally to the Word.Application object MS Office seems to expose also "Excel.Application" and "PowerPoint.Application" objects. Google's your friend to find the right methods and properties :).

Share this post


Link to post
Share on other sites

thanks for all replies,

@BrewManNH:

thanks for that suggestion. I'm not experienced at all with registry stuff, but i'll try.

@KaFu:

yeah that works but i forgot to mention, i can't seem to be able to get Word application object safely. _WordAttach fails sometimes and ObjGet is even worse. I don't know why but i quite gave up, so i'm looking for some way that is Object-free :)

Share this post


Link to post
Share on other sites

well...

$var = RegRead("HKCU\Software\Microsoft\Office\12.0\Word\", "File MRU")

ConsoleWrite("return  value: "& $var & @CRLF & "error: "&@error&  @CRLF&"extended: "&@extended&@CRLF)

this returns:

return value:

error: -1

extended: 2

any suggestion?

Share this post


Link to post
Share on other sites

You're sure that the key exists? I got ...\Office\14.0\Word\ and no "File MRU" at all. Maybe try:

#AutoIt3Wrapper_UseX64=n

Might also work for the ObjGet part :) ...

Share this post


Link to post
Share on other sites

#AutoIt3Wrapper_UseX64=n doesn't change anything. i have a 32bit winword on a 32bit system, why should x64 work?

anyway, the requested key should exist, from what i read in the helpfile, error -1 means that value doesn't exists, while error 1 means that the whole key doesnt exist. I get error 1 if i change the path, for example if i use RegRead("HKCU\Software\Microsoft\Office\11.0\Word\", "File MRU") error is 1. so The key seem to exist from what i understand.

Share this post


Link to post
Share on other sites

any suggestion?

This script returns last files used on Office Word. Don't know whether this is useful to you but...

Global $var[100]
Global $officeVersion="14.0" ;change it to your version

For $i = 1 to 100
    $var[$i-1] = RegEnumVal("HKCU\Software\Microsoft\Office\" & $officeVersion & "\Word\File MRU\", $i)
    If @error <> 0 Then
        ExitLoop
    EndIf
Next

For $i=0 To Ubound($var)-1
    If StringInStr($var[$i], "Item") <> 0 Then
        $result=StringRegExp(RegRead("HKCU\Software\Microsoft\Office\" & $officeVersion & "\Word\File MRU\", $var[$i]),".*\*(.*)",1)
        ConsoleWrite($var[$i] & " value is: " & $result[0] & @CRLF)
    EndIf
Next

Note: Keep in mind that I did the script in a hurry so maybe there are a lot of ways to do it much better

Share this post


Link to post
Share on other sites

thanks! it works!

Share this post


Link to post
Share on other sites

to anyone with the same problem: sahsanu's method above works great, but from what i have seen (4 PCs) Office 12.0 (2007) works good, while Office 11.0 (2003) doesn't store recent files in that position. To find the correct registry path just open a word document (so that it is included in the recent used list) then open regedit (run->regedit) and search for the name or path of the file just used.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0