Re: help with error checking in code NEWBIE
- From: "Jennifer" <J.Evans.1970@xxxxxxxxx>
- Date: 18 Jul 2006 06:50:33 -0700
One thing you can do is check to see if the folder exists before you
try to create it - the same way you are checking if the C:\ folder
exists.
If objFSO.FolderExists(cFOL) Then
For intSUB = 0 To UBound(arrSUB)
strSUB = arrSUB(intSUB)
sNewFolder = cFOL & "\" & strFOL.Name & "\" & strSUB
If objFSO.FolderExists(sNewFolder) = False Then
objFSO.CreateFolder(sNewFolder)
intFOL = intFOL + 1
Else
' folder already exists
End If
Next
End If
Also, one thing a lot of people do is to is to put "On Error Resume
Next at the top of a script. This will cause your script to skip over
the error and keep on moving to the next part of the script. You use
this in conjunction with checking for errors.
For example, you may have a subroutine that logs errors to a table.
Under any bit of code you have that you think might break, you put in
your error check by checking the error number. if the error number is
not zero, then there was an error. You can get the error description
by using Err.Description. What I normally do is pass the error number,
description, script name and a small description of where the error
occurred to my subroutine. It is saved in a table and I can go back
and look at it later. With the "On Error Resume Next", the script
doesn't inconveniently crash.
If Err.Number <> 0 Then
Call LogError ("DriveSpace.VBS", "Insert Into Server_DiskUsage", _
strComputer, Err.Number, Err.Source, _
Err.Description & " " & DiskID & " " & Now
)
End If
Then in the LogError subroutine I save the info in a table and (this is
important), clear the error: Err.Clear.
HTH,
Jennifer
damian.gillespie@xxxxxxxxx wrote:
Hello all,and hope someone can help me.
I have got this scipt below of the net that will make folders under
folders that are already exsisting.The problem is that it dosn't have
any error checking. If a folder already has the exsisting structure it
will just bomb out. I have absolutly no scripting experience and was
hoping if someone could modify it for me. Cheers
Damian.
'****
'* This Visual Basic Script (VBS) does the following:
'* 1) For all subfolders under a given folder (cFOL),
'* 2) Add new subfolders to each per an array (cSUB).
'****
Option Explicit
'*
'* Declare Constants
'*
Const cVBS = "addsubfolders.vbs"
Const cFOL = "C:\"
Const cSUB = "maths,english,french,german"
'*
'* Declare Variables
'*
Dim intFOL
intFOL = 0
Dim strFOL
Dim arrSUB
arrSUB = Split(cSUB,",")
Dim intSUB
Dim strSUB
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objGFO
Set objGFO = objFSO.GetFolder(cFOL)
Dim objFOL
Set objFOL = objGFO.SubFolders
'*
'* Add Subfolders
'*
If objFSO.FolderExists(cFOL) Then
For Each strFOL In objFOL
intFOL = intFOL + 1
For intSUB = 0 To UBound(arrSUB)
strSUB = arrSUB(intSUB)
'WScript.Echo cFOL & "\" & strFOL.Name & "\" & strSUB
objFSO.CreateFolder(cFOL & "\" & strFOL.Name & "\" &
strSUB)
Next
Next
End If
'*
'* Destroy Objects
'*
Set objFOL = Nothing
Set objGFO = Nothing
Set objFSO = Nothing
'*
'* Finish
'*
MsgBox intFOL & " folders processed.",vbInformation,cVBS
.
- Follow-Ups:
- Re: help with error checking in code NEWBIE
- From: damian . gillespie
- Re: help with error checking in code NEWBIE
- References:
- help with error checking in code NEWBIE
- From: damian . gillespie
- help with error checking in code NEWBIE
- Prev by Date: Re: Switch firewall on/off
- Next by Date: Total files Size by type
- Previous by thread: help with error checking in code NEWBIE
- Next by thread: Re: help with error checking in code NEWBIE
- Index(es):
Relevant Pages
|