Solved

part complete vbs to set file attributes/delete files and output results, need help finshing

Posted on 2013-01-17
7
600 Views
Last Modified: 2013-01-23
Hi all,

Ive part completed this script but am currently getting

line 27 Char 1
error: wrong number of arguments of invalid property assignment
i also am trying to modify it to show the file names aswell as the total files then email the results...

Email works fine but it said it only did 1 file when i tested it
cannot modify attributes
havent tested filenames yet so not sure if the line breaks work for the results dim

THanks for any help

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select The Folder To Search :", (0))
If objFolder Is Nothing Then
Wscript.Quit
Else
	Set objFolderItem = objFolder.Self
	objPath = objFolderItem.Path
End If
 
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(objPath)
Dim filenames, results, fileCount
    fileCount = 0
fileCount = CountFiles(objFolder)

Set objMessage = CreateObject("CDO.Message") 

results = fileCount & " files have been deleted from the Home Folders Volume. The files where:- " & vbCrLf & Replace(filenames,",",vbCrLf))
objMessage.Subject = "Files Deleted" 
objMessage.From = "foldercleanup@test.co.uk" 
objMessage.To = "network.admin@test.co.uk" 
objMessage.TextBody = results
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="10.1.1.105"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
objMessage.Configuration.Fields.Update
'objMessage.Send

Function CountFiles(objFolder)
    On Error Resume Next
    Dim objSubFolder
    Dim objFile
    Dim fileCount
    
    fileCount = 0
    
    ' Count files in this folder
    For Each objFile In objFolder.Files
		If objFile.Attributes AND ReadOnly Then
		objFile.Attributes = objFile.Attributes XOR ReadOnly
		End If
        If objFile.DateLastModified < Date - 8 Then
			'objFile.Delete
            fileCount = fileCount + 1
			filenames = filenames & ", " & objFile.Name
        End If
    Next
    
    ' Count files in subfolders
    For Each objSubFolder In objFolder.SubFolders
        fileCount = fileCount + CountFiles(objSubFolder)
    Next
    CountFiles = fileCount
End Function

Open in new window

0
Comment
Question by:awilderbeast
[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
  • 3
  • 2
7 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 38788651
Line 18 remove the extra ending bracket )
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 38792221
Thanks, no errors now

i have two files in  a folder im running this against with attributes set to read only, this script isnt removing that checkbox, is something missing?

Thanks

Heres the code

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select The Folder To Search :", (0))
If objFolder Is Nothing Then
Wscript.Quit
Else
	Set objFolderItem = objFolder.Self
	objPath = objFolderItem.Path
End If
 
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(objPath)
Dim filenames, results, fileCount
    fileCount = 0
fileCount = CountFiles(objFolder)

Set objMessage = CreateObject("CDO.Message") 

results = fileCount & " files have been deleted from the Home Folders Volume. The files where:- " & vbCrLf & Replace(filenames,",",vbCrLf)
objMessage.Subject = "Files Deleted" 
objMessage.From = "foldercleanup@apdcomms.co.uk" 
objMessage.To = "network.admin@apdcomms.co.uk" 
objMessage.TextBody = results
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="10.1.1.105"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
objMessage.Configuration.Fields.Update
'objMessage.Send

Function CountFiles(objFolder)
    On Error Resume Next
    Dim objSubFolder
    Dim objFile
    Dim fileCount
    
    fileCount = 0
    
    ' Count files in this folder
    For Each objFile In objFolder.Files
		If objFile.Attributes AND ReadOnly Then
		objFile.Attributes = objFile.Attributes XOR ReadOnly
		End If
        If objFile.DateLastModified < Date - 8 Then
			'objFile.Delete
            fileCount = fileCount + 1
			filenames = filenames & ", " & objFile.Name
        End If
    Next
    
    ' Count files in subfolders
    For Each objSubFolder In objFolder.SubFolders
        fileCount = fileCount + CountFiles(objSubFolder)
    Next
    CountFiles = fileCount
End Function

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38804106
Hi, all you are missing is this:
Const ReadOnly = 1

add that above
Dim objSubFolder

and it will work.

Regards,

Rob.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:awilderbeast
ID: 38804980
Thanks!

im alsmost there now, just the filenames part isnt working

the email comes through with just
"55 files have been deleted from the Home Folders Volume. The files where:- "

i dont think its adding the filenames to that string, what am i missing?

Thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38807868
I see the correct files when I run this:

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select The Folder To Search :", (0))
If objFolder Is Nothing Then
	WScript.Quit
Else
	Set objFolderItem = objFolder.Self
	objPath = objFolderItem.Path
End If
 
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(objPath)
Dim filenames, results, fileCount
    fileCount = 0
fileCount = CountFiles(objFolder)

Set objMessage = CreateObject("CDO.Message") 

results = fileCount & " files have been deleted from the Home Folders Volume. The files where:- " & vbCrLf & Replace(filenames,", ",vbCrLf)
WScript.Echo results
objMessage.Subject = "Files Deleted" 
objMessage.From = "foldercleanup@apdcomms.co.uk" 
objMessage.To = "network.admin@apdcomms.co.uk" 
objMessage.TextBody = results
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="10.1.1.105"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
objMessage.Configuration.Fields.Update
'objMessage.Send

Function CountFiles(objFolder)
    On Error Resume Next
    Const ReadOnly = 1
    Dim objSubFolder
    Dim objFile
    Dim fileCount
    
    fileCount = 0
    
    ' Count files in this folder
    For Each objFile In objFolder.Files
		If objFile.Attributes AND ReadOnly Then
		objFile.Attributes = objFile.Attributes XOR ReadOnly
		End If
        If objFile.DateLastModified < Date - 8 Then
			'objFile.Delete
            fileCount = fileCount + 1
			filenames = filenames & ", " & objFile.Name
        End If
    Next
    
    ' Count files in subfolders
    For Each objSubFolder In objFolder.SubFolders
        fileCount = fileCount + CountFiles(objSubFolder)
    Next
    CountFiles = fileCount
End Function

Open in new window


I have added a
WScript.Echo results

line that should show you what the body of the message is going to be.

It appears to work fine.

Regards,

Rob.
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 38808986
Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

756 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