• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Empty recyclebin script need to log all the deletions rather than clearing the old data.

Hi,

Empty recyclebin script need to log all the deletions rather than clearing the old data.
At present it records and clears the old data when run twice.

Regards
Sharath
Const RECYCLE_BIN = &Ha&
Const FILE_SIZE = 3
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(RECYCLE_BIN)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshNetwork = WScript.CreateObject("WScript.Network") 
 
strLogPath = "\\ism\logs\Recycler\"
strComputerName = objWshNetwork.Computername            
 
' change the 2 to an 8 if you want to append to the log, not overwrite it
Set objLog = objFSO.OpenTextFile(strLogPath & strComputerName & ".log", 2, True)
 
Set colItems = objFolder.Items
 
objLog.WriteLine "Started: " & Now
 
For Each objItem in colItems
    if trim(objItem.Type) = "File Folder" then
		objLog.WriteLine "Folder: " & objItem.Name & " " & objItem.Size
		objFSO.DeleteFolder(objItem.Path)
	else
		objLog.WriteLine "File: " & objItem.Name & " " & objItem.Size
		objFSO.DeleteFile(objItem.Path)
	end if
Next
 
objLog.Close
Set objLog = Nothing

Open in new window

0
bsharath
Asked:
bsharath
  • 11
  • 9
1 Solution
 
Chris BottomleyCommented:
Hello bsharath,

Currently it logs teh file/folder then deletes it.  How do you want it to be changed?

Regards,

chris_bottomley
0
 
bsharathAuthor Commented:
i want the log to collect it should not clear
0
 
Chris BottomleyCommented:
To simply log without clearing is as follows:

Chris
Const RECYCLE_BIN = &Ha&
Const FILE_SIZE = 3
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(RECYCLE_BIN)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshNetwork = WScript.CreateObject("WScript.Network") 
 
'strLogPath = "\\ism\logs\Recycler\"
strLogPath = "c:\deleteme\"
strComputerName = objWshNetwork.Computername            
 
' change the 2 to an 8 if you want to append to the log, not overwrite it
Set objLog = objFSO.OpenTextFile(strLogPath & strComputerName & ".log", 2, True)
 
Set colItems = objFolder.Items
 
objLog.WriteLine "Started: " & Now
 
For Each objItem in colItems
    if trim(objItem.Type) = "File Folder" then
            objLog.WriteLine "Folder: " & objItem.Name & " " & objItem.Size
'            objFSO.DeleteFolder(objItem.Path)
      else
            objLog.WriteLine "File: " & objItem.Name & " " & objItem.Size
'            objFSO.DeleteFile(objItem.Path)
      end if
Next
 
objLog.Close
Set objLog = Nothing

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bsharathAuthor Commented:
It still clears the log file old data

I uncommented both the commented lines and tried

i want the recycle bin files data to stay as it is when run each time and append if any found
0
 
Chris BottomleyCommented:
The files in teh recycle bin are not deleted.

I failed to remove my folder change though so this change will maintain your log file and perhaps this is the error you are seeing.

cHRIS
Const RECYCLE_BIN = &Ha& 
Const FILE_SIZE = 3 
  
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.Namespace(RECYCLE_BIN) 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objWshNetwork = WScript.CreateObject("WScript.Network")  
  
strLogPath = "\\ism\logs\Recycler\" 
'strLogPath = "c:\deleteme\" 
strComputerName = objWshNetwork.Computername             
  
' change the 2 to an 8 if you want to append to the log, not overwrite it 
Set objLog = objFSO.OpenTextFile(strLogPath & strComputerName & ".log", 2, True) 
  
Set colItems = objFolder.Items 
  
objLog.WriteLine "Started: " & Now 
  
For Each objItem in colItems 
    if trim(objItem.Type) = "File Folder" then 
            objLog.WriteLine "Folder: " & objItem.Name & " " & objItem.Size 
'            objFSO.DeleteFolder(objItem.Path) 
      else 
            objLog.WriteLine "File: " & objItem.Name & " " & objItem.Size 
