+ Post New Thread
Results 1 to 12 of 12
Scripts Thread, can someone help with a vbs script in Coding and Web Development; been staring at scripts all day and my brains turned to mush if left (existing,1) = "#" and right (existing,1) ...
  1. #1


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,504
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287

    can someone help with a vbs script

    been staring at scripts all day and my brains turned to mush

    if left (existing,1) = "#" and right (existing,1) not = "," then outputstring = existing & " : " & outputstring

    basically trying to read a value and check the first and last characters such that if it starts with # and ends with , alter a variable but all i keep getting is expected then but it looks correct to me

  2. #2

    witch's Avatar
    Join Date
    Nov 2005
    Location
    Dorset
    Posts
    11,042
    Thank Post
    1,315
    Thanked 2,308 Times in 1,623 Posts
    Rep Power
    692
    Quote Originally Posted by sted View Post
    been staring at scripts all day and my brains turned to mush

    if left (existing,1) = "#" and right (existing,1) not = "," then outputstring = existing & " : " & outputstring

    basically trying to read a value and check the first and last characters such that if it starts with # and ends with , alter a variable but all i keep getting is expected then but it looks correct to me
    @sted
    Try something like this

    if instr(existing, "#") = 1 and inStr(StrReverse(existing), ",") = 1 then outputstring = existing & " : " & outputstring


    (Just in case those of you who know me think I have gone mad - mrwITch posted this!!)
    Last edited by witch; 23rd March 2012 at 08:44 PM.

  3. 2 Thanks to witch:

    mac_shinobi (25th March 2012), sted (24th March 2012)

  4. #3


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,504
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287
    thanks both witches lol its not erroring now just need to alter last bit to be not ","
    sorted had to ad an escape clause below it as well but script now works thanks
    Last edited by sted; 24th March 2012 at 11:02 AM.

  5. #4

    Join Date
    Feb 2009
    Posts
    45
    Thank Post
    1
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Quote Originally Posted by sted View Post
    thanks both witches lol its not erroring now just need to alter last bit to be not ","
    sorted had to ad an escape clause below it as well but script now works thanks
    Sorry didn't read original posting properly
    for your 'not =' use <>

  6. Thanks to mrwITch from:

    sted (24th March 2012)

  7. #5


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,504
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287
    Quote Originally Posted by mrwITch View Post
    Sorry didn't read original posting properly
    for your 'not =' use <>
    Just changed
    if instr(existing, "#") = 1 and inStr(StrReverse(existing), ",") = 1 then outputstring = existing & " : " & outputstring
    To
    if instr(existing, "#") = 1 and not inStr(StrReverse(existing), ",") = 1 then outputstring = existing & " : " & outputstring
    Seemed to work but in testing while the expression is fine it didn't do quite what i wanted lol typical so next line is if first and last character is a # then quit (changed final , for a # as its easier to spot

  8. #6

    Join Date
    Feb 2009
    Posts
    45
    Thank Post
    1
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Quote Originally Posted by sted View Post
    Just changed
    if instr(existing, "#") = 1 and inStr(StrReverse(existing), ",") = 1 then outputstring = existing & " : " & outputstring
    To
    if instr(existing, "#") = 1 and not inStr(StrReverse(existing), ",") = 1 then outputstring = existing & " : " & outputstring
    Seemed to work but in testing while the expression is fine it didn't do quite what i wanted lol typical so next line is if first and last character is a # then quit (changed final , for a # as its easier to spot
    the
    not inStr(StrReverse(existing), ",") = 1
    won't work

    Use the following
    if instr(existing, "#") = 1 and inStr(1, StrReverse(existing), ",") = 0 then outputstring = existing & " : " & outputstring

    should do it

    This now tests the 1st character (after reversing the string)
    The = 0 should indicate that a "," wasn't found

    So your complete test
    if instr(existing, "#") = 1 and inStr(1, StrReverse(existing), ",") = 0 then outputstring = existing & " : " & outputstring

    should be read as follows
    If the 1st char of existing is a "#" and the last char of existing is NOT a "," then modify outputstring

  9. #7


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,504
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287
    Seems to work ok i just didn't 100% think it through so it works right but it had an unexpected side effect

  10. #8

    Join Date
    Feb 2009
    Posts
    45
    Thank Post
    1
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Just re-thought this -- instr is NOT the best way of handling this

    if mid(existing, 1, 1) = "#" and mid(strReverse(existing), 1, 1) <> "," then outputstring = existing & " : " & outputstring

    The above is a better test for what you wanted


    ?? what was the unexpected side effect ??

  11. #9

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,677
    Thank Post
    3,209
    Thanked 1,030 Times in 955 Posts
    Rep Power
    361
    Maybe just me ( could be totally wrong and off the mark here so apologies in advance as am not an expert by any means of the word ) but using all these different functions ie InStrRev, StrReverse etc just seems to over complicate it ??

    Code:
    if left (existing,1) = "#" and right (existing,1) <> "," then outputstring = existing & " : " & outputstring
    Something along those lines ?

  12. #10

    Join Date
    Feb 2009
    Posts
    45
    Thank Post
    1
    Thanked 5 Times in 5 Posts
    Rep Power
    11
    Quote Originally Posted by mac_shinobi View Post
    Maybe just me ( could be totally wrong and off the mark here so apologies in advance as am not an expert by any means of the word ) but using all these different functions ie InStrRev, StrReverse etc just seems to over complicate it ??

    Code:
    if left (existing,1) = "#" and right (existing,1) <> "," then outputstring = existing & " : " & outputstring
    Something along those lines ?
    Yeah - that should also do it

  13. #11


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,504
    Thank Post
    227
    Thanked 848 Times in 727 Posts
    Rep Power
    287
    Quote Originally Posted by mrwITch View Post
    Just re-thought this -- instr is NOT the best way of handling this

    if mid(existing, 1, 1) = "#" and mid(strReverse(existing), 1, 1) <> "," then outputstring = existing & " : " & outputstring

    The above is a better test for what you wanted


    ?? what was the unexpected side effect ??
    what it does is read a variable from active directory. in theory what it was supposed to do was if it started with a hash but diddnt end with one take the original data add some more and then add a hash. on next run do nowt. what it did was run that bit on the first run through then on 2nd runthrough overwrite the data i was trying to save lol. i just added another if start and end =# exit the script line not 100% what i wanted but does the job well enough

  14. #12

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,686
    Thank Post
    333
    Thanked 513 Times in 481 Posts
    Rep Power
    178
    I may be totally off the mark here but like this?

    Code:
    existing = "#test"
    wscript.echo existing
    
    
    if left(existing,1) = "#" and right(existing,1) <> "#" then existing = existing & "newdata" & "#"
    
    wscript.echo existing
    
    if left(existing,1) = "#" and right(existing,1) <> "#" then existing = existing & "newdata" & "#"
    
    wscript.echo existing
    Take in a variable (from AD for you though),
    If it doesn't end in # add "sometext"
    Even if run again, do nothing to do it as it end sin # now?

    Steve

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 0
    Last Post: 22nd November 2009, 11:07 AM
  2. excel lookup problem, can someone help me please?
    By RabbieBurns in forum Windows
    Replies: 3
    Last Post: 2nd July 2008, 12:51 PM
  3. can someone help with my wireless
    By darbyshire in forum Wireless Networks
    Replies: 2
    Last Post: 14th May 2008, 10:13 PM
  4. Replies: 2
    Last Post: 13th June 2007, 10:45 AM
  5. Been thick, can someone help..
    By timbo343 in forum Windows
    Replies: 7
    Last Post: 15th March 2007, 06:20 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •