Solved

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

Posted on 2013-01-17
7
594 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
  • 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now