Solved

Assistance on VBS Script for deletion of files

Posted on 2006-06-11
8
595 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

809 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