[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  VBscript
. . -> [Subject]  scripting.filesystemobject limitation problem
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

scripting.filesystemobject limitation problem

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Frikk   (2 posts)  [Biography] bio
Date Wed 09 Mar 2005 12:22 PM (UTC)
Message
The following is a purge file script i wrote. it takes three arguments, folder to be purged, log file, and the age of the files/folders to be deleted:


Dim t_date, p_date, date_diff, DateCheck, Output, stream, argv
Dim log_file, t_age, num_files, num_folders, temp_files
Const ReadOnly = 1
'on error resume next
sub doFolder(myFolder, date_age) 
	date_age = CInt(date_age)
	set objFiles = myFolder.files
	for each objfile in objFiles
	
		wscript.echo objFolder2.path & vbcrlf
		
		't_date = objfile.datelastmodified
		t_date = objfile.datecreated
		p_date = left(t_date, (InStr(1, t_date, " ") - 1))
		date_diff = CInt(datediff("d", p_date, Date))
		If (date_diff > date_age) then 
			Output = Output & "deleting " & objfile.path & " (" & (date_diff) & " days old)" & vbcrlf
			'wscript.echo "deleting " & objfile.path & " (" & (date_diff) & " days old)" & vbcrlf
			
			' Change the Files / Folders to Writeable
			If (objfile.Attributes AND ReadOnly) Then
				Output = Output & "*** " & objfile.name & " is readonly, changing to writeable" & vbcrlf
				'wscript.echo objfile.name & " is readonly, changing to writeable" & vbcrlf
				objFile.Attributes = objFile.Attributes XOR ReadOnly
			End If
			
			objfile.delete 
			
			num_files = num_files + 1
		Else
			Output = Output & "skipping " & objfile.path & " (" & (date_diff) & " days old)" & vbcrlf
		end If 
	next 
	
	' Now check all the sub directories recursively
	set objFolders = myFolder.subfolders 
	for each objFolder2 in objfolders 
	
		call doFolder (objFolder2, date_age)
		
		wscript.echo objFolder2.path & vbcrlf
		't_date = objFolder2.datelastmodified
		t_date = objFolder2.datecreated
		p_date = left(t_date, (InStr(1, t_date, " ") - 1))
		date_diff = CInt(datediff("d", p_date, Date))
		
		set objfiles2 = objfolder2.files
		temp_files = 0
		
		for each objfile2 in objfiles2
			temp_files = temp_files + 1
		next
		
		' Change the Files / Folders to Writeable
		If (objFolder2.Attributes AND ReadOnly) Then
			Output = Output & "*** " & objFolder2.name & " is readonly, changing to writeable" & vbcrlf
			'wscript.echo objfile.name & " is readonly, changing to writeable" & vbcrlf
			objFolder2.Attributes = objFolder2.Attributes XOR ReadOnly
		End If
		
		If (temp_files = 0 AND date_diff > date_age) Then
			'Delete Folder, too
			Output = Output & "deleting " & objFolder2.path & " (" & date_dff & " days old)" & vbcrlf
			num_folders = num_folders + 1
			objFolder2.delete
		Else
			'Output = Output & "skipping " & objfolder.path & " (" & (date_diff) & " days old)" & vbcrlf
		
		End If
		
	next 
end sub 

'Get Arguments
Set argv = Wscript.Arguments
If argv.count <> 3 Then
	'wscript.echo "################################################################"
	'wscript.echo "File Purger v.99!" & vbcrlf
	'wscript.echo "usage: " & wscript.scriptname & " <purge_path> <log_file> <delete_age>"
	'wscript.echo "example: " & wscript.scriptname & "c:\temp c:\purge_log.txt 7"
	'wscript.echo "the above example will remove all file older than 7 days in c:\temp, "
	'wscript.echo "and log them in c:\purge_log.txt"
	'wscript.echo "################################################################"
	wscript.quit
End If

purge_path = argv(0)
log_file =  argv(1)
t_age = argv(2)

num_files = 0
num_folders = 0

'Wscript.Echo "Starting Purger: " & date & " @ " & time
'Wscript.Echo "Removing objects older than " & t_age & " days"

set objFileSys=createobject("scripting.filesystemobject") 
set objFolder = objfilesys.GetFolder (purge_path)

call doFolder(objFolder, t_age)

' Write it all to disk, 8 = append
Set stream = objFileSys.OpenTextFile(log_file, 8, true)
stream.write "#########################################################" & vbcrlf & _
	"Starting Purger: " & date & " @ " & time & vbcrlf & _
	"Config: delete age: " & t_age & " days | " & _
	"log path: " & log_file & vbcrlf &  _
        "deleted: files: " & num_files & " folders: " & num_folders & vbcrlf & _
	"#########################################################" & vbcrlf
stream.write Output & vbcrlf & vbcrlf
stream.close

'Wscript.echo "deleted: files: " & num_files & " folders: " & num_folders
'Wscript.echo "Log Written!"


It works great. It is recursive, and only deletes files and folders with creation dates older than x days.

The problem is that once it reads 255 folders, it crashes out. The error is 'invalid use' or something like that, of

p_date = left(t_date, (InStr(1, t_date, " ")


t_date is the only variable used, but its coming straight from the t_date = objfile.datecreated. This makes me believe there is a variable that is being set too small (2^8) when the set objFileSys=createobject("scripting.filesystemobject") is created.

Does anyone know of a fix for this, or maybe why its happening?
[Go to top] top

Posted by Frikk   (2 posts)  [Biography] bio
Date Reply #1 on Wed 09 Mar 2005 12:24 PM (UTC)
Message
in case you didnt realize it, most of the console output is supressed. You can just remove the comments from all of the wscript.echo lines to see all of the console output.

It also writes to a log file when its done (obviously).

Thanks!
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Wed 09 Mar 2005 05:32 PM (UTC)
Message
What are the contents of the t_date variable right before the crash?

It's possible that the object has some limitation in that it can only index 255 subfolders per folders but I would find that a little surprising, personally.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


7,153 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]