Results 1 to 7 of 7

Thread: Search Question.

  1. #1

    Thread Starter
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Unhappy

    Here are two question that I reaaaaaaaly need answers to.

    1. How do you search if a file exists or not, and what variables are returned?

    2. How (Can, even.) do you search the registry for the values of keys or strings?

  2. #2
    Guest
    Q1:

    Code:
    Private Sub Command1_Click()
    If Dir("C:\program.exe") <> "" Then
    Msgbox "file exists"
    Else
    Msgbox "file doesn't exist"
    End If
    End Sub

  3. #3
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    There is also an undocumented API, #45:

    Code:
    Private Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
    Private Sub Form_Load()
      MsgBox "Does the file exist?" + Str$(SHFileExists("c:\config.sys"))
    End Sub

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    or use fso
    
    'does file exist using fso
    
    Dim sFile$
    
    sFile = "Name And Path Of Your File"
    
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    If fs.fileexists(sFile$) = True Then
    	MsgBox "File Exists...Your code here!"
    Else
    	MsgBox "Does Not Exist...Your code here!"
    End If
    	Set fs = Nothing
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5

    Thread Starter
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425
    Thanks for all the answers to Q1, anyone know about Q2????

  6. #6
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553

    Lightbulb

    Yes, this is an extract from some API found in KPD's API guide (http://kpdteam.tripod.com/agnet/aandown.htm)

    Code:
    Const HKEY_CURRENT_CONFIG = &H80000005
    Const HKEY_LOCAL_MACHINE = &H80000002
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
    Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
    Private Sub Form_Paint()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim hKey As Long, Cnt As Long, sSave As String
        'Clear the form
        Me.Cls
        Me.Print "RegEnumKeyEx"
        'Open a registry key
        RegOpenKey HKEY_LOCAL_MACHINE, "Enum", hKey
        Do
            'Create a buffer
            sSave = String(255, 0)
            'Enumerate the keys
            If RegEnumKeyEx(hKey, Cnt, sSave, 255, 0, vbNullString, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
            'Print the result to the form
            Me.Print StripTerminator(sSave)
            Cnt = Cnt + 1
        Loop
        'Close the registry key
        RegCloseKey hKey
        Me.Print vbCrLf + "RegEnumValue:"
        'Open a new key
        RegOpenKey HKEY_CURRENT_CONFIG, "Display\Fonts", hKey
        Cnt = 0
        Do
            'Create a buffer
            sSave = String(255, 0)
            'enumerate the values
            If RegEnumValue(hKey, Cnt, sSave, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
            'pritn the results to the form
            Me.Print StripTerminator(sSave)
            Cnt = Cnt + 1
        Loop
        'Close the registry
        RegCloseKey hKey
    End Sub
    'This function is used to stripoff all the unnecessary chr$(0)'s
    Private Function StripTerminator(sInput As String) As String
        Dim ZeroPos As Integer
        'Search the first chr$(0)
        ZeroPos = InStr(1, sInput, vbNullChar)
        If ZeroPos > 0 Then
            StripTerminator = Left$(sInput, ZeroPos - 1)
        Else
            StripTerminator = sInput
        End If
    End Function

  7. #7
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    There are some good tutorials with code examples right here
    on VB World: http://www.vb-world.com/registry/
    Donald Sy - VB (ab)user

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