Solved

check if file exists and read data if it does

Posted on 2014-02-04
1
208 Views
Last Modified: 2014-02-05
Hi,

I have a USB Drive with a file called xcel.axe on it, how can I get excel to scan all EXTERNAL drives, i.e. for USB thumbdrives hunting for this file, and if found load its contents into a variable called password, the .axe file is just a txt file with one line.

the file is stored in the root of my usb
0
Comment
Question by:DemonForce
1 Comment
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 39834593
i would be using the FileSystemObject fso

fso has a drives collection. I assume all 'external' drives are removable...

Sub Test1()
    MsgBox "AXE text=" & GetAxeText

End Sub

Function GetAxeText() As String
    Const dtRemovable = 1
    
    Dim dfldr As Object
    Dim filenameReqd As String
    GetAxeText = ""   'set as blank
    filenameReqd = "xcel.axe"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set dc = fso.Drives
    For Each d In dc
        Select Case d.DriveType
           Case 0: ' "Unknown"
           Case 1: ' "Removable"
              'this is a candiate drive
              Set dfldr = d.RootFolder
              flname = dfldr & filenameReqd
              If fso.FileExists(flname) Then
                   Set ts = fso.OpenTextFile(flname, 1)
                   txtLine = ts.ReadLine
                   ts.Close
                   GetAxeText = txtLine
                   Exit Function
              End If
           Case 2: ' "Fixed"
           Case 3: ' "Network"
           Case 4: ' "CD-ROM"
           Case 5: ' "RAM Disk"
        End Select
    Next
End Function

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question