All Forums >> [Scripting] >> WSH & Client Side VBScript >> [Resolved] File Delete vbscript with an error Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
I have a vbscript that deletes files based on their time. I'm currently getting an error message:
Line:7 Char:3 Error: File not found Code: 800A0035 runtime error.
I researched this error and found out that it is related to not finding the file name, not I'm stuck here, as I want the script to delete all files and not just one.
Here is the vbscript code:
[code] strFolder = "N:\Download_Data\Files\" Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(strFolder) Set objFSO = CreateObject("Scripting.FileSystemObject") For Each strFileName in objFolder.Items If len(objFSO.GetExtensionName(strFileName))> 0 Then Set objFile = objFSO.GetFile(strFolder & strFileName.Name) If DateDiff("N",objFile.DateLastModified,Now())> 59 Then objFSO.DeleteFile(strFolder & strFileName.Name),True End If End If Next
Thanks for your help!
< Message edited by netman06 -- 1/17/2008 4:42:47 PM >
(1) To me it looks like your code should work, as long as nobody else deletes/renames/locks files in "N:\Download_Data\Files\" while the script is running.
(2) But it is difficult to gain confidence in the code, because
(a) no "Option Explicit"
(b) mixing of "Shell.Application" and "Scripting.FileSystemObject"
(c) lying about data types and meanings: "strFileName" isn't a string and doesn't contain a file name
(d) reliance on default properties: are you sure, that a FolderItem2 returns a string containing the file's name or path?
(e) no "show me whats going on" code
(f) critical expressions are inlined, e.g.
DateDiff("N",objFile.DateLastModified,Now()) > 59
isn't so complicated, but when your are in trouble it pays to be able to dump values you rely on and keep the control statements simple (see my code in (3))
(g) doing things twice: you have a File object as the loop variable (masked by the bad "strFileName" variable, so why use GetFile to get another one?
(3) So I rewrote your code to:
(4) After some testing (stepping thru with the debugger, changing attributes, opening files with other applications, changing permissions) I came to the conclusion, that removing a file while the script is in the loop is the only way to get a "file not found" error.
(5) Removing the diagnostic code (perhaps to early, but I want to show a piece of 'short' code (cf. (b), (g)) and marking the place for error handling, I came up with
I hope this will help you to determine, why you got the error.