Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output function result to file instead of screen echo 1

Status
Not open for further replies.

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:
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.
 
Two tips:
1) comment out all On Error Resume Next instructions.
2) Use the Option Explicit instruction in the very first line of the whole script.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK PHV, here is my updated test script:
Code:
Option Explicit

Dim oFSO, oFiletxt, ONewestFile, sFilePath, sFileName, sPath

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'

Now neither the text document nor the screen echo are occuring. I am not getting any error messages when I run the script.
 
A starting point:
Code:
Option Explicit
Dim oFSO, oFiletxt, ONewestFile

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFSO.CreateTextFile("C:\NewestFile4.txt", True)

Set ONewestFile = oFSO.GetFile(GetNewestFile("c:\"))
oFiletxt.WriteLine oNewestFile.Path & " last modified: " & oNewestFile.DateLastModified
WScript.Echo "Newest file is " & oNewestFile.Path
oFiletxt.Close

Function GetNewestFile(ByVal sPath)
   Dim sNewestFile, dPrevDate, oFolder, oFile
   Set oFolder = oFSO.GetFolder(sPath)
   For Each oFile In oFolder.Files
      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

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Works great. I'll integrate your sample into my larger script ASAP. Thanks so much for the help PHV - a star for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top