Rizz
Rizz

Reputation: 25

Write names of files in a folder that have been created/modified today to text file

I have written a VBScript to write all the file names in a folder to a text file, but what I actually want is a listing of only the names of .txt files in a specific folder that have been modified or created today.

The code:

Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder("D:\test")
Set ObjOutFile = fso.CreateTextFile("D:\test\File_names.txt")
Set ObjFiles = ObjFolder.Files

For Each ObjFile In ObjFiles
    ObjOutFile.WriteLine(ObjFile.Name)
Next

ObjOutFile.Close

Upvotes: 1

Views: 1545

Answers (2)

Fred
Fred

Reputation: 5808

Its a long time since I have written any VBScript but this is along the right lines. You may need to adjust it a bit

Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile


Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder("c:\temp\")
Set ObjOutFile = fso.CreateTextFile("c:\temp\File_names.txt")
Set ObjFiles = ObjFolder.Files

For Each ObjFile In ObjFiles
   If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then
    If FormatDateTime(ObjFile.DateLastModified, 2) = FormatDateTime(Now(), 2) OR FormatDateTime(ObjFile.DateCreated , 2) = FormatDateTime(Now(), 2)   Then
            ObjOutFile.WriteLine(ObjFile.Name)
    Else
            ObjOutFile.WriteLine(ObjFile.Name & " - Modified:" & FormatDateTime(ObjFile.DateLastModified, 2) & " " & FormatDateTime(Now(),2) & " Created: " & FormatDateTime(ObjFile.DateCreated , 2) & " " & FormatDateTime(Now(), 2) )

    End if
   Else
     ObjOutFile.WriteLine(ObjFile.Name & " Not Text")
   End if
Next
ObjOutFile.Close

Upvotes: 1

JosefZ
JosefZ

Reputation: 30113

A code snippet with changes only:

For Each ObjFile In ObjFiles
    If StrComp( Right( ObjFile.name, 4), ".txt", vbTextCompare) = 0 and _
      ( Fix( ObjFile.DateCreated)      = Fix( Now) or _
        Fix( ObjFile.DateLastModified) = Fix( Now)       ) Then

            ObjOutFile.WriteLine(ObjFile.Name)
    End If 
Next

FIX() explanation:

date format is a floating point value, counting days since midnight 30 December 1899. Hours and minutes are represented as fractional days

Upvotes: 0

Related Questions