Solved

Part complete vbs for each file in folder and files in subfolders...

Posted on 2013-01-15
12
594 Views
Last Modified: 2013-01-17
Hi all,

just need some help with the below, at the mo i have to for eaches but im guessing there is an easier way.

i want to get all files in the folder selected and all files in all subfolders...

Thanks for any help

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select The Folder To Enumerate :", (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)
For each objFolder.SubFolders in objFolder

Next

For each objFile in objFolder.Files
	if objFile.DateLastModified < Date()-8 Then
	msgBox "Found file: " & objFile.Name
	End If
Next

Open in new window

0
Comment
Question by:awilderbeast
  • 8
  • 4
12 Comments
 
LVL 5

Expert Comment

by:LazyFolk
Comment Utility
Dir() function may help you (depend what you try to acheive)

http://bytes.com/topic/visual-basic/answers/637239-using-dir-list-folder-names

MSDN has an example on how to detect directory if you want to do this recursively
http://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.71).aspx
0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
i got it sussed! thanks

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)

GetFiles(objFolder)

Function GetFiles(FolderName)
    On Error Resume Next
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile
 
    Set ObjFiles = objFolder.Files
     
    For Each ObjFile In ObjFiles
		if objFile.DateLastModified < Date()-8 Then
		msgBox "Found file: " & objFile.Name
		End If
    Next
     
    Set ObjSubFolders = ObjFolder.SubFolders     
    For Each ObjFolder In ObjSubFolders         
        GetFiles(ObjFolder.Path)
    Next
End Function

Open in new window

0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for awilderbeast's comment #a38778875

for the following reason:

Found answer
0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
one addition: i need it to msgbox only once but its msgboxing 3 times (as i have 3 subfolders in what im testing)

what can i do for it to only msgbox 1 count?

thanks

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 CountFiles
CountFiles = 0

GetFiles(objFolder)

Function GetFiles(FolderName)
    On Error Resume Next
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile
 
    Set ObjFiles = objFolder.Files
    
    For Each ObjFile In ObjFiles
		if objFile.DateLastModified < Date()-8 Then
		'msgBox "Found file: " & objFile.Name
		CountFiles = CountFiles + 1
		End If
    Next
     
    Set ObjSubFolders = ObjFolder.SubFolders     
    For Each ObjFolder In ObjSubFolders         
        GetFiles(ObjFolder.Path)
    Next
	msgBox CountFiles
	
End Function

Open in new window

0
 
LVL 5

Expert Comment

by:LazyFolk
Comment Utility
add a boolean in the parameters of GetFile function

GetFiles (path,boolean)

Depending on boolean value, you display message box or not
0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
i tried the below, i get.. cannot use a parentheses when calling a sub line 15 char 25

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 CountFiles
CountFiles = 0

GetFiles(objFolder,true)

Function GetFiles(FolderName,displaybox)
    On Error Resume Next
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile
 
    Set ObjFiles = objFolder.Files
    
    For Each ObjFile In ObjFiles
		if displaybox = true Then
			if objFile.DateLastModified < Date()-8 Then
			'msgBox "Found file: " & objFile.Name
			CountFiles = CountFiles + 1
			End If
		End If
    Next
     
    Set ObjSubFolders = ObjFolder.SubFolders     
    For Each ObjFolder In ObjSubFolders         
        GetFiles(ObjFolder.Path,false)
    Next
	msgBox CountFiles
	
End Function

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Accepted Solution

by:
LazyFolk earned 500 total points
Comment Utility
Can you explain what you are trying to achieve ?

From what I see you try to count the number of file in a given folder (including sub folders)

Is that correct ?


The following code achieve this :

Sub Button1_Click()
    Dim fileCount
    fileCount = 0
    
    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)

    fileCount = CountFiles(objFolder)
    MsgBox (fileCount)
End Sub

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.DateLastModified < Date - 8 Then
            'msgBox "Found file: " & objFile.Name
            fileCount = fileCount + 1
        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


The main program is included in the click method of Button1 in the attached excel sheet


The function CountFile return the number of file in a given folder including subfolders.
This fiunction call itself to get the number of files in subfolders.

The total is displayed in a message box at the end of the main program
fileCount.xlsm
0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
Well I actually need it to delete any file older than 8 days then tell me how many it did, i think youve done it as above, but in place of the

'msgBox "Found file: " & objFile.Name

do i just do a

objFile.Delete

?

Thanks
0
 
LVL 5

Expert Comment

by:LazyFolk
Comment Utility
yes this should work
0
 
LVL 1

Author Closing Comment

by:awilderbeast
Comment Utility
Thanks!
0
 
LVL 1

Author Comment

by:awilderbeast
Comment Utility
Hey there, just need a little extra adding to this, which when ive tried i get

line 27 Char 1
error: wrong number of arguments of invalid property assignment

im trying to check for any read only files and change them to read write, then delete them if they are older than 8 days

Thanks

Sub Button1_Click()
    Dim fileCount
    fileCount = 0
    
    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)

    fileCount = CountFiles(objFolder)
    MsgBox (fileCount)
End Sub

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
        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 1

Author Comment

by:awilderbeast
Comment Utility
actually i wouldnt mind making a few addiotns, heres the new question for any help you can provide

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_27998757.html

Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

771 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

15 Experts available now in Live!

Get 1:1 Help Now