Dbyte
Technical User
- Mar 6, 2002
- 87
I need to modify a function that finds the newest file in a folder & outputs the result to the screen. I want the output added to a .html document that was already opened earlier in the script. Here is my code from a test script:
This function will be used multiple times within a larger script so I'd like to be able to feed folders' paths into it & control/format the script's output (i.e. write to file) outside of the function itself. Right now this script creates an empty file but the correct output appears on screen.
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFSO.CreateTextFile("C:\NewestFile4" & ".txt", True)
sFilePath = "C:\NewestFile4.txt"
sFileName = oFSO.GetFileName(sPath)
sPath = "c:\"
Function GetNewestFile(ByVal sPath)
sNewestFile = Null
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sFilePath)
Set oFiles = oFolder.Files
For Each oFile In oFiles
If IsNull(sNewestFile) Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
Elseif dPrevDate < oFile.DateLastModified Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
End If
Next
If IsNull(sNewestFile) Then sNewestFile = ""
GetNewestFile = sNewestFile
End Function
If oNewestFile <> "" Then
oNewestFile = GetNewestFile(sNewestPath)
oFiletxt.WriteLine(sNewestFile & " last modified: " & sNewestFile.DateLastModified)
WScript.Echo "Newest file is " & sNewestFile
End If
oFiletxt.Close'
This function will be used multiple times within a larger script so I'd like to be able to feed folders' paths into it & control/format the script's output (i.e. write to file) outside of the function itself. Right now this script creates an empty file but the correct output appears on screen.