how to check the last 3 characters of the registry entry using a vbscript - vbscript

We are planning to distribute the device drivers according to the model of the machine, through SCCM. Device drivers are placed in the SCCM share.Device driver folders are named in such a way that only model number will be there.eg E6410. So we need a script to check the last 3 characters of the registry [HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\BIOS\SystemProductName.] eg:Dell Latitude E6410 ;Last 3 characters =410. so that it will compare the share and if match found then download the corresponding device driver folder to the local machine.

This should achieve the result you are looking for.
Option Explicit
' Open the WScript.Shell object to read the registry key.
Dim objWS, strKeyValue, strKeySuffix
Set objWS = CreateObject("WScript.Shell")
strKeyValue = objWS.RegRead("HKLM\HARDWARE\DESCRIPTION\System\BIOS\SystemProductName")
' Get last three characters of the key value.
strKeySuffix = Right(strKeyValue, 3)

How about getting the registry entry (as string) and get the last 3 chars by creating a substring with starting position string.length - 3 and length 3?
I'm not well versed in vbscript but it should be easy to achieve the same result as in C#:
string key = "your registry key";
string substr = key.Substring(key.Length - 3, 3);

Related

Looping over all databases in a directory

I need to loop over all databases in a certain directory, where the database name is ACPwxyz.mdb, where wxyz is the equivalent to an MMYY value for the period that database was used for.
E.g., the database for July 2017 would be ACP0717.mdb.
I've never written in VB6 before and I totally hate it, but it's an extension to an existing project so I'm stuck with it!
Is there a way of looping over all files in a directory, checking if the file name follows the format of ACPwxyz.mdb or not, and if it does, then opening a connection to it?
I've looked around a bit and see Dir(x, y), but I'm not sure if I can use this in this situation?
Any tips would be appreciated.
You can use Dir, yes.
If you use something like this:
Dim strFile As String
strFile = Dir(yourDBPath, "ACP????.mdb") ' mdb for MS-Access files
Do Until strFile = ""
If Len(strFile) = 11 Then ' Ensure the DB file name is 11 characters, which yours are
'Do something // You can also check the file name doesn't = a certain name if needed
End If
strFile = Dir
Loop
Dir accepts either an asterisk (*), or a question mark (?) as wildcards in file names, so this will look for any database in the set path that is called ACP followed by 4 characters.

How to create a Printer object in VB

I am facing an issue in VB 6 while creating a Printer Object.
Basically, I need to create a printer object so that I can set the correct tray on which printing needs to be performed.
I have the Printer Name with me.
All the code that I am able to find online involves looping through all the available printers and finding a match with our printer name.
Is there a way I can create the Printer object prn directly from the Printer name.
Any help would be appreciated.
You can't. The VB6 Printers collection is accessed only by index, not by name. See Visual Studio 6 Printer Object, Printers Collection.
So you have to search the collection for the printer you want. For instance:
Private Function FindPrinter(PrinterName As String) As Printer
Dim i As Integer
For i = 0 To Printers.Count - 1
If Printers(i).DeviceName = PrinterName Then
Set FindPrinter = Printers(i)
Exit For
End If
Next i
Exit Function
End Function
The above doesn't handle the situation where the printer you're looking for isn't in the collection. You'll want to add logic to cover that condition - what you'll want to do is specific to your particular tasks and requirements. This example is also a case-sensitive name search, so keep that in mind, too.

Comparing files in two different folders with string variable names

Due to automatic naming conventions I'm stuck with, files are saved in a particular archive one of two ways. Either they have a 13-digit match to the record in the database, or they have a 10 digit match. The ones which have the 13 digit also have the ten digit match, and should be treated preferentially. I have two scripts right now that work: one just looks for 13-digit matches and moves them to another folder. The other moves by datemodified, which is not a very stable variable.
My attempts at combining look something like this:
'dimming of obj, folder paths, sql db info and connection open above
SQL = "SELECT ticket FROM orders WHERE dateinvoice>'6/1/16"
Recordset.Open SQL, Connection
Do While Not Recordset.EOF
ticket = Recordset("ticket")
id = Right(ticket, 2)
suffix = CInt(id)
compare = Left(ticket, Len(ticket) - 3)
search = compare & "-0" & suffix
For Each objFile In colFiles.Files
If Left(objFile.Name, 13) = search Then
Set objNewestFile = objFile
skip = 1
Else
skip = 0
End If
If Left(objFile.Name, 10) = search And skip = 0 Then
Set objNewestFile = objFile
End If
Next
Recordset.MoveNext
On Error Resume Next
objFSO.CopyFile objNewestFile.Path, strDestFolder
Set objFile = Nothing
Loop
This is the scaled-down version I went back to, having attempted various convoluted solutions like:
storing the skipped records in an array,
having a second loop through comparing files in folder1 to folder2 (every time I use FileExists it doesn't seem to recognize the destination),
creating a temporary SQL table to store the skipped records in.
The following have been very useful:
Compare two files in two different folders and replace it by the newer
https://blogs.technet.microsoft.com/heyscriptingguy/2005/06/20/how-can-i-determine-if-a-file-exists-and-if-it-does-exit-the-script/
Classic ASP 3.0 Create Array from a Recordset
ETA: There can be many duplicate files that share the 10 digit identifier. The only good rule is: if it has the 13 identifier version, that's the one to move. Part of my issue lies in setting paths if I use FileExists-- when I try wildcards, it doesn't recognize the path. If I don't use them, it doesn't compensate for all the variables.

How do i get drive letter for a running form

I need to get the letter of the drive my form is running from.
The reason why i need this, is because i'll be copying a text file (from the removable drive in which the form will be) to the computer. Is it even possible? If yes could someone help me with the code?
NOTE I'm using Visual Basic 6
Dim disk_letter As String
disk_letter = Left(CurDir$(), 1)
That was all.
I think this is the best approach considering the drive letter can have more than one letter.
Dim driveLetter As String
driveLetter = Left$(App.path, InStr(App.path, ":") - 1)
This should return you the drive letter that you are running the exe from.
Left$(App.Path, InStr(App.Path, ":"))

trim strim in visual basic 6.0

I am working in visual basic 6.0 and attempting to retreive the server of a Queue. So far the only way I have found to get this is from the path name (oQueue.QueueInfo.PathName) (which gives me: vdi***\testQueue02)
I would like to trim this down to just vdi***\
But I can not do it by number of characters because I have hundreds of queues all with different lenghts and also the servers may have different lenghts.
Is there any way to do this??
Thanks,
Is it always the "first part" of the path name you are trying to retrieve?
`Dim newArray() as String = Split(oQueue.QueueInfo.PathName,"\")
Dim path = newArray(0) & "\"`

Resources