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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with script searching for file...

Status
Not open for further replies.

gavinr98

IS-IT--Management
May 6, 2004
18
0
0
US
Need some help on this script, it works fine if I put in the name of the file and the extension, however I need to script to search for any new files regardless of the file name and file extension... can anyone help with this? I keep getting "No file Exists" every time I run it.

Const startDir = "C:\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")
DateToday = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
sFileNameStart = "*."
sFileNameEnd = ".*"
Set objEmail = CreateObject("CDO.Message")
Set oFolder = oFSO.GetFolder(startDir)
sFullFileName = ""
Recurse oFolder
If sFullFileName <> "" Then
sendEmail
Else
WScript.Echo "No File Exists."
End If

Sub Recurse(oFldr)
For Each oSubFolder In oFldr.SubFolders
Recurse oSubFolder
Next
For Each oFile In oFldr.Files
If Left(LCase(oFile.Name), Len(sFileNameStart)) = LCase(sFileNameStart) And _
Right(LCase(oFile.Name), Len(sFileNameEnd)) = LCase(sFileNameEnd) Then
sFullFileName = oFile.Name
Exit Sub
End If
Next
End Sub

Any help on this would be appreciated...
Thanks...
 
Your code is looking for a file beginning and ending with an astrix.

Code:
If Left(LCase(oFile.Name), Len(sFileNameStart)) = LCase(sFileNameStart) And _
 Right(LCase(oFile.Name), Len(sFileNameEnd)) = LCase(sFileNameEnd) Then

These are textual comparisons. The astrix is interpretted as an astrix, not a wild card. Worry not about the filename and extension; such comparisons will not help in acheiving your goal. Instead, compare dates

find files younger than 7 days
Code:
if (datediff("d", oFile.dateLastModified, now) < 7) then
   sFullFileName = sFullFileName & oFile.Name & vbNewLine
end if

Additionally, your recursive function will produce no results unless or return sFullFileName (only functions, not subs, can return values).

Code:
function recurse(oFolder)
   for each oSubFolder in oFolder.SubFolder
       sFiles = recurse(oFolder)
   next

   for each oFile in oFolder.Files
       if (datediff("d", oFile.dateLastModified, now) < 7) then
          sFiles = sFiles & oFile.Name & vbNewLine
       end if
   next
   recurse = sFiles
end function

arrFiles = split(sFiles, vbNewLine)
msgbox ubound(arrFiles) + 1 & " File(s) found: " & vbNewLine & sFiles

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks for the reply, so I have made the changes to the script and I get an error, object Required on line (4,1) ... This is how the script looks; any ideas?

Dim oFile
Const startDir = "C:\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")
If (datediff("d", oFile.dateLastModified, now) < 7) then
sFullFileName = sFullFileName & oFile.Name & vbNewLine
end If
Set objEmail = CreateObject("CDO.Message")
Set oFolder = oFSO.GetFolder(startDir)
sFullFileName = ""
recurse oFolder
If sFullFileName <> "" Then
sendEmail
Else
WScript.Echo "No File Exists."
End If

function recurse(oFolder)
for each oSubFolder in oFolder.SubFolder
sFiles = recurse(oFolder)
Next
For each oFile in oFolder.Files
if (datediff("d", oFile.dateLastModified, now) < 7) then
sFiles = sFiles & oFile.Name & vbNewLine
end if
next
recurse = sFiles
end Function
arrFiles = split(sFiles, vbNewLine)
msgbox ubound(arrFiles) + 1 & " File(s) found: " & vbNewLine & sFiles

 
line 4:
If (datediff("d", oFile.dateLastModified, now) < 7) then

oFile has not been defined yet.

- Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top