Results 1 to 13 of 13

Thread: How to separate code blocks in vbs

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    How to separate code blocks in vbs

    Hello I have multiple .vbs files that create shortcuts for me in my start menu when I install my commandline scanner. What I would like to do is put them all into one .vbs file instead of having multiple files. How can I do this?

    Here is an example of one of the files script.
    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows Environment Variable.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt_LAN.lnk")
      objLink.Description = "Shortcut to downIt_LAN"
      objLink.TargetPath = "C:\scanner\Updater\BATS\DownIt_LAN.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\scanner\Updater\BATS"
      objLink.Hotkey = "CTRL+SHIFT+ALT+L"
      objLink.Description = "This will update the scanner from LAN server."
      objLink.Save
    end sub
    
    ' Program starts running here.
    call CreateShortCut()
    and here is an example of another one of the vbs scripts I want to put into one file with the script above.
    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows desktop.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = "C:\scanner\Updater\BATS\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\scanner\Updater\BATS"
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    
    ' Program starts running here.
    call CreateShortCut()
    and finally just incase it is different if more than 2 a third script I want to put in so all 3 of these separated files are all in one script. Is this possible?

    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows desktop.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\killAll.lnk")
      objLink.Description = "Shortcut to killAll"
      objLink.TargetPath = "C:\killAll.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\"
      objLink.Hotkey = "CTRL+SHIFT+ALT+END"
      objLink.Description = "This will end the processes of alot."
      objLink.Save
    end sub
    
    ' Program starts running here.
    call CreateShortCut()
    Thank you in advance.

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    Re: How to separate code blocks in vbs

    Quote Originally Posted by papayrus View Post
    Hello I have multiple .vbs files that create shortcuts for me in my start menu when I install my commandline scanner. What I would like to do is put them all into one .vbs file instead of having multiple files. How can I do this?

    Here is an example of one of the files script.
    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows Environment Variable.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt_LAN.lnk")
      objLink.Description = "Shortcut to downIt_LAN"
      objLink.TargetPath = "C:\scanner\Updater\BATS\DownIt_LAN.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\scanner\Updater\BATS"
      objLink.Hotkey = "CTRL+SHIFT+ALT+L"
      objLink.Description = "This will update the scanner from LAN server."
      objLink.Save
    end sub
    
    ' Program starts running here.
    call CreateShortCut()
    and here is an example of another one of the vbs scripts I want to put into one file with the script above.
    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows desktop.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = "C:\scanner\Updater\BATS\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\scanner\Updater\BATS"
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    
    ' Program starts running here.
    call CreateShortCut()
    and finally just incase it is different if more than 2 a third script I want to put in so all 3 of these separated files are all in one script. Is this possible?

    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows desktop.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\killAll.lnk")
      objLink.Description = "Shortcut to killAll"
      objLink.TargetPath = "C:\killAll.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\"
      objLink.Hotkey = "CTRL+SHIFT+ALT+END"
      objLink.Description = "This will end the processes of alot."
      objLink.Save
    end sub
    
    ' Program starts running here.
    call CreateShortCut()
    Thank you in advance.
    I think I came up with it. Can someone tell me if this is proper safe coding for this?

    Code:
    ' Make sure variables are declared.
    option explicit
    
    '
    ' Routine to create "mylink.lnk" on the Windows Environment Variable.
    '
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt_LAN.lnk")
      objLink.Description = "Shortcut to downIt_LAN"
      objLink.TargetPath = "C:\scanner\Updater\BATS\DownIt_LAN.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\scanner\Updater\BATS"
      objLink.Hotkey = "CTRL+SHIFT+ALT+L"
      objLink.Description = "This will update the scanner from LAN server."
      objLink.Save
    end sub
    
    sub CreateShortCut2()
    dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = "C:\scanner\Updater\BATS\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\scanner\Updater\BATS"
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    
    sub CreateShortCut3()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\killAll.lnk")
      objLink.Description = "Shortcut to killAll"
      objLink.TargetPath = "C:\killAll.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = "C:\"
      objLink.Hotkey = "CTRL+SHIFT+ALT+END"
      objLink.Description = "This will end the processes of alot."
      objLink.Save
    end sub
    
    
    ' Program starts running here.
    call CreateShortCut()
    call CreateShortCut2()
    call CreateShortCut3()

  3. #3
    Addicted Member
    Join Date
    Jul 2009
    Posts
    208

    Re: How to separate code blocks in vbs

    Yes, that looks OK, though I prefer to put the Calls at the top of the code because code written outside any subroutine/function comprises the 'main' procedure and I can see immediately what it is. Subordinate routines/functions called by 'main' are then defined below it.

    You can call a subroutine without the Call keyword like this:
    Code:
    CreateShortCut
    CreateShortCut2
    CreateShortCut3

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    Re: How to separate code blocks in vbs

    OK done got one last question. I want to make the target directory and the working directory be the current directory this is what I came up with and it seems to be working can you tell me if this code is alright?

    Code:
    option explicit
    call CreateShortCut()
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = objShell.CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = objShell.CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    Quote Originally Posted by His Nibbs View Post
    Yes, that looks OK, though I prefer to put the Calls at the top of the code because code written outside any subroutine/function comprises the 'main' procedure and I can see immediately what it is. Subordinate routines/functions called by 'main' are then defined below it.

    You can call a subroutine without the Call keyword like this:
    Code:
    CreateShortCut
    CreateShortCut2
    CreateShortCut3
    Last edited by papayrus; Apr 4th, 2012 at 07:53 PM.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    17

    Re: How to separate code blocks in vbs

    Quote Originally Posted by papayrus View Post
    OK done got one last question. I want to make the target directory and the working directory be the current directory this is what I came up with and it seems to be working can you tell me if this code is alright?

    Code:
    option explicit
    call CreateShortCut()
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = objShell.CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = objShell.CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    Try this:

    Code:
    'Define the current path
    CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
    'If running from desktop in WinXP result will be: 
    'C:\Documents and Settings\username\Desktop\downIt.bat
    TargetPath = CurrentDirectory & "\downIt.bat"
    
    'If running from desktop in WinXP result will be:
    'C:\Documents and Settings\username\Desktop
    WorkingDirectory = CurrentDirectory

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    Re: How to separate code blocks in vbs

    I changed my code to this and now I get an error.

    Code:
    ' Make sure variables are declared.
    option explicit
    ' Program starts running here.
    call CreateShortCut()
    
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      TargetPath = CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      WorkingDirectory = CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    Last edited by papayrus; Apr 5th, 2012 at 12:08 AM.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    17

    Re: How to separate code blocks in vbs

    Quote Originally Posted by papayrus View Post
    I changed my code to this and now I get an error.

    Code:
    ' Make sure variables are declared.
    option explicit
    ' Program starts running here.
    call CreateShortCut()
    
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      TargetPath = CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      WorkingDirectory = CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    With the Option Explicit defined you need to include/set all variables accordingly. Try this:
    Code:
    ' Make sure variables are declared.
    option explicit
    ' Program starts running here.
    call CreateShortCut()
    
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink, CurrentDirectory, TargetPath, WorkingDirectory
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      TargetPath = CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      WorkingDirectory = CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    Re: How to separate code blocks in vbs

    Quote Originally Posted by BoOkWoRm View Post
    With the Option Explicit defined you need to include/set all variables accordingly. Try this:
    Code:
    ' Make sure variables are declared.
    option explicit
    ' Program starts running here.
    call CreateShortCut()
    
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink, CurrentDirectory, TargetPath, WorkingDirectory
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      TargetPath = CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      WorkingDirectory = CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub
    This actually has no errors and creates the link in accessories and all the items are there accept for the target path and the start in directory boxes are empty in the shortcuts properties. I created a directory and put the downIt.bat in it with the make shortcut VBS here. Am I doing something wrong? I added an attachment screenshot of the links shortcut properties so you can see what I mean.
    EDIT::
    Just want to add that if in the 2 lines TargetPath = CurrentDirectory & "\downIt.bat" and WorkingDirectory = CurrentDirectory I add to the beginning of those lines objLink. it seems to work perfectly. So should I change them to objLink.TargetPath = CurrentDirectory & "\downIt.bat" and objLink.WorkingDirectory = CurrentDirectory or is this wrong? I also find that if I delete the declarations of TargetPath and WorkingDirectory it is working fine as well.
    Attached Images Attached Images  
    Last edited by papayrus; Apr 5th, 2012 at 01:18 PM.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    17

    Re: How to separate code blocks in vbs

    You need to specify the path for the shortcut, example:

    Code:
    objLink.TargetPath = CurrentDirectory & "\downIt.bat"

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    Re: How to separate code blocks in vbs

    Quote Originally Posted by BoOkWoRm View Post
    You need to specify the path for the shortcut, example:

    Code:
    objLink.TargetPath = CurrentDirectory & "\downIt.bat"
    Ok this is what I have now is this good code or should I still declare WorkingDirectory and TargetPath after DIM or no need?

    Code:
    option explicit
    ' Program starts running here.
    call CreateShortCut()
    
    
    sub CreateShortCut()
      dim objShell, strEnvironment, objLink, CurrentDirectory
      set objShell = CreateObject("WScript.Shell")
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    end sub

  11. #11
    Addicted Member
    Join Date
    Jul 2009
    Posts
    208

    Re: How to separate code blocks in vbs

    Quote Originally Posted by papayrus View Post
    should I still declare WorkingDirectory and TargetPath after DIM or no need?
    WorkingDirectory and TargetPath are properties of the objLink variable (a shortcut object) which you have declared, so they are not separate variables and there's no need to declare (Dim) them.

    With Option Explicit you will get an undefined variable error message if any variables are undefined, so it serves as an essential development aid. Therefore always use Option Explicit.

  12. #12

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    7

    Re: How to separate code blocks in vbs

    Quote Originally Posted by His Nibbs View Post
    WorkingDirectory and TargetPath are properties of the objLink variable (a shortcut object) which you have declared, so they are not separate variables and there's no need to declare (Dim) them.

    With Option Explicit you will get an undefined variable error message if any variables are undefined, so it serves as an essential development aid. Therefore always use Option Explicit.
    Ok so they are part of objLink already like a family but I am confused then about current directory is that part of the objLink property?
    EDIT I just thought about it am thinking that no it is not because it is a directory in itself and not part of what a link would be or something like that.

  13. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    17

    Re: How to separate code blocks in vbs

    Quote Originally Posted by papayrus View Post
    Ok so they are part of objLink already like a family but I am confused then about current directory is that part of the objLink property?
    EDIT I just thought about it am thinking that no it is not because it is a directory in itself and not part of what a link would be or something like that.
    For the sake of sanity and house cleaning, here's a better example to your final code above:

    Code:
    Option Explicit
    Dim objShell, strEnvironment, objLink, CurrentDirectory
    Set objShell = CreateObject("WScript.Shell")
    CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
    ' Program starts running here.
    Call CreateShortCut()
    
    Sub CreateShortCut()
      strEnvironment = objShell.ExpandEnvironmentStrings("%USERPROFILE%\Start Menu\Programs\Accessories")
      Set objLink = objShell.CreateShortcut(strEnvironment & "\downIt.lnk")
      objLink.Description = "Shortcut to downIt"
      objLink.TargetPath = CurrentDirectory & "\downIt.bat"
      objLink.WindowStyle = 1
      objLink.WorkingDirectory = CurrentDirectory
      objLink.Hotkey = "CTRL+SHIFT+ALT+U"
      objLink.Description = "This will update the scanner from the internet."
      objLink.Save
    END Sub

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