+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, Sub within a sub - VBS Script in Coding and Web Development; Hi What I am doing is re-doing the sartup script to work with vista as well. This is a snipit ...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,940
    Thank Post
    886
    Thanked 1,693 Times in 1,472 Posts
    Blog Entries
    12
    Rep Power
    447

    Question Sub within a sub - VBS Script

    Hi
    What I am doing is re-doing the sartup script to work with vista as well.

    This is a snipit of my code:

    Code:
    If filesys.FileExists ("C:\WINDOWS\Web\Wallpaper\Windows XP.jpg") Then call xp2003
    If filesys.FileExists ("C:\WINDOWS\Web\Wallpaper\Windows Server 2003.bmp") Then call xp2003
    If filesys.FileExists ("C:\Users\Default\ntuser.dat.log") Then call nt6
    
    ‘******Windows XP & 2003******
    sub xp2003
    If Not fso.FileExists("C:\Documents and Settings\All Users\Start Menu\Programs\Kar2ouche\kar2ouche.lnk") Then 
       call kar2ouche
    End If
    sub kar2ouche
    	filesys.CreateFolder "C:\Documents and Settings\All Users\Start Menu\Programs\Kar2ouche"
    	filesys.CopyFile "\\server\NETLOGON\shortcuts\Kar2ouche.lnk", "c:\Documents and Settings\All Users\Start Menu\Programs\Kar2ouche\"
    
    END sub
    fso.DeleteFile("C:\documents and settings\all users\desktop\Microsoft Student with Encarta Premium 2008 DVD.lnk")
    END sub
    ‘******Windows Vista & 2008******
    Sub nt6
    END sub
    Basically I have a sub within a sub and it doesn’t work. I want to keep the start-up scrip al in one and not have separate ones for Vista & XP. How do I go about this please?

    Please Note: There will be some code in the Vista section but i haven't done it yet.

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,143
    Thank Post
    863
    Thanked 2,695 Times in 2,285 Posts
    Blog Entries
    9
    Rep Power
    772
    Your subroutine should be defined outside the other subroutine just like the first one, you should then call the subroutine if needed from within your subroutine. If the subroutine is not called it is not executed and so should not slow the execution down.

    If you are doing this to clean up the code and make it more readable you could put all of your subroutines in an include file like this

    Basically you chuck all of your subroutines in the VBS file and then run the WSF that includes the VBS, much cleaner and it only need WSH 2.0 (in Windows 2000) or higher to run them.

  3. #3

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,940
    Thank Post
    886
    Thanked 1,693 Times in 1,472 Posts
    Blog Entries
    12
    Rep Power
    447
    just like the first one
    Do you mean the XP section?

    Im not doing it to clean it up just dont want Vista running the XP code and vise versa.

  4. #4

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,143
    Thank Post
    863
    Thanked 2,695 Times in 2,285 Posts
    Blog Entries
    9
    Rep Power
    772
    I mean that all of the subroutines should be flat and not nested inside one another like this:
    Code:
    Sub DoOneThing
    MSGbox "One Thing"
    if bla then DoAnotherThingForXP
    End Sub
    
    Sub DoAnotherThingForXP
    EndSub
    
    Sub DoAnotherThingForVista
    EndSub
    As long as the code does not call the routine they will not be run so if your logic says if XP then do this bunch of stuff defined in these subroutines it will only run those subroutines that are called from your main block of code.

    Basically if I define a subroutine and do not call it in a script it will not run that chunk of code.

  5. #5

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    On a slightly different note, if you want to determine the version of Windows, it's probably better to use WMI rather than checking for files which might not be there (particularly bitmaps which might get deleted when you decide to try and tidy up the image ...)

    The function below will give you back the version number of Windows - eg 5.1.2600 for XP; 5.2.3790 for Server 2003.

    Code:
    function GetOS
      Set oWMI = GetObject("winmgmts:\\.\root\CIMV2")
      Set colItems = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
      For Each oItem in colItems
        GetOS=oItem.Version
      Next
    end function

  6. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,756
    Thank Post
    3,265
    Thanked 1,052 Times in 973 Posts
    Rep Power
    365

    Win32 class for wmi

    if you look here :

    Win32_OperatingSystem Class (Windows)

    you can use the caption property in that code possibly and that should tell you the version as well as a string of what os it is.

    Code:
    function GetOS
      Set oWMI = GetObject("winmgmts:\\.\root\CIMV2")
      Set colItems = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
      For Each oItem in colItems
        GetOS=oItem.Caption    '<-- changed it here
      Next
    end function

SHARE:
+ Post New Thread

Similar Threads

  1. Script Spy
    By bizzel in forum Scripts
    Replies: 6
    Last Post: 9th March 2008, 12:15 AM
  2. Help with KIX script please
    By salan in forum Windows
    Replies: 8
    Last Post: 8th November 2007, 06:55 PM
  3. Best script ever!
    By Zabu in forum Scripts
    Replies: 0
    Last Post: 6th November 2007, 11:48 PM
  4. Logon script & Printer script question.
    By Galway in forum Windows
    Replies: 3
    Last Post: 29th August 2007, 11:00 AM
  5. Does anyone have a script...
    By wesleyw in forum Scripts
    Replies: 4
    Last Post: 5th July 2007, 01:58 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
  •