SearchOption problem...?

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1641 Views - Last Post: 15 April 2013 - 06:02 AM Rate Topic: -----

#1 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

SearchOption problem...?

Posted 12 April 2013 - 09:46 AM

I need to search for a specific type of file, example .mp3 in my user folder with this code:

Sub Backup()
        Dim sourceDir As String = "C:\Users\User1\"
        Dim destDir As String = "C:\Users\User1\MP3FOLDER\"
        Try
            If Not System.IO.Directory.Exists(destDir) Then
                System.IO.Directory.CreateDirectory(destDir)
            End If

            For Each strEntry As String In System.IO.Directory.GetFiles(sourceDir, "*.mp3", IO.SearchOption.AllDirectories)
                Dim fileNew As System.IO.FileInfo
                fileNew = New System.IO.FileInfo(strEntry)
                If fileNew.Exists Then
                    fileNew.CopyTo(destDir & "\" & fileNew.Name, True)
                End If

            Next
        Catch unath As UnauthorizedAccessException
        End Try
        Try

            For Each strEntry As String In System.IO.Directory.GetFiles(sourceDir, "*.mp3", IO.SearchOption.AllDirectories)
                Dim strDest As String = System.IO.Path.Combine(destDir, System.IO.Path.GetFileName(strEntry))
                FileCopy(strEntry, strDest)
            Next
        Catch unatho As UnauthorizedAccessException
        Catch ex As Exception

            MsgBox(ex.ToString())
        End Try
    End Sub


The problem is in Appdata/Local folder... witch throws me an unauthorized exception, even if i put catch as above stated, the application doesn't seem to run further... is any option to skip that folder/'s or something?

Thanks.

This post has been edited by HeBTeMy: 12 April 2013 - 09:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SearchOption problem...?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,885
  • Joined: 12-December 12

Re: SearchOption problem...?

Posted 12 April 2013 - 09:57 AM

Place your Try..Catch block within the For..Each loop; this way, it can skip unauthorized directories, but continue looping.
Was This Post Helpful? 0
  • +
  • -

#3 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: SearchOption problem...?

Posted 12 April 2013 - 03:41 PM

View Postandrewsw, on 12 April 2013 - 09:57 AM, said:

Place your Try..Catch block within the For..Each loop; this way, it can skip unauthorized directories, but continue looping.


Thanks, that worked, i had to modify some things in the code to make it as i want it to do.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,885
  • Joined: 12-December 12

Re: SearchOption problem...?

Posted 12 April 2013 - 03:45 PM

View PostHeBTeMy, on 12 April 2013 - 10:41 PM, said:

View Postandrewsw, on 12 April 2013 - 09:57 AM, said:

Place your Try..Catch block within the For..Each loop; this way, it can skip unauthorized directories, but continue looping.


Thanks, that worked, i had to modify some things in the code to make it as i want it to do.

Thanks.

No worries. It might be helpful to others who come across this thread if you post your working code.
Was This Post Helpful? 0
  • +
  • -

#5 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: SearchOption problem...?

Posted 12 April 2013 - 04:30 PM

Dim foldername As String = Path.GetFileName("sourceDir path") ' I use this to get the name of the "root" folder being copied to put everything organized.
 Private Sub CopyDirectory(ByVal sourceDir As String, ByVal destDir As String)
        Try
            If Not Directory.Exists(destDir) Then
                Directory.CreateDirectory(destDir)
            End If

            For Each strEntry As String In Directory.GetFiles(sourceDir)
                Dim fileNew As FileInfo
                fileNew = New FileInfo(strEntry)
                If fileNew.Exists Then
                    fileNew.CopyTo(destDir & "\" & fileNew.Name, True)
                End If
            Next

            For Each strEntry As String In Directory.GetDirectories(sourceDir)
                Dim strDest As String = Path.Combine(destDir, Path.GetFileName(strEntry))
                CopyDirectory(strEntry, strDest)
            Next
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim t = New Thread(Sub() Me.CopyDirectory("Put sourceDir path", "Put destDir path" & foldername))
        t.Start()
    End Sub
End Class



Here it is, thanks for the help one more time.
Just one more question about my OP post, is there a way to skip folders in a "AllDirectories" or the example above search ?
I want to backup all but the Appdata folder and stuff...

This post has been edited by HeBTeMy: 12 April 2013 - 04:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,885
  • Joined: 12-December 12

Re: SearchOption problem...?

Posted 12 April 2013 - 05:05 PM

There might be other ways to skip folders, but I would look into using GetDirectories, loop through these using an if statement to exclude the folder (or folders), then nest a loop within this using GetFiles.

This post has been edited by andrewsw: 12 April 2013 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: SearchOption problem...?

Posted 12 April 2013 - 05:09 PM

Hi,
You could change the loop that copies the directories to look for the folder (appdata) and have it skip it like this. I used the (ToLower) and filled in the folder name in lowercase. Its just to make it so you don`t have to worry about the case of the name.
            For Each strEntry As String In Directory.GetDirectories(sourceDir)
                If Not strEntry.ToLower.Contains("appdata") Then
                    Dim strDest As String = Path.Combine(destDir, Path.GetFileName(strEntry))
                    CopyDirectory(strEntry, strDest)
                End If
            Next


This post has been edited by IronRazer: 12 April 2013 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,885
  • Joined: 12-December 12

Re: SearchOption problem...?

Posted 12 April 2013 - 05:32 PM

There is also LINQ:

            Dim dirsExclude = Directory.GetDirectories("C:\Users\User1\") _
                              .Where(Function(x) Not x.ToLower.Contains("appdata"))

(although I haven't tested this)

There is no advantage to this method over IronRazer's solution :)

Actually, searching all sub-directories probably requires this:

            Dim dirsExclude = Directory.GetDirectories("C:\Users\User1\", "*", SearchOption.AllDirectories) _
                              .Where(Function(x) Not x.ToLower.Contains("appdata"))

This post has been edited by andrewsw: 12 April 2013 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: SearchOption problem...?

Posted 12 April 2013 - 05:49 PM

Doing that excludes potentially valid directories and filenames
Eg \frappdatabase
Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: SearchOption problem...?

Posted 12 April 2013 - 06:04 PM

View PostAdamSpeight2008, on 12 April 2013 - 07:49 PM, said:

Doing that excludes potentially valid directories and filenames
Eg \frappdatabase

Yes that is true. I guess you could break it down to the last directory and then see if that equals the directory that you do not want to copy like this
            For Each strEntry As String In Directory.GetDirectories(sourceDir)
                If Not strEntry.ToLower.Substring(strEntry.LastIndexOf("\") + 1) = "appdata" Then
                    Dim strDest As String = Path.Combine(destDir, Path.GetFileName(strEntry))
                    CopyDirectory(strEntry, strDest)
                End If
            Next


This post has been edited by IronRazer: 12 April 2013 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#11 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: SearchOption problem...?

Posted 12 April 2013 - 06:14 PM

Yeah, although that probably would work for me, i see your point.

Private Sub CopyDirectory(ByVal sourceDir As String, ByVal destDir As String)

        If Not Directory.Exists(destDir) Then
            Directory.CreateDirectory(destDir)
        End If
        Try
            For Each strEntry As String In Directory.GetFiles(sourceDir, "*.png", SearchOption.AllDirectories)
                If Not strEntry.ToLower.Contains("appdata") Then
                    Dim fileNew As FileInfo
                    fileNew = New FileInfo(strEntry)
                    If fileNew.Exists Then
                        fileNew.CopyTo(destDir & "\" & fileNew.Name, True)
                    End If
                End If
            Next
        Catch ex2 As IOException
        Catch ex1 As UnauthorizedAccessException
        Catch ex As Exception

        End Try
        Try
            For Each strEntry As String In Directory.GetDirectories(sourceDir, "*.png", SearchOption.AllDirectories)
                If Not strEntry.ToLower.Contains("appdata") Then
                    Dim strDest As String = Path.Combine(destDir, Path.GetFileName(strEntry))
                    CopyDirectory(strEntry, strDest)
                End If
            Next
        Catch ex2 As IOException
        Catch ex1 As UnauthorizedAccessException
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try



Can anyone explain me how after i put the "*.png" and searchoption why the whole sub "stops" working? Like nothing happens? Am i doing something wrong?
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: SearchOption problem...?

Posted 12 April 2013 - 06:16 PM

String manipulation on file paths is a bad idea. The directory separator \ is a chosen by the FileSystem.

Path.Combine is recommended for joining paths.

You could use a method like the one shown in the Finding Files tutorial

The cause of the original issue is the user doesn't have the access rights to the directory.

This post has been edited by AdamSpeight2008: 12 April 2013 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

#13 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: SearchOption problem...?

Posted 12 April 2013 - 06:17 PM

You could also use (EndsWith) to check the last directory by changing it to this
                If Not strEntry.ToLower.EndsWith("\appdata") Then


Was This Post Helpful? 0
  • +
  • -

#14 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: SearchOption problem...?

Posted 12 April 2013 - 06:39 PM

View PostAdamSpeight2008, on 12 April 2013 - 06:16 PM, said:

String manipulation on file paths is a bad idea. The directory separator \ is a chosen by the FileSystem.

Path.Combine is recommended for joining paths.

You could use a method like the one shown in the Finding Files tutorial

The cause of the original issue is the user doesn't have the access rights to the directory.


But i can copy all the folders with the Catch exceptions above, i just can't search and copy all specific type of files and dk why...

Edit: I tested on other folder... it works, it just doesn't work on my user root folder, is any workaround ?
I tested it right now withou catch statement and it says i don't have permission to access appdata/something folder when i have
If Not strEntry.ToLower.EndsWith("appdata") Then


w00t?

This post has been edited by HeBTeMy: 12 April 2013 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

#15 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: SearchOption problem...?

Posted 13 April 2013 - 09:07 AM

Ok, i ajusted more the code to my needs and realize the problem is in the userfolder that cannot be copied, i can't start a file type search on them, i can copy it all, but with specific file search it doesn't work, gonna search more to see what's the problem.. as i also tried with admin priviledges.

If anyone knows why i'd like to know, thanks.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2