'            objFSO.DeleteFile(objItem.Path) 
      end if 
Next 
  
objLog.Close 
Set objLog = Nothing

Open in new window

0
 
bsharathAuthor Commented:
But now it does not delete any files in the recyclebin
I want them deleted each time its run and log the deleted data names. Say i have some names in the recyclebin when run 2nd time the names has to get logged below the first
0
 
Chris BottomleyCommented:
THat's not what the question asked for hence it's not what I was doing!

Noting that what is in the log file is the filename there can already be multiple entries relating to different paths.  You are now asking for a repeat entry of the file name ... one alternative is to increment a counter against each file name.

I haven't considered exactly how this can be implemented but assuming I can, can you indicate/clarify the requirement now?

Chris
0
 
bsharathAuthor Commented:
Sorry for confusing..But what i need is

When run it has to log the file names from the recyclebin into the log file and then delete them. if run say again it needs to check the recyclebin if any data available then log them below the first and then delete them. If no data in recycle bin then do nothing
0
 
Chris BottomleyCommented:
ALlowing for two files of the same name in different folders and several runs then the follwing is representative of the current format:

Started: 05/01/2010 10:44:58
File: File-1.txt 6 ... This is in folder 1 - first time around
File: File-1.txt 6 ... This is in folder 2 - first time around
File: File-1.txt 6 ... This is in folder 2 - second time around
File: File-2.txt 66
File: File-2.txt 66

Alternatively and potentially easier is:

Started: 05/01/2010 10:44:58
File: File-1.txt 6 <3>
File: File-2.txt 66 <2>

Chris
0
 
bsharathAuthor Commented:
If you are asking the format. i am ok with any format until all geta accumilated and nothing gets deleted from the log. thats the only change i need
0
 
Chris BottomleyCommented:
What about the size parameter ... do you want to use that as well - note a second delete of the same file may be a different size.

If you only want one size keeping then which latest/smallest/largest?

Chris
0
 
bsharathAuthor Commented:
Each file will have a size.So need the size adjacent to the name
Just making sure we both are in the same track
What i want is
Say i have 2 files on the desktop
File1.txt
File2.txt
Now i delete file1.txt and run script when run it has to look as
Started: 05/01/2010 5:24:04 PM
File: File1.txt 110592
When i delete the 2nd file and run it has to look as

File: File1.txt 110592
File: File2.txt 117760

The sizes are of its respective files
0
 
Chris BottomleyCommented:
Understood ok so far ... my question was if I recreate or copy back file1.txt and it starts as size 110592 when I delete it then there will be:

File: File1.txt 110592
File: File1.txt 110592
File: File2.txt 117760

or

File: File1.txt 110592 <1:2> ' suggesting was 1 now 2
File: File2.txt 117760 <1:1>

If I repeat the exercise but edit the file (at some time) pre delete we potentially have:

File1.txt 110592 and
File1.txt 110593

They are same file but would be represented differently if the file size is used for the unique identifier i.e:

File: File1.txt 110592
File: File1.txt 110592
File: File1.txt 110593
File: File2.txt 117760

or

File: File1.txt 110592 <2:3> ' suggesting was 2 now 3
File: File2.txt 117760 <1:1>

or

File: File1.txt 110593 <2:3> ' suggesting was 2 now 3
File: File2.txt 117760 <1:1>

WHat is the requirement in this case?

Chris
0
 
bsharathAuthor Commented:
Chris i will need the file names. i think i will go with the names only and no Sizes for this post....
0
 
Chris BottomleyCommented:
Have been busy trying to make it work ... so it is in the format as before with 1 line per file name and size.

Chris
Const FILE_SIZE = 3
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(RECYCLE_BIN)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshNetwork = WScript.CreateObject("WScript.Network") 
 
'strLogPath = "\\ism\logs\Recycler\"
strLogPath = "c:\deleteme\"
strComputerName = objWshNetwork.Computername            

