ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

VB Scripts



  Fiesta ST
I've got a script that deletes all files and folders in a folder that are older than 14 days. However I'm sure it used to work but it's stopped working.

Code:

'**************************************
' Name: Remove Files Older than 14 Days From Common Folder
'
'**************************************

Set fso = CreateObject("Scripting.FileSystemObject")
olddate = DateAdd("d", -14, date)

'wscript.stdout.writeline "Today is " & Date
'wscript.stdout.writeline "Deleting files unaccessed since " & olddate
'wscript.stdout.writeline " "
'wscript.stdout.writeline "Connecting To FileShare "
Set folder = fso.GetFolder("E:\Company Folder\CommonPool") '("\\<Server>\d$\temp\archive")' Get the folder
'WScript.StdOut.Writeline "Getting a List of the Files"
Set fc = folder.Files
For Each f1 In fc
if f1.DateLastModified < olddate Then
'wscript.stdout.write "Removing: " & f1.DateLastModified & vbtab & f1.name & VbCrLf
fso.deletefile(f1)
End if
Next

Does this seem correct? or am I missing something.
 
Changing :

Set fc = folder.Files
For Each f1 In fc
if f1.DateLastModified < olddate Then
'wscript.stdout.write "Removing: " & f1.DateLastModified & vbtab & f1.name & VbCrLf
fso.deletefile(f1)
End if
Next

To :

For Each f1 In folder.Files
if f1.DateLastModified < olddate Then
'wscript.stdout.write "Removing: " & f1.DateLastModified & vbtab & f1.name & VbCrLf
fso.deletefile(f1)
End if
Next

Should do it (i think), i haven't done a vbs for a while and i've not got time to test it at the moment.
 
Sorry didn't read it properly :eek:

As above, to do folders you'd use

For Each f1 In folder.subFolders
if f1.DateLastModified < olddate Then
'wscript.stdout.write "Removing: " & f1.DateLastModified & vbtab & f1.name & VbCrLf
fso.deletefolder(f1)
End if
Next
 
  Fiesta ST
Cheers guys I've tried

'**************************************
' Name: Remove Files Older than 14 Days From Common Folder
'
'**************************************

Set fso = CreateObject("Scripting.FileSystemObject")
olddate = DateAdd("d", -14, date)

'wscript.stdout.writeline "Today is " & Date
'wscript.stdout.writeline "Deleting files unaccessed since " & olddate
'wscript.stdout.writeline " "
'wscript.stdout.writeline "Connecting To FileShare "
Set folder = fso.GetFolder("E:\Company Folder\CommonPool") '("\\<Server>\d$\temp\archive")' Get the folder
'WScript.StdOut.Writeline "Getting a List of the Files"
For Each f1 In folder.subFolders
if f1.DateLastModified < olddate Then
'wscript.stdout.write "Removing: " & f1.DateLastModified & vbtab & f1.name & VbCrLf
fso.deletefolder(f1)
End if
Next

doesn't seem to work :(
 
Cheers guys I've tried

'**************************************
' Name: Remove Files Older than 14 Days From Common Folder
'
'**************************************

Set fso = CreateObject("Scripting.FileSystemObject")
olddate = DateAdd("d", -14, date)

'wscript.stdout.writeline "Today is " & Date
'wscript.stdout.writeline "Deleting files unaccessed since " & olddate
'wscript.stdout.writeline " "
'wscript.stdout.writeline "Connecting To FileShare "
Set folder = fso.GetFolder("E:\Company Folder\CommonPool") '("\\<Server>\d$\temp\archive")' Get the folder
'WScript.StdOut.Writeline "Getting a List of the Files"
For Each f1 In folder.subFolders
if f1.DateLastModified < olddate Then
'wscript.stdout.write "Removing: " & f1.DateLastModified & vbtab & f1.name & VbCrLf
fso.deletefolder(f1)
End if
Next

doesn't seem to work :(

Try changing

fso.deletefolder(f1)

To :

fso.deletefolder f1
 


Top