-
Oct 5th, 2015, 11:39 AM
#1
Thread Starter
Junior Member
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.
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
-
Oct 5th, 2015, 01:09 PM
#2
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.
-
Oct 6th, 2015, 08:21 AM
#3
Thread Starter
Junior Member
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.
-
Oct 6th, 2015, 09:24 AM
#4
Thread Starter
Junior Member
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 :-(
-
Oct 6th, 2015, 09:27 AM
#5
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.
-
Oct 6th, 2015, 11:14 AM
#6
Thread Starter
Junior Member
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
Originally Posted by dday9
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.
-
Oct 6th, 2015, 11:46 AM
#7
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
Originally Posted by rzubulake
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."
-
Oct 6th, 2015, 01:49 PM
#8
Thread Starter
Junior Member
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
Originally Posted by SJWhiteley
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? :-(
-
Oct 6th, 2015, 02:31 PM
#9
Thread Starter
Junior Member
-
Oct 6th, 2015, 04:15 PM
#10
Thread Starter
Junior Member
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
When i catch the message in a message box.
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.
-
Oct 6th, 2015, 04:26 PM
#11
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.
-
Oct 6th, 2015, 04:46 PM
#12
Thread Starter
Junior Member
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?
-
Oct 6th, 2015, 04:58 PM
#13
Thread Starter
Junior Member
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..
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.
-
Oct 6th, 2015, 05:31 PM
#14
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.
-
Oct 6th, 2015, 05:51 PM
#15
Thread Starter
Junior Member
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
Originally Posted by Inferrd
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.
-
Oct 6th, 2015, 06:02 PM
#16
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
Originally Posted by rzubulake
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.
-
Oct 6th, 2015, 06:30 PM
#17
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:
Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String)
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)
If (sourceDirectoryInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse
(sourceDirectoryInfo.Attributes And FileAttributes.System) = FileAttributes.System OrElse
(sourceDirectoryInfo.Attributes And FileAttributes.ReparsePoint) = FileAttributes.ReparsePoint Then
Exit Sub
Else
' 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 (fileSystemInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse
(fileSystemInfo.Attributes And FileAttributes.System) = FileAttributes.System Then
Continue For
Else
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
End If
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
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
-
Oct 6th, 2015, 10:39 PM
#18
Thread Starter
Junior Member
Re: Copying Files from Documents folder and how to skip hidden folder or delete ones?
Originally Posted by Inferrd
I am way too tired to look into this properly, but the following modifications to your code should get you started:
VB.NET Code:
Public Sub CopyFavoritesFolder(ByVal sourcePath As String, ByVal destinationPath As String) Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath) If (sourceDirectoryInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse (sourceDirectoryInfo.Attributes And FileAttributes.System) = FileAttributes.System OrElse (sourceDirectoryInfo.Attributes And FileAttributes.ReparsePoint) = FileAttributes.ReparsePoint Then Exit Sub Else ' 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 (fileSystemInfo.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden OrElse (fileSystemInfo.Attributes And FileAttributes.System) = FileAttributes.System Then Continue For Else 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 End If Next Catch ex As Exception MsgBox(ex.Message) End Try End If 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|