How do i get drive letter for a running form - vb6

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, ":"))

Related

Vbscript that scans for a certain name of a drive, instead of drive letters and copies files to that usb drive name

I am trying to figure out how to make a vbscript that will scan for a USB device with a specific name and then once located copy a file to it.
Example instead of scanning for drive letter J, scan for a specific name corresponding to the specific USB device I want to copy to.
Lets say my USB device I want to locate is named TEAM, I want the script to scan for the name TEAM instead of a drive letter and copy files to it. I want it this way because drive letters can change one time my USB device named TEAM might be letter J next it might be E.
This way it doesn't have to worry about if the drive letter changes and will always be able to get to my device unless of course I change the USB device's name.
I am new to programming and I've made a script that will scan for a letter but I'd like to change it so it uses the USB name instead of drive letter.
Thanks help on this would be greatly appreciated.
Ask WMI for the required information
Option Explicit
Function getDriveLetterFromVolumeName( volumeName )
Dim volumes, volume
' Unless we found a matching volume, an empty string will be the returned value
getDriveLetterFromVolumeName=""
' Ask WMI for the list of volumes with the requested label
Set volumes = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") _
.ExecQuery("SELECT DriveLetter FROM Win32_Volume WHERE Label='" & volumeName & "'")
' If exist an matching volume, get its drive letter
If volumes.Count > 0 Then
For Each volume In volumes
getDriveLetterFromVolumeName = volume.DriveLetter
Exit For
Next
End If
End Function
WScript.Echo getDriveLetterFromVolumeName( "TEAM" )
edited to adapt to comments
So, once we are able to determine if it is available and what the drive letter is, the only remaining task is copy a source file to the target drive
Dim fileToCopy
fileToCopy = "c:\source\folder\myfile.txt"
Dim usbDrive
usbDrive = getDriveLetterFromVolumeName( "TEAM" )
If Not (usbDrive="") Then
With WScript.CreateObject("Scripting.FileSystemObject")
If .FileExists(fileToCopy) Then
.CopyFile fileToCopy, usbDrive & "\targetFolder\myFile.txt"
End If
End With
End If

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) & "\"`

How to reach and show a picture which is inside a Seagate Gigabit Ethernet BlackArmor Disk with vb6

Actually i have no idea how this kinda disks works. Its the first time i deal with such disk. I putted some pictures in a folder named as oldpics and with a vb code i wanna reach it but some how i just cant find the picture inside it and its imposible cause i know that the picture is in that folder.
'This A_PicRoad is = "\\198.162.1.20\oldpics\" which is exist for sure
If mdiMain.fsoexist.FileExists(mdiMain.A_PicRoad & stock & ".jpg") Then
pic1.Picture = LoadPicture(mdiMain.A_PicRoad & stock & ".jpg")
Set img = New ImageFile
img.LoadFile mdiMain.A_PicRoad & stok & ".jpg"
End If
'But somehow cant find the picture and leaving this if statement without processing anything..
so what could be causing that?
Edit: I just remember that at first i must enter a username and pass while reaching \\198.162.1.20\ after that i can reach \\198.162.1.20\oldpics\..
If it's a normal SMB network share, then you must use a full valued UNC path (Note the preceding \\)
\\198.162.1.20\oldpics\
You can also map a drive and access it via a drive letter.

how to check the last 3 characters of the registry entry using a 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);

Search filesystem for filepath using VBA

I am looking for a way to search a specific folder for a subfolder containing a certain string. Below I have listed a function I typed off the top of my head to see if it would work. Well, it does work but when I am talking about searching through 6,000 folders on a network drive it just isn't fast enough.
I'm sure that there is a better way to do it but I can't seem to dig anything up on Google.
Is there an object that allows me to leverage the windows built in file system searching and indexing capabilities?
As an alternative, does someone have a way to optimize my code? The main bottleneck is the usage of instr.
Here is the code:
Function findPath(strId As String) As String
checkObj
Dim strBase As String
strBase = opt.photoBasePath
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dim baseFolder As Object
Set baseFolder = fs.getfolder(strBase)
Dim folder As Object
For Each folder In baseFolder.subfolders
If InStr(1, folder.name, strId) > 0 Then
findPath = strBase & "\" & folder.name
Exit Function
End If
Next folder
End Function
P.S. I'm sure someone will suggest modifying my folder structure so that I can programmatically predict the path but for various reason that isn't possible in my case.
You could use the FindFirstFile Win32 API, which allows you to search for files or sudirectories matching a specified name. Additionally, you could also use the FindFirstFileEx function, along with a FINDEX_SEARCH_OPS parameter of FindExSearchLimitToDirectories, which would limit your search to a file that matches a specified name and is also a directory (if the file system supports directory filtering). For more information on using these functions from VB/VBA see the following:
http://www.xtremevbtalk.com/showpost.php?p=1157418&postcount=4
http://support.microsoft.com/kb/185476
http://www.ask-4it.com/how-to-use-findfirstfile-win32-api-from-visual-basic-code-2-ca.html
Consider splitting traversing the folders from finding your key. Instead of the instr test, store the folder names in a table, then use a query on the table to find your target. It might be slower, but searching should be faster.

Resources