Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-17
7
Medium Priority
?
608 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Make the most of your online learning experience.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

719 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