Jump to content

how to find file location - (Moved)


Recommended Posts

HI Eveyone!

Request your help on how to find the file location of other application using autoit script.

Let me explain the scenario for better understanding.

I open  a txt file using notepad++ from C:\User\RedAck\Desktop\test.txt

 

I want to write a script in such a way that it reads the file path (C:\User\RedAck\Desktop\) from Notepad++ or de we have a inbuilt function? 

Link to post
Share on other sites
16 hours ago, Earthshine said:

i answered that one here, that code will work, if you launch notepad++ from desktop it will display the message. I used a UDF for this

 

 

Thank you for your suggestion.

 

But i'm not looking for location of a process. I would like to find the location of files used by the process i.e., location of txt file used by Notepad++

Link to post
Share on other sites

I tested "Handle" and it seems to work quite good and delivers a lot of info. However it does not show the file opened by notepad++.exe, and the reason for this might be, that there is no open handle, the program reads the data and closes the handle again (e.g. file is deleteable).

I could not think of another global method to find open files, I guess it comes down to program specifics, e.g. this seems to work for notepad++.exe.

MsgBox(0,"",StringReplace(WinGetTitle("[CLASS:Notepad++]")," - Notepad++",""))

 

Edited by KaFu
Link to post
Share on other sites

I’ve use this myself and I got the idea from process explorer which that handle program comes from. 

So, if you go to a text file and right click on it and say 'Edit', this is what you get. If you just open Notepad++ by yourself and then load in a file, it will just display Notepad++ executable location. You can only get the file it's opened this way in my example by passing Notepad a file parameter to edit. Other than that, you could run a Save As on the application such as notepad and capture the file path that way. Anyway, good luck moving forward.

#include "_ProcessListProperties.au3"
#include <Array.au3>

ShellExecute('Notepad.exe', 'C:\Users\Peter\Desktop\InstalledSoftware.csv')


$avRET = _ProcessListProperties("notepad.exe")
_ArrayDisplay($avRET)
$cmdline = $avRet[1][9]
MsgBox($MB_SYSTEMMODAL, 'Cmdline or where is my process called from? ', $cmdline)

 

Capture2.PNG

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to post
Share on other sites

ok, because I am a bulldog and don't let to let things go, here is an even better example of how to do it!

Credit goes to Ripdad!!

change this line to the app you are interested in, make it Notepad++ or whatever. I used this as a test for myself to look at the csv file in excel. 

Local $proc = "Excel.exe" ; make that whatever application you are looking for
; FileInUse with Info using WMI

; example one with process
Local $proc = "Excel.exe"
Local $rtn = FileInUseInfo($proc, 1)
If (Not @error) And $rtn Then MsgBox(0, 'FileInUseInfo(' & $proc & ', 1)', $rtn)

; example two with file
Local $file = @ScriptName
Local $rtn = FileInUseInfo($file, 2)
If (Not @error) And $rtn Then MsgBox(0, 'FileInUseInfo(' & $file & ', 2)', $rtn)


;##############################################################################################
; FileInUseInfo - Modify it for your needs
;
; example $mode = 1; FileInUseInfo("AutoIt3.exe", 1) ;returns file(s) used by process
;
; example $mode = 2; FileInUseInfo("SomeFile.txt", 2) ;returns process(s) using file
;##############################################################################################
Func FileInUseInfo($sInput, $mode = 1, $strComputer = "localhost")
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    If @error Then Return SetError(-1)
    Local $output = '', $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", 0x10 + 0x20)
    If Not IsObj($colItems) Then Return SetError(-2)
    For $objItem In $colItems
        If $objItem.CommandLine = '' Or StringLeft($objItem.CommandLine, 1) = '\' Then ContinueLoop
        If $mode = 1 And $objItem.Name = $sInput Then
        ElseIf $mode = 2 And StringInStr($objItem.CommandLine, $sInput) Then
        Else
            ContinueLoop
        EndIf
        $output &= "PID: " & $objItem.ProcessId & @CRLF
        $output &= "CommandLine: " & $objItem.CommandLine & @CRLF & @CRLF
    Next
    Return $output
EndFunc

 

Capture.PNG

EDIT: The second method seems more reliable. Even if i manually open notepad and drop a file on it, it reports the commandline correctly. @Ripdad

 method works best IMO

 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to post
Share on other sites
21 hours ago, Earthshine said:

Did you try the code i posted? You must not have. Ok. On your own now. I’ve wasted time here I see

I'm really very sorry for not trying the code. I just read the topic and replied.

Link to post
Share on other sites
19 hours ago, Earthshine said:

ok, because I am a bulldog and don't let to let things go, here is an even better example of how to do it!

Credit goes to Ripdad!!

change this line to the app you are interested in, make it Notepad++ or whatever. I used this as a test for myself to look at the csv file in excel. 

Local $proc = "Excel.exe" ; make that whatever application you are looking for
; FileInUse with Info using WMI

; example one with process
Local $proc = "Excel.exe"
Local $rtn = FileInUseInfo($proc, 1)
If (Not @error) And $rtn Then MsgBox(0, 'FileInUseInfo(' & $proc & ', 1)', $rtn)

; example two with file
Local $file = @ScriptName
Local $rtn = FileInUseInfo($file, 2)
If (Not @error) And $rtn Then MsgBox(0, 'FileInUseInfo(' & $file & ', 2)', $rtn)


;##############################################################################################
; FileInUseInfo - Modify it for your needs
;
; example $mode = 1; FileInUseInfo("AutoIt3.exe", 1) ;returns file(s) used by process
;
; example $mode = 2; FileInUseInfo("SomeFile.txt", 2) ;returns process(s) using file
;##############################################################################################
Func FileInUseInfo($sInput, $mode = 1, $strComputer = "localhost")
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    If @error Then Return SetError(-1)
    Local $output = '', $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", 0x10 + 0x20)
    If Not IsObj($colItems) Then Return SetError(-2)
    For $objItem In $colItems
        If $objItem.CommandLine = '' Or StringLeft($objItem.CommandLine, 1) = '\' Then ContinueLoop
        If $mode = 1 And $objItem.Name = $sInput Then
        ElseIf $mode = 2 And StringInStr($objItem.CommandLine, $sInput) Then
        Else
            ContinueLoop
        EndIf
        $output &= "PID: " & $objItem.ProcessId & @CRLF
        $output &= "CommandLine: " & $objItem.CommandLine & @CRLF & @CRLF
    Next
    Return $output
EndFunc

 

Capture.PNG

EDIT: The second method seems more reliable. Even if i manually open notepad and drop a file on it, it reports the commandline correctly. @Ripdad

 method works best IMO

 

I'm really very thank full and sorry to you. I have modified the code as per our requirement but, we are facing issues. could you please help us on that.

Capture.PNG

As you can see this the location is populated using 

$output &= "CommandLine: " & $objItem.CommandLine & @CRLF & @CRLF

Along with application path and file name. I tried to trim that value so that i can get only "C:\Users\Peter\Desktop\" using following command

$output = $objItem.CommandLine
$convert = _ArrayToString($output)
$filepath = _StringBetween($output, " ", "$filename")[1]

 

Getting following error

"C:\Program Files (x86)\AutoIt3\Include\String.au3" (37) : ==> Subscript used on non-accessible variable.:
Func _StringBetween($sString, $sStart, $sEnd, $iMode = $STR_ENDISSTART, $bCase = False)
Func _StringBetween($sString, $sStart, $sEnd,^ ERROR

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...