Excelmacros只读取今天创build的文件的input

我有一个应用程序,以txt格式导出每日报告。 我有一个macros从这些报告中提取某些数据行,并把它们放在一个输出xls文件中。 我的macros的input目录是一个单独的文件夹,我手动移动今天的报告。

我希望我的macros能够从默认的报告文件夹中读取,并只读取使用今天的date创build的文件。

报告文件的命名约定如下:1101_16_16_AppServiceUser_YYYYMMDDhhmmssXXX.txt不确定文件名上的最后3位数字代表什么,但它们始终是数字。

帮帮我?

哇,这是快! 谢谢…第一次使用stackoverflow。 我想我应该包括拉动数据并将其转储到Excel的代码…这里是:

Sub PullLinesFromEPremisReport() Dim FileName, PathN, InputLn As String Dim SearchFor1, SearchFor2, OutpFile As String Dim StringLen1, StringLen2 As Integer Dim colFiles As New Collection Dim bridgekey As String PathO = "C:\Documents and Settings\GROMERO\Desktop\CM reconciliation\output\" PathN = "C:\Documents and Settings\GROMERO\Desktop\CM reconciliation\input\" FileName = Dir(PathN) While FileName <> "" colFiles.Add (FileName) FileName = Dir Wend SearchFor1 = "BRIDGE KEY" StringLen1 = Len(SearchFor1) OutpFile = "RESULTS.xls" Open PathO & OutpFile For Output As #2 For Each Item In colFiles Open PathN & Item For Input As #1 Do Until EOF(1) = True Line Input #1, InputLn If (Left(LTrim$(InputLn), StringLen1) = SearchFor1) Then bridgekey = InputLn End If Loop Close #1 Next Item Close #2 End Sub 

Daniel的答案是正确的,但使用FileSystemObject需要几个步骤:

确保你有一个“Microsoft脚本运行时”的参考: 在这里输入图像说明在这里输入图像描述

然后,遍历目录中的文件:

 Sub WorkOnTodaysReports() 'the vars you'll need Dim fso As New FileSystemObject Dim fldr As Folder Dim fls As Files Dim fl As File Set fldr = fso.GetFolder("C:\Reports") Set fls = fldr.Files For Each fl In fls 'InStr returns the position of the substring, or 0 if not found ' EDIT: you can explicitly use the reliable parts of your file name ' to avoid false positives If InStr(1, fl.Name, "AppServiceUser_" & Format(Now, "YYYYMMDD")) > 0 Then 'Do your processing End If Next fl End Sub 

编辑:所以我认为 ,从你张贴的代码,你可以发送PathN到主要的报告文件夹像你想要的,然后只是修改你的While语句像这样:

 While FileName <> "" If InStr(1, FileName, "AppServiceUser_" & Format(Now, "YYYYMMDD")) > 0 Then colFiles.Add (FileName) End If FileName = Dir Wend 

两种方法可以做到这一点从我的头顶。 假设您正在通过FileSystemObject使用File

在文件名上做一个Instr ,在file.Name寻找Format(Date, "YYYYMMDD")

或者使用一个更简单的方法循环遍历文件,并在你的循环中执行此操作:

 If File.DateCreate >= Date Then 'Do something end if 

其中File是用于循环遍历File的实际variables。

 If fileName like "*AppServiceUser_" & Format(Now, "YYYYMMDD") & _ "#########.txt" Then 'good to go End If