Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB 2010 code fails when copying root directory.

VB 2010 code fails when copying root directory.

Scheduled Pinned Locked Moved Visual Basic
visual-studiocsharpsysadmindebuggingtools
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KreativeKai
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • K KreativeKai

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • L Luc Pattyn

        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.

        K Offline
        K Offline
        KreativeKai
        wrote on last edited by
        #3

        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

        L D 2 Replies Last reply
        0
        • K KreativeKai

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.

          K 1 Reply Last reply
          0
          • K KreativeKai

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            K 1 Reply Last reply
            0
            • D 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 Kreskowiak

              K Offline
              K Offline
              KreativeKai
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • L Luc Pattyn

                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.

                K Offline
                K Offline
                KreativeKai
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • K KreativeKai

                  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

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  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

                  K 1 Reply Last reply
                  0
                  • D 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 Kreskowiak

                    K Offline
                    K Offline
                    KreativeKai
                    wrote on last edited by
                    #9

                    Thanks for the clarification! It is evident now that I was misunderstanding the File.Attributes logic. :)

                    Lost in the vast sea of .NET

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups