Solved

continue execution even if the line fails

Posted on 2014-11-26
4
132 Views
Last Modified: 2014-11-26
Hi;

I have a vbs script in which I need to go through the files and folders in a drive.

The following script fails regarding permission settings are not enough on a folder, failing.jpg

failing
What I need is that I want the application continue to run:

'==========================================================================
' NAME: folderSize.vbs
' COMMENT: Scans folder sizes of a selected folder.
'==========================================================================
Dim objFSO, objFolder, objFile, objFile2, F, myVar1, myVar2, folderCount, strWrite

folderCount = 0
objFile2 = "folderdetails.csv"

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Call browseFolder(strFolderSrc,"Source")
Set objFolder = objFSO.GetFolder(strFolderSrc)

'Write Header Row
Set objFile = CreateObject("Scripting.FileSystemObject")   
Set strWrite = objFile.OpenTextFile(objFile2, 2, True)
strWrite.WriteLine("Folder Name,Size (MB),# Files,# Sub Folders")
Wscript.Sleep 300

ShowFolderDetails objFolder

strWrite.Close

MsgBox "Complete."

Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objFile2 = Nothing
Set F = Nothing
Set myVar1 = Nothing
Set myVar2 = Nothing
Set strWrite = Nothing

WScript.Quit
  
'==========================================================================
'Functions
Function ShowFolderDetails(oF)
    strWrite.WriteLine(oF.Name + "," + CStr(oF.Size /1024\1024) + "," + CStr(oF.Files.Count) + "," + CStr(oF.Subfolders.Count))
	
    'Comment out the following line and the loop to end the statement
    'to list all subfolders.(End Loop is 6 lines down)
    Do While folderCount < 1
    	for each F in oF.Subfolders
        	ShowFolderDetails F
    	Next
    	folderCount = folderCount + 1
    Loop
End Function

' browseFolder brings up the selection box to choose both the source and the destination.
Function browseFolder(myVar1,myVar2)
	Const WINDOW_HANDLE = 0
	Const NO_OPTIONS = 0
	Set objShell = CreateObject("Shell.Application")
	Set objFolder = objShell.BrowseForFolder _
    	(WINDOW_HANDLE, "Select a " & myVar2 & " folder:", NO_OPTIONS, "C:\Scripts")
	On Error Resume Next
	Set objFolderItem = objFolder.Self
	objPath = objFolderItem.Path
	myVar1 = objPath
	Call objPathChk(myVar1)
End Function

' objPathChk checks to make sure that a source has been selected.
Function objPathChk(myVar1)
	If myVar1 = "" Then
		MsgBox "Scan Folder Not Specified." & VbCrLf & _
				"Scan will now quit.", vbOKOnly, "Terminate"
		WScript.Quit
	End If		
End Function
'==========================================================================

Open in new window

0
Comment
Question by:jazzIIIlove
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 400 total points
ID: 40466735
Hi,

Insert       On Error resume next
before the problem code line

and insert  On Error goto 0
just after the the problem line

Regards
0
 
LVL 25

Assisted Solution

by:Shaun Kline
Shaun Kline earned 100 total points
ID: 40466750
Adding to Rgonzo1971's comment (no points please), after the faulty line, you should consider checking for the error using
If Err.Number <> 0 Then
' Your code
End If
On Error Goto 0

This provides a mechanism to see what files/folders/action caused the error and log it.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 40466757
Hi I did as you said as follows. The execution finishes but nothing is coming forward. The program is for filesize. Can you check?
'==========================================================================
' NAME: folderSize.vbs
' COMMENT: Scans folder sizes of a selected folder.
'==========================================================================
Dim objFSO, objFolder, objFile, objFile2, F, myVar1, myVar2, folderCount, strWrite

folderCount = 0
objFile2 = "folderdetails.csv"

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Call browseFolder(strFolderSrc,"Source")
Set objFolder = objFSO.GetFolder(strFolderSrc)

'Write Header Row
Set objFile = CreateObject("Scripting.FileSystemObject")   
Set strWrite = objFile.OpenTextFile(objFile2, 2, True)
strWrite.WriteLine("Folder Name,Size (MB),# Files,# Sub Folders")
Wscript.Sleep 300

ShowFolderDetails objFolder

strWrite.Close

MsgBox "Complete."

Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objFile2 = Nothing
Set F = Nothing
Set myVar1 = Nothing
Set myVar2 = Nothing
Set strWrite = Nothing

WScript.Quit
  
'==========================================================================
'Functions
Function ShowFolderDetails(oF)
On Error resume next
    strWrite.WriteLine(oF.Name + "," + CStr(oF.Size /1024\1024) + "," + CStr(oF.Files.Count) + "," + CStr(oF.Subfolders.Count))
On Error goto 0	
    'Comment out the following line and the loop to end the statement
    'to list all subfolders.(End Loop is 6 lines down)
    Do While folderCount < 1
    	for each F in oF.Subfolders
        	ShowFolderDetails F
    	Next
    	folderCount = folderCount + 1
    Loop
End Function

' browseFolder brings up the selection box to choose both the source and the destination.
Function browseFolder(myVar1,myVar2)
	Const WINDOW_HANDLE = 0
	Const NO_OPTIONS = 0
	Set objShell = CreateObject("Shell.Application")
	Set objFolder = objShell.BrowseForFolder _
    	(WINDOW_HANDLE, "Select a " & myVar2 & " folder:", NO_OPTIONS, "C:\Scripts")
	On Error Resume Next
	Set objFolderItem = objFolder.Self
	objPath = objFolderItem.Path
	myVar1 = objPath
	Call objPathChk(myVar1)
End Function

' objPathChk checks to make sure that a source has been selected.
Function objPathChk(myVar1)
	If myVar1 = "" Then
		MsgBox "Scan Folder Not Specified." & VbCrLf & _
				"Scan will now quit.", vbOKOnly, "Terminate"
		WScript.Quit
	End If		
End Function
'==========================================================================

Open in new window

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 40466787
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

708 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