Solved

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

Posted on 2013-01-17
7
598 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

785 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