VB 2010 code fails when copying root directory.
-
I have a VB application I wrote in 2002 with Visual Studio 2002 at that time. It copies files and directories with a variety of parameters. We use the utility to copy files from server to server or destktop to desktop via scheduled tasks, etc. It is a framework 1.0 app and has worked for years. This past year it was evident it needs upgraded to VS 2010. Everything is working fine except in Windows 7 with parameters we've used for years with XP and on the servers, when we copy a root directory, for example, D:\, the copy program will try to copy the Recycle Bin and the System Volume Information and fail. In previous operating systems I'm guessing it didn't try to copy these items, but in Windows 7 for some reason it does. We really don't want to copy these two items anyhow, so in the new VS VB 2010 code I wrote the following logic, but I've found that from one server it isn't working:
Dim source as string = “D:\” Dim currentDirectory As DirectoryInfo = New DirectoryInfo(source) Dim di As DirectoryInfo For Each di In currentDirectory.GetDirectories() If blnIsRoot = True Then If (di.Attributes = FileAttributes.Normal) Or _ (di.Attributes = FileAttributes.Directory) Or _ (di.Attributes = FileAttributes.SparseFile) Or _ (di.Attributes = FileAttributes.ReparsePoint) Or _ (di.Attributes = FileAttributes.NotContentIndexed) Or _ (di.Attributes = FileAttributes.System) Or _ (di.Attributes = FileAttributes.Offline) Or _ (di.Attributes = FileAttributes.Temporary) Or _ (di.Attributes = FileAttributes.Device) Or _ (di.Attributes = FileAttributes.Hidden) Or _ (di.Attributes = FileAttributes.Encrypted) Or _ (di.Attributes = FileAttributes.Compressed) Or _ (di.Attributes = FileAttributes.Archive) Or _ (di.Attributes = FileAttributes.ReadOnly) Then .... logic to copy directory end if statments, next statements, etc...
With this code, it will copy a root directory (d:\) and all sub directories and ignore the recycle bin and system volume information. Code works 99% of the time, but with one server which houses our intranet, I'm copying from \\inttranetserver\wwwroot\dept\its\ to d:\test\ and it is not copying any directories at all, even though there are multiple under ..\its\ I run the code in debug, set breakpoints and skip the if statement (shown above) checking attributes and it works fine, but put that if statment back in it doesn't copy any of the directorie -
I have a VB application I wrote in 2002 with Visual Studio 2002 at that time. It copies files and directories with a variety of parameters. We use the utility to copy files from server to server or destktop to desktop via scheduled tasks, etc. It is a framework 1.0 app and has worked for years. This past year it was evident it needs upgraded to VS 2010. Everything is working fine except in Windows 7 with parameters we've used for years with XP and on the servers, when we copy a root directory, for example, D:\, the copy program will try to copy the Recycle Bin and the System Volume Information and fail. In previous operating systems I'm guessing it didn't try to copy these items, but in Windows 7 for some reason it does. We really don't want to copy these two items anyhow, so in the new VS VB 2010 code I wrote the following logic, but I've found that from one server it isn't working:
Dim source as string = “D:\” Dim currentDirectory As DirectoryInfo = New DirectoryInfo(source) Dim di As DirectoryInfo For Each di In currentDirectory.GetDirectories() If blnIsRoot = True Then If (di.Attributes = FileAttributes.Normal) Or _ (di.Attributes = FileAttributes.Directory) Or _ (di.Attributes = FileAttributes.SparseFile) Or _ (di.Attributes = FileAttributes.ReparsePoint) Or _ (di.Attributes = FileAttributes.NotContentIndexed) Or _ (di.Attributes = FileAttributes.System) Or _ (di.Attributes = FileAttributes.Offline) Or _ (di.Attributes = FileAttributes.Temporary) Or _ (di.Attributes = FileAttributes.Device) Or _ (di.Attributes = FileAttributes.Hidden) Or _ (di.Attributes = FileAttributes.Encrypted) Or _ (di.Attributes = FileAttributes.Compressed) Or _ (di.Attributes = FileAttributes.Archive) Or _ (di.Attributes = FileAttributes.ReadOnly) Then .... logic to copy directory end if statments, next statements, etc...
With this code, it will copy a root directory (d:\) and all sub directories and ignore the recycle bin and system volume information. Code works 99% of the time, but with one server which houses our intranet, I'm copying from \\inttranetserver\wwwroot\dept\its\ to d:\test\ and it is not copying any directories at all, even though there are multiple under ..\its\ I run the code in debug, set breakpoints and skip the if statement (shown above) checking attributes and it works fine, but put that if statment back in it doesn't copy any of the directorieHi, some facts: 1. file attributes are flags, several of them can be set at the same time, so your compare statements aren't going to cut it. 2. Windows, since Vista, is more protective about a number of files and folders, your old code is probably getting access violations. what I do: - copy everything you need in a big loop; inside the loop, have a try-catch, and log each individual failure. - before copying, check for some special names and skip those. - the exceptions will tell you which files you will have to skip, i.e. which checks to add to your code. - and this will have to amended every time you move to the next Windows version... :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
-
Hi, some facts: 1. file attributes are flags, several of them can be set at the same time, so your compare statements aren't going to cut it. 2. Windows, since Vista, is more protective about a number of files and folders, your old code is probably getting access violations. what I do: - copy everything you need in a big loop; inside the loop, have a try-catch, and log each individual failure. - before copying, check for some special names and skip those. - the exceptions will tell you which files you will have to skip, i.e. which checks to add to your code. - and this will have to amended every time you move to the next Windows version... :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
On this fact: "file attributes are flags, several of them can be set at the same time, so your compare statements aren't going to cut it." I am seeing this when I follow via breakpoints, where it seems like several attributes are set. This makes sense to me. Your suggestions: - copy everything you need in a big loop; inside the loop, have a try-catch, and log each individual failure. - before copying, check for some special names and skip those. I like your suggestions, I just have a question. As far as tracking it to a log file, what attributes do you think I should catch that would be helpful to log? di.Attributes, di.fullname... anything else? When you say special names, are you saying if I check fullname and the name of the recycle bin to a string I define "Recycle Bin ...". Is the name always consistent. We have several differnt servers 2000, 2003, 2008 and workstations are basically XP, Vista, and now 7. I assume the names might change between operating systems and the log will be helpful. I like this idea, but hoping you can clarify a little? Thanks for your help! ;)
Lost in the vast sea of .NET
-
On this fact: "file attributes are flags, several of them can be set at the same time, so your compare statements aren't going to cut it." I am seeing this when I follow via breakpoints, where it seems like several attributes are set. This makes sense to me. Your suggestions: - copy everything you need in a big loop; inside the loop, have a try-catch, and log each individual failure. - before copying, check for some special names and skip those. I like your suggestions, I just have a question. As far as tracking it to a log file, what attributes do you think I should catch that would be helpful to log? di.Attributes, di.fullname... anything else? When you say special names, are you saying if I check fullname and the name of the recycle bin to a string I define "Recycle Bin ...". Is the name always consistent. We have several differnt servers 2000, 2003, 2008 and workstations are basically XP, Vista, and now 7. I assume the names might change between operating systems and the log will be helpful. I like this idea, but hoping you can clarify a little? Thanks for your help! ;)
Lost in the vast sea of .NET
KreativeKai wrote:
what attributes ...?
just the name, attributes aren't relevant, they can change, e.g. when a backup is made.
KreativeKai wrote:
special names...
check everything you ever encountered, do not change with the Windows version, just add to the skip list:
for (...) {
...
if (shortName=="Recycle Bin") continue;
if (shortName=="Trash Can") continue;
...
try {
....
} catch(...) {
....
}
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
-
On this fact: "file attributes are flags, several of them can be set at the same time, so your compare statements aren't going to cut it." I am seeing this when I follow via breakpoints, where it seems like several attributes are set. This makes sense to me. Your suggestions: - copy everything you need in a big loop; inside the loop, have a try-catch, and log each individual failure. - before copying, check for some special names and skip those. I like your suggestions, I just have a question. As far as tracking it to a log file, what attributes do you think I should catch that would be helpful to log? di.Attributes, di.fullname... anything else? When you say special names, are you saying if I check fullname and the name of the recycle bin to a string I define "Recycle Bin ...". Is the name always consistent. We have several differnt servers 2000, 2003, 2008 and workstations are basically XP, Vista, and now 7. I assume the names might change between operating systems and the log will be helpful. I like this idea, but hoping you can clarify a little? Thanks for your help! ;)
Lost in the vast sea of .NET
On top of what Luc said, why are you even checking attributes?? Unless they determine eligibility of a file to be copied, you have no need to look at the file attributes. Your code snippet suggests that you're not using the attributes for anything at all, so I'd just remove the checks.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
On top of what Luc said, why are you even checking attributes?? Unless they determine eligibility of a file to be copied, you have no need to look at the file attributes. Your code snippet suggests that you're not using the attributes for anything at all, so I'd just remove the checks.
A guide to posting questions on CodeProject[^]
Dave KreskowiakHi Dave, I was checking attributes because originally I was hoping to exclude the Recycle Bin and System Volume Information by skipping a system attribute, but found through testing that if I did an "if statement" with all the attributes it worked. Didn't make sense, but worked... Unfortunately from our intranet server, the copy was skipping those folders, so the logic was no longer sound. You would think Microsoft would have a way of catching the recycle bin in the logic by looking for an attribute of some kind related to system files/directories? Oh well, the logic worked by catching the name of the directories and skipping them that way. Thanks for the feedback! Always appreciated!
Lost in the vast sea of .NET
-
KreativeKai wrote:
what attributes ...?
just the name, attributes aren't relevant, they can change, e.g. when a backup is made.
KreativeKai wrote:
special names...
check everything you ever encountered, do not change with the Windows version, just add to the skip list:
for (...) {
...
if (shortName=="Recycle Bin") continue;
if (shortName=="Trash Can") continue;
...
try {
....
} catch(...) {
....
}
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
Thanks for all your help Luc! It is appreciated! :cool: Below is the code I used in case anyone else can learn from it later when searching the message boards...
Dim di As DirectoryInfo For Each di In currentDirectory.GetDirectories() Dim strDirectoryName As String = di.Name Dim intFoundRB As Integer = strDirectoryName.IndexOf("$RECYCLE.BIN") Dim intFoundSV As Integer = strDirectoryName.IndexOf("System Volume Information") 'Skip $Recycle.Bin or System Volume Information directories If (intFoundRB = -1) And (intFoundSV = -1) Then Try ... Copy logic Catch ex As Exception ... logic writing to log file for future reference if needed... End Try End If Next
Lost in the vast sea of .NET
-
Hi Dave, I was checking attributes because originally I was hoping to exclude the Recycle Bin and System Volume Information by skipping a system attribute, but found through testing that if I did an "if statement" with all the attributes it worked. Didn't make sense, but worked... Unfortunately from our intranet server, the copy was skipping those folders, so the logic was no longer sound. You would think Microsoft would have a way of catching the recycle bin in the logic by looking for an attribute of some kind related to system files/directories? Oh well, the logic worked by catching the name of the directories and skipping them that way. Thanks for the feedback! Always appreciated!
Lost in the vast sea of .NET
For future reference, your code wasn't checking to see if any attributes were turned on. That IF statement you posted only returned True if one and only one attribute was turned on. Your check got lucky if it returned true. You would normally check to see if a single bit flag is enabled like this:
If (di.Attributes And FileAttributes.System) = FileAttributes.System Then
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
For future reference, your code wasn't checking to see if any attributes were turned on. That IF statement you posted only returned True if one and only one attribute was turned on. Your check got lucky if it returned true. You would normally check to see if a single bit flag is enabled like this:
If (di.Attributes And FileAttributes.System) = FileAttributes.System Then
A guide to posting questions on CodeProject[^]
Dave KreskowiakThanks for the clarification! It is evident now that I was misunderstanding the File.Attributes logic. :)
Lost in the vast sea of .NET