• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

Assistance on VBS Script for deletion of files

I've downloaded this script and I've made some modifications. It currently deletes all files older than 5 days old from the root of C:\ProdFiles and all subfolders. I need to make the following changes, exempt any files residing in the root of the parent folder, but delete any file from subfolders over 5 days old. I've purchased a couple of books on scripting, but any assistance and recommendations for improvement would be appreciated. I would also like to put the results of the scan in the root of C:\ProdFiles.

'Objective: This script is executed by the scheduled deloldfiles.bat. The script scans all sub-folders in C:\ProdFiles
'and deletes all files over 5 days old. Files residing in the root of ProdFiles are exempted from the scan.  
' Created by: XXX
' Created Date: June 07, 2006
' Usage: cscript deloldfiles.vbs c:\ProdFiles 5
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
  set subfolders = folders.subfolders
  set files = folders.files
  wscript.echo ""
  wscript.echo "Deleting Files under the Folder:" & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
    file.delete
    end if
   
  next  

  for each folder in subfolders
    recurse folder
  next  

  set subfolders = nothing
  set files = nothing

end sub
0
jhhaley
Asked:
jhhaley
  • 4
  • 4
1 Solution
 
indu_mkCommented:
Modify "recurse" like this
sub recurse( byref folders)
set subfolders = folders.subfolders
set files = folders.files
if folders.path <> FolderName then
  wscript.echo ""
  wscript.echo "Deleting Files under the Folder:" & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
    file.delete
    end if
   
  next  
end if
  for each folder in subfolders
    recurse folder
  next  

  set subfolders = nothing
  set files = nothing

end sub
0
 
jhhaleyAuthor Commented:
Thanks for the prompt response. I made the change and executed the script by doing "csript deloldfiles.vbs c:\ProdFile 5". It still deletes the files in the root of the parent folder.  
0
 
indu_mkCommented:
This is what I got. It works for me...
Are u sure that u included the if condition(if folders.path <> FolderName then )?
---------------------------------------------------------------------------------------------------
cscript ind.vbs "D:\Test 1" 5

Today:6/13/2006 9:48:54 AM
Started deleting files older than :6/8/2006 9:48:54 AM
________________________________________________


Deleting Files under the Folder:D:\Test 1\test 2
__________________________________________________________________________
Deleting D:\Test 1\test 2\glossary.pdf last modified: 5/26/2006 10:58:54 AM

Completed deleting files older than :6/8/2006 9:48:54 AM
________________________________________________
---------------------------------------------------------------------------------------------------
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jhhaleyAuthor Commented:
Yes I included the change. I'll scan back through again to see if I missed anything.
0
 
jhhaleyAuthor Commented:
Here is the script and the output. Maybe I'm missing something. Not the output is deleting the files from the root of C:\test

' Objective: To delete old production files from the filemover older than 5 days. It is executed in the scheduler by the deloldfiles.bat
' Created by:xxx
' Created Date: June 07, 2006
' Usage: cscript del.vbs c:\test 5
'      : It deletes files older than 5 days
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
set subfolders = folders.subfolders
set files = folders.files
if folders <> FolderName then
  wscript.echo ""
  wscript.echo "Deleting Files under the Folder:" & folders.path
  wscript.echo "__________________________________________________________________________"
  for each file in files
    if file.datelastmodified < newdate then
      wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
      on error resume next
    file.delete
    end if
   
  next  
end if
  for each folder in subfolders
    recurse folder
  next  

  set subfolders = nothing
  set files = nothing

end sub

OUTPUT
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Today:6/13/2006 7:19:20 PM
Started deleting files older than :6/8/2006 7:19:20 PM
________________________________________________


Deleting Files under the Folder:C:\test
__________________________________________________________________________
Deleting C:\test\192.168.2.1-PortMap.xls last modified: 4/29/2006 10:59:49 PM

Deleting Files under the Folder:C:\test\Syslog_files
__________________________________________________________________________
Deleting C:\test\Syslog_files\192.168.2.1-PortMap.xls last modified: 4/29/2006 10:59:49 PM

Deleting Files under the Folder:C:\test\Syslog_files\Syslog_files
__________________________________________________________________________

Deleting Files under the Folder:C:\test\Syslog_files\Syslog_files\Syslog_files
__________________________________________________________________________

Deleting Files under the Folder:C:\test\tes4
__________________________________________________________________________
Deleting C:\test\tes4\192.168.2.1-PortMap.xls last modified: 4/29/2006 10:59:49 PM

Deleting Files under the Folder:C:\test\tes4\Syslog_files
__________________________________________________________________________

Deleting Files under the Folder:C:\test\test1
__________________________________________________________________________
Deleting C:\test\test1\192.168.2.1-PortMap.xls last modified: 4/29/2006 10:59:49 PM

Deleting Files under the Folder:C:\test\test1\Syslog_files
__________________________________________________________________________

Deleting Files under the Folder:C:\test\test2
__________________________________________________________________________
Deleting C:\test\test2\192.168.2.1-PortMap.xls last modified: 4/29/2006 10:59:49 PM

Deleting Files under the Folder:C:\test\test2\Syslog_files
__________________________________________________________________________

Completed deleting files older than :6/8/2006 7:19:20 PM
0
 
indu_mkCommented:
the if condition is "if folders.path <> FolderName then ". u missed the .path.
pls try again.
0
 
indu_mkCommented:
More accurate - "if Ucase(folders.path) <> Ucase(FolderName) then "
0
 
jhhaleyAuthor Commented:
Thanks I'll let you know
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now