Re: help with error checking in code NEWBIE

Tech-Archive recommends: Fix windows errors by optimizing your registry



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

.



Relevant Pages

  • Re: help with error checking in code NEWBIE
    ... Below is the entire script again with an error check for creating the ... in each folder on your C Drive? ... Dim intFOL ...
    (microsoft.public.scripting.vbscript)
  • Re: Script to create subdirectories
    ... Then the rest of the script (i.e. the Function that I posted, ... Dim aFolders, newFolder ... ' Splits the various components of the folder name. ...
    (microsoft.public.windows.server.scripting)
  • Re: map web folder
    ... I ran through the creation script one time today and it worked like a charm! ... until then here is the script to create a web folder ... Dim iRes, jRes, MT, TT ... bString = bString & Chr ...
    (microsoft.public.scripting.wsh)
  • Re: Create OU and users with VBS and Excel
    ... OU each user should be created in, and what profile path to use. ... However, if you want to automate this all in one script, read the OU ... cacls command to assign privileges for the folder to the new user object. ... Dim objRootLDAP, objContainer, objUser, objShell ...
    (microsoft.public.scripting.vbscript)
  • Re: help with error checking in code NEWBIE
    ... Below is the entire script again with an error check for creating the ... in each folder on your C Drive? ... Dim intFOL ...
    (microsoft.public.scripting.vbscript)