Solved

Assistance on VBS Script for deletion of files

Posted on 2006-06-11
8
594 Views
Last Modified: 2012-05-05
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
Comment
Question by:jhhaley
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:indu_mk
Comment Utility
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
 
LVL 1

Author Comment

by:jhhaley
Comment Utility
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
 
LVL 4

Expert Comment

by:indu_mk
Comment Utility
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
 
LVL 1

Author Comment

by:jhhaley
Comment Utility
Yes I included the change. I'll scan back through again to see if I missed anything.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:jhhaley
Comment Utility
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
 
LVL 4

Expert Comment

by:indu_mk
Comment Utility
the if condition is "if folders.path <> FolderName then ". u missed the .path.
pls try again.
0
 
LVL 4

Accepted Solution

by:
indu_mk earned 250 total points
Comment Utility
More accurate - "if Ucase(folders.path) <> Ucase(FolderName) then "
0
 
LVL 1

Author Comment

by:jhhaley
Comment Utility
Thanks I'll let you know
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now