Re: help with error checking in code NEWBIE
- From: "Jennifer" <J.Evans.1970@xxxxxxxxx>
- Date: 19 Jul 2006 11:31:55 -0700
I would put some error checking right under the CreateFolder line:
objFSO.CreateFolder(cFOL & "\" & strFOL.Name & "\" & strSUB)
Below is the entire script again with an error check for creating the
folder. Watch for line wrapping.
Just out of curiosity... do you really want to put these sub folders
in each folder on your C Drive? That would put them in your Windows
folder. Probably wouldn't actually hurt anything. I'm just curious as
to why you'd do that.
HTH,
Jennifer
'****
'* 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
On Error Resume Next
'*
'* 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)
sNewFolder = cFOL & "\" & strFOL.Name & "\" & strSUB
If objFSO.FolderExists(sNewFolder) = False Then
objFSO.CreateFolder(sNewFolder)
If Err.Number <> 0 Then
' You had an error creating the folder.
' If you want to keep going, you could
' write the error to a file.
' With On Error Resume Next, your script
' won't automatically quit.
' Write to file example:
'Dim fil
'Set fil = objFSO.CreateTextFile ("C:\ErrorLog.txt"),
true
'fil.Write sNewFolder & " | " & Err.Number & " | " &
Err.Description & vbcrlf
' If you uses trhis, move the Dim and Set statements
' out of the loop.
End If
End If
Next
Next
End If
'*
'* Destroy Objects
'*
Set objFOL = Nothing
Set objGFO = Nothing
Set objFSO = Nothing
'*
'* Finish
'*
MsgBox intFOL & " folders processed.",vbInformation,cVBS
damian.gillespie@xxxxxxxxx wrote:
Thanks Jennifer
I have been trying to make the mods with your code but i just can't
seem to get it to work. I really don't know where to stick the code.
Jennifer wrote:
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
- From: damian . gillespie
- Re: help with error checking in code NEWBIE
- References:
- help with error checking in code NEWBIE
- From: damian . gillespie
- Re: help with error checking in code NEWBIE
- From: Jennifer
- Re: help with error checking in code NEWBIE
- From: damian . gillespie
- help with error checking in code NEWBIE
- Prev by Date: Re: Map Network Drive from a file
- Next by Date: Re: Help with this regular expression:
- Previous by thread: Re: help with error checking in code NEWBIE
- Next by thread: Re: help with error checking in code NEWBIE
- Index(es):
Relevant Pages
|