Solved

check if file exists and read data if it does

Posted on 2014-02-04
1
201 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now