RedAck Posted May 29, 2019 Share Posted May 29, 2019 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 comment Share on other sites More sharing options...
Earthshine Posted May 29, 2019 Share Posted May 29, 2019 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 My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted May 29, 2019 Moderators Share Posted May 29, 2019 Moved to the appropriate forum. Moderation Team "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
RedAck Posted May 30, 2019 Author Share Posted May 30, 2019 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 comment Share on other sites More sharing options...
KaFu Posted May 30, 2019 Share Posted May 30, 2019 I did not try it, but this one sounds up to the task. https://docs.microsoft.com/en-us/sysinternals/downloads/handle OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Earthshine Posted May 30, 2019 Share Posted May 30, 2019 (edited) Did you try the code i posted? You must not have. Ok. On your own now. I’ve wasted time here I see Edited May 30, 2019 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
KaFu Posted May 30, 2019 Share Posted May 30, 2019 (edited) 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 May 30, 2019 by KaFu Earthshine 1 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
Earthshine Posted May 30, 2019 Share Posted May 30, 2019 (edited) 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) Edited May 30, 2019 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Earthshine Posted May 30, 2019 Share Posted May 30, 2019 (edited) 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 expandcollapse popup; 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 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 May 30, 2019 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
RedAck Posted May 31, 2019 Author Share Posted May 31, 2019 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 comment Share on other sites More sharing options...
RedAck Posted May 31, 2019 Author Share Posted May 31, 2019 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 expandcollapse popup; 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 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. 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 comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now