简体   繁体   中英

UI Automation / Retrieve File Extension

I am tasked with a project that requires me to retrieve a specific file from a folder where I can only get an X and Y on the screen. While in XP I managed to use the fact that windows explorer is in essence a list view, and used the WM_HITTEST message to obtain information about the file, in Windows 7, this is not the case.

To solve this problem, I am using UI Automation, which is a great tool for such things, only problem is that in the case, the windows handle I am looking at belongs to the desktop, and the desktop might have several files with the same name but with different extensions (and windows is configured to "hide extensions of known file types") UI automation does not return the extension back to me. I have tried many things, but I cannot find any robust solution which would give me 100% success.

Has anyone tried this? successfully?

Could you provide more details regarding "a specific file from a folder"?
What rules would you use to identify a file manually?

I wouldn't say going through the GUI is the best way for such cases. If there is anything, that you can use for recognition of a file, stored in the file/folder system, I would try going through the back-end.

A simple example to illustrate. Counting total number of text files contained in a folder, and storing a path of the all Excel files found.

Dim sFolder
Dim FSO, objFolder, objFile, objXLSList
Dim intTXTCount

sFolder = "C:\TEMP"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objXLSList = CreateObject("Scripting.Dictionary")

Set objFolder = FSO.GetFolder(sFolder)
intTXTCount = 0
For Each objFile In objFolder.Files
 If Regex_Test(objFile.Name, ".*\.[t,T][t,T][t,T]") Then
  intTXTCount = intTXTCount + 1
 End If
 If Regex_Test(objFile.Name, ".*\.[x,X][l,L][s,S]") Then
  objXLSList.Add objXLSList.Count, objFile.Name
 End If
Next

Thank you,
Albert Gareev
http://automation-beyond.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM