Jump to content

Recommended Posts

Posted

This vbs script is used to remove files from a network share after they haven’t been modified for so long. The script its self works fine I just need to change it to record what the script is deleting.

 

 

The original code

 

' removes files that are longer than the day are eg. "c:\script name" "delete location" "number of 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)

recurse folders

sub recurse( byref folders)

set subfolders = folders.subfolders

set files = folders.files

for each file in files

if file.datelastmodified < newdate then

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

 

 

I tried to change it to this it still removes the files but doesn’t write to the file but I can’t see what I’ve done wrong.

 

 

' removes files that are longer than the day are eg. "c:\script name" "delete location" "number of days" “c:\log file”

Set objArgs = WScript.Arguments

FolderName = objArgs(0)

Days = objArgs(1)

logpath = objArgs(2)

 

set fso = createobject("scripting.filesystemobject")

set OutputFile = fso.OpenTextFile(logpath)

set folders = fso.getfolder(FolderName)

datetoday = now()

newdate = dateadd("d", Days*-1, datetoday)

recurse folders

sub recurse( byref folders)

set subfolders = folders.subfolders

set files = folders.files

for each file in files

if file.datelastmodified < newdate then

on error resume next

OutputFile.Write file

OutputFile.close

file.delete

end if

 

next

 

for each folder in subfolders

recurse folder

next

 

set subfolders = nothing

set files = nothing

 

end sub

I would love it if someone could show me what I did wrong or even point me to somewhere that will help me see what I did wrong.

Posted
This vbs script is used to remove files from a network share after they haven’t been modified for so long. The script its self works fine I just need to change it to record what the script is deleting.

 

 

The original code

 

 

 

I tried to change it to this it still removes the files but doesn’t write to the file but I can’t see what I’ve done wrong.

 

 

 

I would love it if someone could show me what I did wrong or even point me to somewhere that will help me see what I did wrong.

 

not much wrong. I changed the arguments to variables to make diagnostics easier.

with these alterations it worked for me, running from d:\temp\new folder

 

stuff I've changed / added

 

FolderName = "d:\temp\new folder"

Days = 5

logpath = "d:\temp\new folder\log.txt"

 

Const ForReading = 1, ForWriting = 2, ForAppending = 8

set OutputFile = fso.OpenTextFile(logpath, forwriting, true)

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...