VB Script - QB64 Wiki

VB Script

From QB64 Wiki

Jump to: navigation, search

VB Script can be used to access the Windows API to do various tasks such as create shortcuts or get Operating System values. Text file names with the VBS extension can be created in Notepad or in a QB64 program routine that can supply the script values.

Contents


  • Use the following command in Qbasic or QB64 to run a script file without the Microsoft notice:
SHELL _HIDE "CSCRIPT //NOLOGO " + scriptfile$
  • VBS Script files can also be manually clicked on to run the script. Running Scripts from the Command Line
  • Long code lines can use an _ underscore to continue the code to the next text row
  • Message boxes are easy to create using MsgBox "Text string message".


REQUIRES INTERNET EXPLORER 8 on XP or newer Windows machines!


In the following examples, a QB program can create the variable values and the VBS script text file to run with SHELL.
Highlighted text values can be changed to fit program needs.

Startup Program

Script to create shortcut in the Startup folder where Hello.lnk is the shortcut and C:\programs\Hello.exe is the target start up program.

'Place VBS script file anywhere Dim objWshShl Dim strTargetFolder Dim objProgramsShortcut Set objWshShl = WScript.CreateObject("WScript.Shell") strTargetFolder = objWshShl.SpecialFolders("Programs") 'Special Startup folder and link file name Set objProgramsShortcut = objWshShl.CreateShortcut(strTargetFolder & "\Startup\Hello.lnk") objProgramsShortcut.TargetPath = "c:\programs\Hello.exe" '<<<< target path objProgramsShortcut.Save

Courtesy of Terry Ritchie
Note: String variables inside of quotes can be substituted for the highlighted text above to create a custom VBS file in Qbasic.
PRINT #1, "objProgramsShortcut.TargetPath = " + CHR$(34) + "c:\programs\Hello.exe" + CHR$(34)


It is recommended that program users be aware of and authorize all programs that start up with Windows!
They can be manually deleted or dragged from the Startup folder in the Start All Programs Menu.

(Return to top)

LNK Shortcut

Creates a shortcut with icon in the current QB64 directory to run the QB64 IDE. Supply the QB64.EXE path to use it in other folders.

'Place VBS script file in QB64 folder with QB64.EXE! Set oWS = CreateObject("WScript.Shell") sPath = oWS.CurrentDirectory 'current path of target sLinkFile = sPath & "\QB64.LNK" Set oLink = oWS.CreateShortcut(sLinkFile) oLink.TargetPath = sPath & "\QB64.EXE" 'Target program 'oLink.Arguments = " -c " & file 'command arguments to compile a file name oLink.Description = "QB64 IDE" 'Comment title 'oLink.HotKey = "ALT+CTRL+Q" oLink.IconLocation = sPath & "\QB64icon.ICO, 0" 'icon in current folder oLink.WindowStyle = "1" oLink.WorkingDirectory = sPath 'working path oLink.Save

Note: Requires QB64icon.ico (Rt click to download) icon. The Arguments string can be used to compile a specified program.


Creates a shortcut with icon only on the Current User's Desktop to run the QB64 IDE. Other user desktops are not affected.

'Place VBS script file in QB64 folder with QB64.EXE! Set oWS = CreateObject("WScript.Shell") sPath = oWS.CurrentDirectory 'current path of target Set oEnv = oWS.Environment("Process") sProfile = oEnv.Item("UserProfile") 'user's profile path sLinkFile = sProfile & "\Desktop\QB64.LNK" 'path to shortcut name Set oLink = oWS.CreateShortcut(sLinkFile) oLink.TargetPath = sPath & "\QB64.EXE" 'target path 'oLink.Arguments = " -c " & sfile 'command arguments to compile a file name oLink.Description = "QB64 Editor" 'comment title oLink.IconLocation = sPath & "\QB64icon.ICO, 0" 'icon in current folder oLink.WindowStyle = "1" oLink.WorkingDirectory = sPath 'working path oLink.Save

Note: Requires QB64icon.ico (Rt click to download) icon. The Arguments string can be used to compile a specified program.


(Return to top)

PIF Shortcut

Creates Shortcut PIF in current folder where QB.EXE (QB4.5) is the target to RUN a Qbasic BAS file using the QLB standard Library.

