保存一个前面带有一个字母的新文件
请查找代码:
问题是该文件夹有一个很大的编号。文件数
===================================================================================== Dim fso, objFolder, obFileList, folderpath,counter folderpath = "G:\Everyone\Model Office Testing Documents\HP QC\QTP\PSISAutomation\Logs" Set fso = CreateObject("Scripting.FileSystemObject") Set objFolder = fso.GetFolder(folderpath) Set objFileList = objFolder.Files For Each File In objFileList msgbox("5") If InStr(1,File.Name,"DE_For_Pol_Print_APPA_7A_Copy_") = 1 Then counter=counter+1 End If Next counter=counter+1 msgbox("new file will be saved as: " &"DE_For_Pol_Print_APPA_7A_Copy_"& Chr(64 + Counter))
Please find the code:
Problem is the folder has a large no. of files
===================================================================================== Dim fso, objFolder, obFileList, folderpath,counter folderpath = "G:\Everyone\Model Office Testing Documents\HP QC\QTP\PSISAutomation\Logs" Set fso = CreateObject("Scripting.FileSystemObject") Set objFolder = fso.GetFolder(folderpath) Set objFileList = objFolder.Files For Each File In objFileList msgbox("5") If InStr(1,File.Name,"DE_For_Pol_Print_APPA_7A_Copy_") = 1 Then counter=counter+1 End If Next counter=counter+1 msgbox("new file will be saved as: " &"DE_For_Pol_Print_APPA_7A_Copy_"& Chr(64 + Counter))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用 FSO,而应使用 WMI,将文件名放入 SELECT 语句中,例如:
"DE_For_Pol_Print_APPA_7A_Copy_%"
。这应该返回一个仅包含具有所请求文件名的文件的集合(比总集合更快)。文件集合没有 count 属性,但您可以使用:
这不会访问内部文件对象,并且应该运行得相当快。
第二种甚至更快(但在我看来更丑陋)的技术是使用 windowshell 对象中的命令提示符并将目录返回到输出。输出只是一个字符串。现在,计算所需字符串 (DE_For_Pol_Print_APPA_7A_Copy_) 上的匹配数量,这就是您的计数器。
确切的代码留空作为海报的练习。
Do not use the FSO, but make use of the WMI where you put the filename in the SELECT statement, like:
"DE_For_Pol_Print_APPA_7A_Copy_%"
. This should return a collection with only files with the requested filename (faster than a total collection).There is no count property for file collections, but you can use:
This will not access the internal file object and should run reasonably fast.
A second and even faster (but uglier imo) technique is to use the command prompt from a windowshell object and return the dir to the output. The output is just a string. Now, count the amount of matches on your desired string (DE_For_Pol_Print_APPA_7A_Copy_) and that is your counter.
The exact code is left blank as an excercise for the poster.