Results 1 to 18 of 18

Thread: Copying Files from Documents folder and how to skip hidden folder or delete ones???

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Copying Files from Documents folder and how to skip hidden folder or delete ones???

    I am creating a program to look at the documents folder and copy the contents of that folder. I am getting folders that dont exist, that are hidden and have nothing in them. I had to put a Try/catch to make the program run.
    Name:  10-5-2015 11-35-47 AM.jpg
Views: 1154
Size:  40.7 KB
    As you can see from this picture.. My Music and My Videos isnt in the Documents Folder.
    I have been trying to write code to not look at hidden files or exclude hidden files. no luck
    I could write code to delete empty folders when i am done.
    Code:
     Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String)
    
            Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
    
            ' If the destination folder don't exist then create it
            If Not System.IO.Directory.Exists(destinationPath) Then
                System.IO.Directory.CreateDirectory(destinationPath)
            End If
            Dim fileSystemInfo As System.IO.FileSystemInfo
            For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
                Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
    
                ' Now check whether its a file or a folder and take action accordingly
                If TypeOf fileSystemInfo Is System.IO.FileInfo Then
                    System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
                Else
                    ' Recursively call the mothod to copy all the neste folders
                    CopyFavoritesFolder(fileSystemInfo.FullName, destinationFileName)
                    End If
            Next
    This is the code i am using. I tried to Modify it to help me but it doesnt work.. Please help

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    You can get the directory's attributes by getting the IO.DirectoryInfo.Attributes property. From there you can check if it is hidden. As far as the directory not having any contents, then you'd need to check if the GetFiles and GetDirectories method return no items.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    bump... please any help?? Also can i install this to a flash drive so the program will be portable?

    Code:
      Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String)
    
            Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
    
            ' If the destination folder don't exist then create it
            If Not System.IO.Directory.Exists(destinationPath) Then
                System.IO.Directory.CreateDirectory(destinationPath)
            End If
            Dim fileSystemInfo As System.IO.FileSystemInfo
            Try
                For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
                    If File.GetAttributes(sourceDirectoryInfo.ToString) <> FileAttributes.Normal Then
    
                        Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
                        ' Now check whether its a file or a folder and take action accordingly
                        If TypeOf fileSystemInfo Is System.IO.FileInfo Then
    
    
                            System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
                        Else
                            ' Recursively call the mothod to copy all the neste folders
                            CopyFavoritesFolder(fileSystemInfo.FullName, destinationFileName)
                        End If
                    Else
                        Continue For
                    End If
    
                Next
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    This what i came up with but it isnt doing anything different.
    Last edited by rzubulake; Oct 6th, 2015 at 08:48 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Code:
     Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String)
    
            Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
    
            ' If the destination folder don't exist then create it
            If Not System.IO.Directory.Exists(destinationPath) Then
                System.IO.Directory.CreateDirectory(destinationPath)
            End If
            Dim fileSystemInfo As System.IO.FileSystemInfo
            Try
                For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
                    If sourceDirectoryInfo.Attributes <> FileAttributes.Hidden Or FileAttributes.ReadOnly Then
    
    
                        Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
                        ' Now check whether its a file or a folder and take action accordingly
                        If TypeOf fileSystemInfo Is System.IO.FileInfo Then
    
                            System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
                        Else
                            ' Recursively call the mothod to copy all the neste folders
                            CopyFavoritesFolder(fileSystemInfo.FullName, destinationFileName)
                        End If
                    Else
                        Continue For
                    End If
    
                Next
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    Thought i had it.. But no :-(

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    With a quick glance at your last code... this line is not correct:
    Code:
    If sourceDirectoryInfo.Attributes <> FileAttributes.Hidden Or FileAttributes.ReadOnly Then
    It should be more like this:
    Code:
    If sourceDirectoryInfo.Attributes <> FileAttributes.Hidden OrElse sourceDirectoryInfo.Attributes <>  FileAttributes.ReadOnly Then
    You do not need to check if the second condition is true so that is why you want to use OrElse and you also left out to check in the second condition the sourceDirectoryInfo.Attributes is not part.

    There is also no need for the Else where you simply call Continue For and you should always utilize the exception if you're using a Try/Catch.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Quote Originally Posted by dday9 View Post
    With a quick glance at your last code... this line is not correct:
    Code:
    If sourceDirectoryInfo.Attributes <> FileAttributes.Hidden Or FileAttributes.ReadOnly Then
    It should be more like this:
    Code:
    If sourceDirectoryInfo.Attributes <> FileAttributes.Hidden OrElse sourceDirectoryInfo.Attributes <>  FileAttributes.ReadOnly Then
    You do not need to check if the second condition is true so that is why you want to use OrElse and you also left out to check in the second condition the sourceDirectoryInfo.Attributes is not part.

    There is also no need for the Else where you simply call Continue For and you should always utilize the exception if you're using a Try/Catch.
    Still this is the outcome.. I set My Maps and My Received Files to Hidden. It still copied them. I also set Stereo Stuff and Wellmark ID number to Read Only and it still copied that. The copied folder ends up with more items than the original. So something is wrong and I dont know what to do. All I am trying to do is when a employee moves to a new computer i want to copy all his old stuff in his personal files, Desktop, Documents, etc to the new computer. Doesnt seem that hard.
    Name:  10-6-2015 11-09-17 AM.jpg
Views: 901
Size:  16.1 KB

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Quote Originally Posted by rzubulake View Post
    Still this is the outcome.. ...
    Did you check the attributes when stepping through the program? Are they what you expect? Are you looking at the correct object?!

    The program is doing exactly what you are telling it to do, so you have a mistake which should be easily resolved by stepping through - specifically, the line where you check the attributes: what are you checking the attributes of?
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Quote Originally Posted by SJWhiteley View Post
    Did you check the attributes when stepping through the program? Are they what you expect? Are you looking at the correct object?!

    The program is doing exactly what you are telling it to do, so you have a mistake which should be easily resolved by stepping through - specifically, the line where you check the attributes: what are you checking the attributes of?
    Like i said i am new to this. You have the code that I used. I dunno what those folders are copied over. Am i not following you? :-(

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Ok so i followed the code here is what it does.
    1.Name:  10-6-2015 2-21-45 PM.jpg
Views: 782
Size:  24.7 KBIT sees that it is an Hidden or System file.
    2.Exception thrown and exits out of try.catch
    3.Name:  10-6-2015 2-22-20 PM.jpg
Views: 817
Size:  23.0 KB Then it goes here and copies the folder
    4.Name:  10-6-2015 2-23-36 PM.jpg
Views: 807
Size:  23.0 KB Then goes here..

    So If someone can make sense of this that would be amazing.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    When i catch the message in a message box.Name:  10-6-2015 4-14-23 PM.png
Views: 846
Size:  3.9 KB

    here is link to another posting but no answer..
    http://www.vbforums.com/showthread.p...other-location
    Last edited by rzubulake; Oct 6th, 2015 at 04:24 PM.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    If you're getting an access denied error then you do not have the credentials to access the folder. Run as administrator.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Im testing the code by copying the code from my computer to a different folder.
    I access the folder like i would \\WS0435 <-- THIS IS MY COMPUTER the source.. than i am copying to the destination machine. myself again..

    How to i run as an admin then?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    I got the admin rights.. I try to goto the location on the local computer i get this message.. Name:  10-6-2015 4-55-04 PM.png
Views: 806
Size:  5.8 KB
    Try it yourself.. goto documents folder and type "\my music" Its a hidden not real directory. My program is saying i dont have access and than copying the folder anyways.. this is so annoying.

  14. #14
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    The My Music folder isn't actually a folder. It's a junction point, is there for backwards compatibility with older OS's, and links to the actual Music folder.

    I believe it has File Attributes of Hidden, System and ReparsePoint, so you should test for those attributes and ignore the "folder" if they are all present.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Quote Originally Posted by Inferrd View Post
    The My Music folder isn't actually a folder. It's a junction point, is there for backwards compatibility with older OS's, and links to the actual Music folder.

    I believe it has File Attributes of Hidden, System and ReparsePoint, so you should test for those attributes and ignore the "folder" if they are all present.
    Isn't that what I was doing? I searched far and wide how to ignore the folder. It's probably something simple but I can't figure it out. I have spent way to much time on this.

  16. #16
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Quote Originally Posted by rzubulake View Post
    Isn't that what I was doing? I searched far and wide how to ignore the folder. It's probably something simple but I can't figure it out. I have spent way to much time on this.
    Not in the code from Post#4. Don't know what your code currently looks like, though.

  17. #17
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    I am way too tired to look into this properly, but the following modifications to your code should get you started:
    VB.NET Code:
    1. Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String)
    2.  
    3.     Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
    4.  
    5.     If (sourceDirectoryInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse
    6.        (sourceDirectoryInfo.Attributes And FileAttributes.System) = FileAttributes.System OrElse
    7.        (sourceDirectoryInfo.Attributes And FileAttributes.ReparsePoint) = FileAttributes.ReparsePoint Then
    8.         Exit Sub
    9.  
    10.     Else
    11.         ' If the destination folder don't exist then create it
    12.         If Not System.IO.Directory.Exists(destinationPath) Then
    13.             System.IO.Directory.CreateDirectory(destinationPath)
    14.         End If
    15.  
    16.         Dim fileSystemInfo As System.IO.FileSystemInfo
    17.         Try
    18.             For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
    19.                 If (fileSystemInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse
    20.                    (fileSystemInfo.Attributes And FileAttributes.System) = FileAttributes.System Then
    21.                     Continue For
    22.  
    23.                 Else
    24.                     Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
    25.                     ' Now check whether its a file or a folder and take action accordingly
    26.                     If TypeOf fileSystemInfo Is System.IO.FileInfo Then
    27.                         System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
    28.                     Else
    29.                         ' Recursively call the mothod to copy all the neste folders
    30.                         CopyFavoritesFolder(fileSystemInfo.FullName, destinationFileName)
    31.                     End If
    32.  
    33.                 End If
    34.             Next
    35.  
    36.         Catch ex As Exception
    37.             MsgBox(ex.Message)
    38.         End Try
    39.  
    40.     End If
    41.  
    42. End Sub
    I tested it (quickly), and it does ignore hidden files and junction points. You may want to change the logic around a bit

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Posts
    25

    Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?

    Quote Originally Posted by Inferrd View Post
    I am way too tired to look into this properly, but the following modifications to your code should get you started:
    VB.NET Code:
    1. Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String)
    2.  
    3.     Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
    4.  
    5.     If (sourceDirectoryInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse
    6.        (sourceDirectoryInfo.Attributes And FileAttributes.System) = FileAttributes.System OrElse
    7.        (sourceDirectoryInfo.Attributes And FileAttributes.ReparsePoint) = FileAttributes.ReparsePoint Then
    8.         Exit Sub
    9.  
    10.     Else
    11.         ' If the destination folder don't exist then create it
    12.         If Not System.IO.Directory.Exists(destinationPath) Then
    13.             System.IO.Directory.CreateDirectory(destinationPath)
    14.         End If
    15.  
    16.         Dim fileSystemInfo As System.IO.FileSystemInfo
    17.         Try
    18.             For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
    19.                 If (fileSystemInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse
    20.                    (fileSystemInfo.Attributes And FileAttributes.System) = FileAttributes.System Then
    21.                     Continue For
    22.  
    23.                 Else
    24.                     Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)
    25.                     ' Now check whether its a file or a folder and take action accordingly
    26.                     If TypeOf fileSystemInfo Is System.IO.FileInfo Then
    27.                         System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
    28.                     Else
    29.                         ' Recursively call the mothod to copy all the neste folders
    30.                         CopyFavoritesFolder(fileSystemInfo.FullName, destinationFileName)
    31.                     End If
    32.  
    33.                 End If
    34.             Next
    35.  
    36.         Catch ex As Exception
    37.             MsgBox(ex.Message)
    38.         End Try
    39.  
    40.     End If
    41.  
    42. End Sub
    I tested it (quickly), and it does ignore hidden files and junction points. You may want to change the logic around a bit
    OMG thank you so much!!1 I kind of understand that.. I need to look at it longer. lol

Tags for this Thread

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