Set oWS = CreateObject("WScript.Shell") sPath = oWS.CurrentDirectory 'current path sLinkFile = sPath & "\Q-Basics.LNK" 'name of PIF file to create Set oLink = oWS.CreateShortcut(sLinkFile) oLink.TargetPath = sPath & "\QB.EXE" 'QB45 program path is current folder oLink.Arguments = "/L /RUN Q-BASICS.BAS" '<<< program to run without IDE oLink.Description = "Q-basics Demonstrator" 'program description oLink.IconLocation = sPath & "\QB-45.ICO, 0" 'program icon in same folder oLink.WindowStyle = "1" oLink.WorkingDirectory = sPath oLink.Save

Note: Requires QB-45.ICO (Rt click to download) icon. XP operating systems will change DOS program shortcuts to PIF files.


Qbasic BAS files can be run like compiled programs without returning to the IDE when SYSTEM is used to end them!
SHELL "QB.EXE /L /RUN program.BAS"


(Return to top)

Run Once

Script to only run one instance of a program. Displays "Program is Already Running!" message if program is currently in use.

On Error Resume Next strComputer = "." strMessage = "Program is Already Running!" ' message in popup box strProgramName = "qb64.exe" '<<<<<<< program EXE name only strPath = oWS.CurrentDirectory '<< place script in the program folder or enter "path" here strProgramPath = strPath & strProgramName Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objShell = CreateObject("WScript.Shell") Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = '" & strProgramName & "'") If colProcessList.Count > 0 Then MsgBox strMessage Else objShell.Run strProgramPath End If

Code courtesy of Scott Klassen at TechNet
Note: Save text in Notepad as QB64runonce.VBS and place it in the QB64 folder. Then compile the program below to see it work.


Compile the following program in the QB64 IDE and the message box should pop up because QB64.EXE is already running.

PRINT "Waiting four seconds to run QB64!" _DELAY 4 SHELL _HIDE "CSCRIPT //NOLOGO QB64runonce.vbs" SYSTEM


(Return to top)

Drive Letters

Windows Script to write all system drive letters(A and B normally denote floppy disk drives) to a text file as a letter string:

Set objFS = CreateObject("Scripting.FileSystemObject") Set objTF = objFS.CreateTextFile("DirList.txt") '<<< file name to receive Drive letters Set allDR = objFS.Drives For Each oneDR in allDR objTF.Write oneDR.DriveLetter Next objTF.Close

CDE


A program function that creates the script file, runs it, reads the data file, deletes the files and returns the drive letters available:

PRINT "Available drives on this computer: "; GetAvailDrives$ END '--------------------------------------------- '--- Get available drives on this computer --- '--------------------------------------------- FUNCTION GetAvailDrives$ () gadScriptID$ = "GADscript.tmp" gadTextID$ = "GADtext.tmp" gadFile% = FREEFILE OPEN "O", #gadFile%, gadScriptID$ 'open script file for OUTPUT creates empty file '--- a nice method to find all connected drives without making Windows --- '--- to ask for inserting a disk into any drives with removeable media --- PRINT #gadFile%, "Option Explicit" PRINT #gadFile%, "Dim objFS, objTF, allDR, oneDR" PRINT #gadFile%, "Set objFS = CreateObject(" + CHR$(34) + "Scripting.FileSystemObject" + CHR$(34) + ")" PRINT #gadFile%, "Set objTF = objFS.CreateTextFile(" + CHR$(34) + sysTempDir$ + gadTextID$ + CHR$(34) + ")" PRINT #gadFile%, "Set allDR = objFS.Drives" PRINT #gadFile%, "For Each oneDR in allDR" PRINT #gadFile%, " objTF.Write oneDR.DriveLetter" PRINT #gadFile%, "Next" PRINT #gadFile%, "objTF.Close" PRINT #gadFile%, "Set allDR = Nothing" PRINT #gadFile%, "Set objTF = Nothing" PRINT #gadFile%, "Set objFS = Nothing" CLOSE gadFile% SHELL _HIDE "cscript //NoLogo //E:VBS " + gadScriptID$ 'Engine option runs any file name extension OPEN "I", #gadFile%, gadTextID$ GetAvailDrives$ = INPUT$(LOF(gadFile%), gadFile%) CLOSE gadFile% KILL gadTextID$ KILL gadScriptID$ END FUNCTION

Code by RhoSigma
Note: Quoted script values and inserted STRING variables require CHR$(34) to be printed before and after them as PRINT # removes them. SHELL "cscript //E:vbscript text.tmp" or //E:VBS allows script files to be run with custom file name extensions to prevent double-click runs.


(Return to top)

References

See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Personal tools