Solved

Assistance on VBS Script for deletion of files

Posted on 2006-06-11
8
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:indu_mk
ID: 16883778
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
ID: 16891264
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
ID: 16891590
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jhhaley
ID: 16896185
Yes I included the change. I'll scan back through again to see if I missed anything.
0
 
LVL 1

Author Comment

by:jhhaley
ID: 16898922
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
ID: 16899828
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
ID: 16899846
More accurate - "if Ucase(folders.path) <> Ucase(FolderName) then "
0
 
LVL 1

Author Comment

by:jhhaley
ID: 16909004
Thanks I'll let you know
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

628 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