简体   繁体   English

UI自动化/检索文件扩展名

[英]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. 我的项目任务是要求我从只能在屏幕上显示X和Y的文件夹中检索特定文件。 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. 在XP中,我设法利用了Windows资源管理器实质上是列表视图的事实,并使用WM_HITTEST消息获取有关文件的信息,但在Windows 7中却并非如此。

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. 为了解决此问题,我使用了UI Automation,它是处理此类问题的好工具,唯一的问题是,在这种情况下,我正在查看的Windows句柄属于桌面,并且桌面可能包含多个相同的文件名称,但具有不同的扩展名(并且Windows配置为“隐藏已知文件类型的扩展名”),UI自动化不会将扩展名返回给我。 I have tried many things, but I cannot find any robust solution which would give me 100% success. 我已经尝试了很多事情,但我找不到任何强有力的解决方案,这将使我100%的成功。

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. 我不会说通过GUI是此类情况的最佳方法。 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. 计算文件夹中包含的文本文件的总数,并存储找到的所有Excel文件的路径。

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/ http://automation-beyond.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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