Results 1 to 15 of 15

Thread: VBscript help..

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBscript help..

    vb Code:
    1. Case 76
    2.                 ' 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
    3.                 if instr(f1 , "Folder\Databases\Normal") > 0 then  mystr = mystr & f1 & "  - Path not found" & vbnewline ' add to string here
    4.                     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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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'.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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".

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. if something = somethingelse then dothis
    or
    vb Code:
    1. if something = somethingelse then
    2.    dothis
    3. 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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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??

    Name:  String.jpg
Views: 249
Size:  16.7 KB
    Last edited by Kubull; Feb 8th, 2008 at 11:26 AM.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBscript help..

    looks like it should maybe f1.path
    vb Code:
    1. 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

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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)

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Dim fso, f, fc, f1, SourceFile
    2. Set fso = CreateObject("Scripting.FileSystemObject")
    3. Set SourceFile = fso.GetFile("c:\book1.txt")
    4. Set f = fso.GetFolder("c:\temp")
    5. Set fc = f.SubFolders
    6. For Each f1 In fc
    7. 'If InStr(f1.Name, ",") > 0 Then
    8.     On Error Resume Next
    9.         SourceFile.Copy f1 & "\test\"
    10.         If Err.Number <> 0 Then
    11.             Select Case Err.Number    ' Evaluate error number.
    12.               Case 70
    13.                 mystr = mystr & f1 & "  - Permission Denied" & vbNewLine 'add to string here
    14.               Case 76
    15.                 If InStr(f1.Path, "test\") = 0 Then mystr = mystr & f1.Path & "  - Path not found" & vbNewLine
    16.                 Case Else
    17.              End Select
    18.              Err.Clear
    19.         Else
    20.         End If
    21.         On Error GoTo 0
    22. 'End If
    23. 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

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jan 2008
    Posts
    106

    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

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
  •  



Click Here to Expand Forum to Full Width