Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


[Resolved] File Delete vbscript with an error

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,55255
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
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!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 [Resolved] File Delete vbscript with an error - 1/11/2008 2:58:52 AM   
  netman06

 

Posts: 107
Score: 0
Joined: 10/19/2006
Status: offline
Hello,

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 >
 
 
Post #: 1
 
 RE: File Delete vbscript with an error - 1/12/2008 10:58:35 PM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi netman06,

(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.

Good luck

ehvbs

(in reply to netman06)
 
 
Post #: 2
 
 RE: File Delete vbscript with an error - 1/17/2008 4:42:10 PM   
  netman06

 

Posts: 107
Score: 0
Joined: 10/19/2006
Status: offline
Hi, Ehvbs

Thanks for taking the time to explain and comment my code, as I now see why I was getting the error.

Thanks again for all of your help and time.


Take Care,

Mike

(in reply to ehvbs)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> [Resolved] File Delete vbscript with an error Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts