|
-
Feb 4th, 2008, 12:02 PM
#1
Thread Starter
Lively Member
VBscript help..
The below code works like a dream, it copies one file and distributes it to a large number of directories on a shared folder located on our server, it also records the directories it has skipped due to error.
Throughout the shared folder, there is a mix of different Normal.dot templates, some get one and some get another. We differentiate between each user by having a folder in each users directory called either 'Normal' or 'Normal_PM', I use the two versions of this script to do this.
When the script run and updates, it records errors but it also records non-errors due to the file path being different IE
K:\Named, Folder\Databases\Normal - Path Not Found (this isn't an error it's just a different file path)
Is there a line of code I can add to this script that can say something like -
If file path is "Databases\Normal_PM" skip and resume next?
Code:
Dim fso, f, fc, f1, SourceFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set SourceFile = fso.GetFile("K:\Noticeboard\NormalUpdates\Normal\Normal.dot")
Set f = fso.GetFolder("K:\")
Set fc = f.SubFolders
For Each f1 In fc
If InStr(f1.Name, ",") > 0 Then
On Error Resume Next
SourceFile.Copy f1 & "\Databases\normal\"
If Err.Number <> 0 Then
Select Case Err.Number ' Evaluate error number.
Case 70
mystr = mystr & F1 & " - Permission Denied" & vbnewline 'add to string here
Case 76
mystr = mystr & f1 & " - Path not found" & vbnewline ' add to string here
Err.Clear
Case Else
End Select
Else
End If
On Error GoTo 0
End If
Next
Set F1 = fso.CreateTextFile("K:\Noticeboard\Update Script Logs\NormalUpdateScriptLog.txt", True)
f1.Write mystr
f1.Close
Set f1 = nothing
set fso = nothing
-
Feb 4th, 2008, 03:38 PM
#2
Re: VBscript help..
vb Code:
Case 76 ' i thnk this is right, but you would need to test if it does what you want, if the normal is not found then adds to log if instr(f1 , "Folder\Databases\Normal") > 0 then mystr = mystr & f1 & " - Path not found" & vbnewline ' add to string here Err.Clear
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 5th, 2008, 05:35 AM
#3
Thread Starter
Lively Member
Re: VBscript help..
Thanks again Westconn1, The Code you gave me works well, Although I ran it a few times on a test area, and then in VBA, the rest of the code works but it ignores the rest of the line after 'Then'.
-
Feb 5th, 2008, 05:51 AM
#4
Re: VBscript help..
what is the value of f1 at that point?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 6th, 2008, 05:13 AM
#5
Thread Starter
Lively Member
Re: VBscript help..
Not sure, how would I check for that?
I noticed in the script that most 'Then' statements end with a new line, but it didn't like it, complaining of Case Else "Compile Error: Case Else outside Select Case".
-
Feb 6th, 2008, 05:44 AM
#6
Re: VBscript help..
msgbox f1
will show the value of f1 at the point when it is displayed
you can have if then either
vb Code:
if something = somethingelse then dothis
or
vb Code:
if something = somethingelse then dothis end if
the first example is good for single line if statements, the second where there are many statements base on the condition, or if you want to use else or elseif
so no newline after then unless you put end if on the following line
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 8th, 2008, 08:33 AM
#7
Thread Starter
Lively Member
Re: VBscript help..
Gotcha,
This is Screen dump when I run the script through VBA, as you can see the F1 Value is the file path for one of the failed folders (pre-setup), the line stops at then, when I hit F8 again it bypass's the rest of the string. Hmmm??
Last edited by Kubull; Feb 8th, 2008 at 11:26 AM.
-
Feb 8th, 2008, 03:01 PM
#8
Re: VBscript help..
If f1 contains "\Databases\Normal_PM" then the condition is > 0 otherwise it is 0
However I suggest this:
If InStr(1, f1, "\Databases\Normal_PM", vbTextCompare) > 0 Then ......
Last edited by jmsrickland; Feb 8th, 2008 at 03:08 PM.
-
Feb 10th, 2008, 01:10 AM
#9
Re: VBscript help..
looks like it should maybe f1.path
vb Code:
if instr(f1.path , "Folder\Databases\Normal") > 0 then mystr = mystr & f1.path & " - Path not found" & vbnewline
also as jm points out, if you want it to add to log when that string is not present, should be = 0 ' not found
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 10th, 2008, 01:50 AM
#10
Re: VBscript help..
I didn't look at any previous posts I just responded to post 7. Looking back now I think you are correct and it should be f1.path assuming that is a valid property of f1. He should still usr the vbTextCompare however.
-
Feb 10th, 2008, 02:18 AM
#11
Re: VBscript help..
yeah, i had to sort of run the code to find out what f1 actually returned
though the default returned for f1 was the same as f1.path, but post 7 shows path, fail as the default
Last edited by westconn1; Feb 10th, 2008 at 02:22 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 11th, 2008, 10:06 AM
#12
Thread Starter
Lively Member
Re: VBscript help..
Thanks alot guys, the code works again. But alas it still logs the path not found for the Error and the non-errors
K:\FOLDER, Name\Test Area\XXX, Fail - Path not found (Actual Error)
K:\FOLDER, Name\Test Area\XXX, Fail2 - Path not found (Actual Error)
K:\FOLDER, Name\Test Area\XXX, Fail3 - Path not found (Actual Error)
K:\FOLDER, Name\Test Area\ZZZ, Okay2 - Path not found (Normal_PM)
K:\FOLDER, Name\Test Area\ZZZ, Okay3 - Path not found (Normal_PM)
-
Feb 11th, 2008, 03:20 PM
#13
Re: VBscript help..
have you got err.clear in the code for case 76
the best way is to put err.clear after end select then it always cleared
i tested this and it seemed to work correctly
vb Code:
Dim fso, f, fc, f1, SourceFile Set fso = CreateObject("Scripting.FileSystemObject") Set SourceFile = fso.GetFile("c:\book1.txt") Set f = fso.GetFolder("c:\temp") Set fc = f.SubFolders For Each f1 In fc 'If InStr(f1.Name, ",") > 0 Then On Error Resume Next SourceFile.Copy f1 & "\test\" If Err.Number <> 0 Then Select Case Err.Number ' Evaluate error number. Case 70 mystr = mystr & f1 & " - Permission Denied" & vbNewLine 'add to string here Case 76 If InStr(f1.Path, "test\") = 0 Then mystr = mystr & f1.Path & " - Path not found" & vbNewLine Case Else End Select Err.Clear Else End If On Error GoTo 0 'End If Next
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 13th, 2008, 07:14 AM
#14
Thread Starter
Lively Member
Re: VBscript help..
Thanks guys, alas the code still log both error and no-error file paths. I'm starting to understand the code a little, and to be honest I'm probably asking too much of it, it either ignores the file path completely or records them all. at the end of the day I suppose you can't tell the script to ignore one error 76 and then record another.
Thanks for trying, all the best
-
Feb 13th, 2008, 03:35 PM
#15
Re: VBscript help..
i tested the code, and it should work fine
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|