Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assistance on VBS Script for deletion of files

Posted on 2006-06-11
8
Medium Priority
?
600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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 …
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…

670 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