Go Premium for a chance to win a PS4. Enter to Win

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
?
612 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
6 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

877 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