UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> How To Get Files Path From It's Name In Vb.net 2008    
 
   
GlenKruger
post Apr 25 2012, 03:41 PM
Post #1

Utterly Crispy UA Forum Administrator
Posts: 7,100
From: Edmonton,Alberta,Canada



What I want to do is a recursive search through all drives and return first found path to a file by passing something it's file name.

Is there a built in function for this?
I know how to do it in vb6 but would take to much work to recode it.

Lazy, yes I am just thought maybe someone knew an easy relatively fast way of doing it using system I/O method.

I know I can use FSO to do it but it is rather slow doing this way.

Any suggestions appreciated.
Go to the top of the page
 
+
datAdrenaline
post Apr 25 2012, 04:06 PM
Post #2

UtterAccess Editor
Posts: 15,978
From: Northern Virginia, USA



You can use the GetDirectoryName method of the Path class of the System.IO namespace.

http://msdn.microsoft.com/en-us/library/sy...ectoryname.aspx
Go to the top of the page
 
+
GlenKruger
post Apr 25 2012, 05:43 PM
Post #3

Utterly Crispy UA Forum Administrator
Posts: 7,100
From: Edmonton,Alberta,Canada



Thanks Brent but not what I was looking for.
This works for what I need.

CODE
Imports System.IO
Public Class Form1
     'What you need to do is create a form with a list box, a buton, a label and a textbox
     'The label should read Eneter File Name Including Extension
     'The buttons text should be Get Path(s)
     ' The code runs when you press the button
     'If returning all paths be patient it can take awhile depending on how may drives you have

     'Declare constants and variables we need
     Private Declare Function GetDriveType Lib "kernel32" _
    Alias "GetDriveTypeA" _
   (ByVal nDrive As String) As Integer

     Private Const DRIVE_UNKNOWN As Long = 0
     Private Const DRIVE_NO_ROOT_DIR As Long = 1
     Private Const DRIVE_REMOVABLE As Long = 2
     Private Const DRIVE_FIXED As Long = 3
     Private Const DRIVE_REMOTE As Long = 4
     Private Const DRIVE_CDROM As Long = 5
     Private Const DRIVE_RAMDISK As Long = 6

     Public Sub AllFixedDriveSearch()
         If Me.TextBox1.Text = "" Then
             MsgBox("Enter Files Name Including Extension")
             Me.TextBox1.Focus()
             Exit Sub
         End If

         Me.Text = "Busy Searching All Fixed Hard Drives"

         Dim drvs() As String
         Dim list As New List(Of String)

         drvs = _
        System.IO.Directory.GetLogicalDrives()

         For i = drvs.GetLowerBound(0) To drvs.GetUpperBound(0)

             If GetDriveType(drvs(i)) = DRIVE_FIXED Then

                 ' Get recursive List of all files starting in this directory.
                 'list = System.Environment.GetFolderPath(drvs(i))
                 list = GetFilesRecursive(drvs(i))

                 ' Loop through and display each path.
                 For Each path In list
                     Me.ListBox1.Items.Add(path)
                     'Comment out next If Staement if you want to bring back all existing file paths
                     If ListBox1.Items.Count = 1 Then
                         Me.Refresh()
                         Me.Text = "Here is First Path"
                         Exit Sub
                     End If
                 Next
             End If
         Next i

         If list.Count < 1 Then

             Exit Sub
         End If
         Me.Refresh()
         Me.Text = "Here Are Path's"
     End Sub

     ''' <summary>
     ''' This method starts at the specified directory, and traverses all subdirectories.
     ''' It returns a List of those directories.
     ''' </summary>
     Public Function GetFilesRecursive(ByVal initial As String) As List(Of String)
         ' This list stores the results.
         Dim result As New List(Of String)

         ' This stack stores the directories to process.
         Dim stack As New Stack(Of String)

         ' Add the initial directory
         stack.Push(initial)

         ' Continue processing for each stacked directory
         Do While (stack.Count > 0)
             ' Get top directory string
             Dim dir As String = stack.Pop
             Try
                 ' Add all immediate file paths
                 result.AddRange(Directory.GetFiles(dir, "*" & TextBox1.Text & "*"))
                 'Comment out next If Staement if you want to bring back all existing file paths
                 If result.Count = 1 Then
                     Return result
                     Exit Function
                 End If

                 ' Loop through all subdirectories and add them to the stack.
                 Dim directoryName As String
                 For Each directoryName In Directory.GetDirectories(dir)
                     stack.Push(directoryName)
                 Next

             Catch ex As Exception
             End Try
         Loop

         ' Return the list
         Return result
     End Function

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         AllFixedDriveSearch()
     End Sub


     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         Me.Text = "Search For Path(s)"
     End Sub
End Class
Go to the top of the page
 
+
datAdrenaline
post Apr 25 2012, 08:55 PM
Post #4

UtterAccess Editor
Posts: 15,978
From: Northern Virginia, USA



Sorry Glen, I was focusing on "... return first found path to a file by passing something it's file name." So are you looking for a "better" way to do the whole task? --- ie: searching and listing?
Go to the top of the page
 
+
GlenKruger
post Apr 25 2012, 11:20 PM
Post #5

Utterly Crispy UA Forum Administrator
Posts: 7,100
From: Edmonton,Alberta,Canada



Actually I will hard code in file name with or without extension and need to get back first directory that file is located so that I can shell to it. This file can be placed anywhere on computer. I just created an app for testing purposes.
Go to the top of the page
 
+
GlenKruger
post Apr 25 2012, 11:22 PM
Post #6

Utterly Crispy UA Forum Administrator
Posts: 7,100
From: Edmonton,Alberta,Canada



Also this could be modified to detect duplicate files and their location.
Go to the top of the page
 
+
Swatz
post May 16 2012, 07:56 PM
Post #7

UtterAccess VIP
Posts: 1,280
From: Manitowoc, WI



Take a look at directory under namespace system.io

There is directory.getlogicaldrives (this brings back mapped drives along with physical drives, there may be a way to check if the drive is mapped or not)
There is also directory.getfiles

Something like this:

Dim drives As String()
Dim files As String()

drives = Directory.GetLogicalDrives()

Dim str As String
For Each str In drives
files = Directory.GetFiles(str, "*letter*", SearchOption.AllDirectories)
Next str

You need to be careful with security, it will bomb back error message if you do not have access to certain files or directories.
I testd it on my windows 7 machine and it threw back an error trying to search the recycle bin.

Something to play around with.

Frank


Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 25th May 2013 - 10:31 PM