strFileName = strLogPath & strComputerName & ".log"
On Error Resume next
Set logCollection = CreateObject("scripting.dictionary")
Set inFile = objFSO.opentextfile(strfilename, 1)
If Err.Number = 0 Then
	arr = split(infile.readall, vbcrlf)
	infile.close
	Set inFile = Nothing
	For Each itm In arr
		If Left(itm, 7) <> "Started" And itm <> "" Then
			str = lcase(itm)
			If not logCollection.Exists(str) Then
				logCollection.Add str, 1
			Else
				logCollection.Item(str) = logCollection.Item(str) + 1
			End If
		End If
	Next
End If
On Error GoTo 0
 
Set colItems = objFolder.Items
 
For Each objItem in colItems
    if trim(objItem.Type) = "File Folder" then
		str = lcase("Folder: " & objItem.Name & " " & objItem.Size)
            objFSO.DeleteFolder(objItem.Path)
      Else
      	str = LCase("File: " & objItem.Name & " " & objItem.Size)
            objFSO.DeleteFile(objItem.Path)
      end If
	If not logCollection.Exists(str) Then
		logCollection.Add str, 1
	Else
		logCollection.Item(str) = logCollection.Item(str) + 1
	End If
Next
' change the 2 to an 8 if you want to append to the log, not overwrite it
Set outFile = objFSO.opentextfile(strfilename, 2, true)
outfile.WriteLine "Started: " & Now
keys = logCollection.Keys
items = logCollection.Items
For Each objItem in logCollection
	for cnt = 1 To logcollection(objitem)
		outFile.WriteLine objitem
	Next
Next
outFile.Close
Set objLog = Nothing

Open in new window

0
 
bsharathAuthor Commented:
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Run.vbs
Line:      33
Char:      1
Error:      Object required: 'objFolder'
Code:      800A01A8
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
0
 
Chris BottomleyCommented:
Strange it works fine for me ... and it is your original code anyway.

Anything about the way the way you are running it?

Chris
0
 
Chris BottomleyCommented:
Mind you I forgot to change the output path again!

Chris
Const RECYCLE_BIN = &Ha&
Const FILE_SIZE = 3
 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(RECYCLE_BIN)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshNetwork = WScript.CreateObject("WScript.Network") 
 
strLogPath = "\\ism\logs\Recycler\"
'strLogPath = "c:\deleteme\"
strComputerName = objWshNetwork.Computername            

strFileName = strLogPath & strComputerName & ".log"
On Error Resume next
Set logCollection = CreateObject("scripting.dictionary")
Set inFile = objFSO.opentextfile(strfilename, 1)
If Err.Number = 0 Then
	arr = split(infile.readall, vbcrlf)
	infile.close
	Set inFile = Nothing
	For Each itm In arr
		If Left(itm, 7) <> "Started" And itm <> "" Then
			str = lcase(itm)
			If not logCollection.Exists(str) Then
				logCollection.Add str, 1
			Else
				logCollection.Item(str) = logCollection.Item(str) + 1
			End If
		End If
	Next
End If
On Error GoTo 0
 
Set colItems = objFolder.Items
 
For Each objItem in colItems
    if trim(objItem.Type) = "File Folder" then
		str = lcase("Folder: " & objItem.Name & " " & objItem.Size)
            objFSO.DeleteFolder(objItem.Path)
      Else
      	str = LCase("File: " & objItem.Name & " " & objItem.Size)
            objFSO.DeleteFile(objItem.Path)
      end If
	If not logCollection.Exists(str) Then
		logCollection.Add str, 1
	Else
		logCollection.Item(str) = logCollection.Item(str) + 1
	End If
Next
' change the 2 to an 8 if you want to append to the log, not overwrite it
Set outFile = objFSO.opentextfile(strfilename, 2, true)
outfile.WriteLine "Started: " & Now
keys = logCollection.Keys
items = logCollection.Items
For Each objItem in logCollection
	for cnt = 1 To logcollection(objitem)
		outFile.WriteLine objitem
	Next
Next
outFile.Close
Set objLog = Nothing

Open in new window

0
 
bsharathAuthor Commented:
Thanks a lot works perfect...
0
 
Chris BottomleyCommented:
Good oh!

Chris
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now