0

I am trying to move all pdf files in C:\ to E:\mypdf I use this code:

For Each x In New DirectoryInfo("C:\").GetFiles("*.pdf", SearchOption.AllDirectories) 
    File.Copy(x.FullName, Path.Combine("E:\mypdf", x.Name), False)
Next

How can I ignore folders that require administrator privileges?

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • 1
    You could use the `Directory.GetAccessControl(path, AccessControlSections.Access).GetAccessRules(True, False, GetType(SecurityIdentifier))` method(s), get the `IdentityReference.Value` and compare it to the `[WindowsIdentity].Owner.Value` before accessing a directory. Or `Try`/`Catch` the `UnauthorizedAccessException` and ignore it (recommended? No, but...). You also have `ReparsePoints` and other amenities to care about. You may want to pre-filter the listing, excluding directories with attribute `FileAttributes.ReparsePoint` (and `FileAttributes.Encrypted`). Prefilter as much as possible. – Jimi Apr 16 '19 at 12:45
  • Or search SO for a pre-built solution :) – Jimi Apr 16 '19 at 12:46
  • Can you help me write code? I tried but did not succeed – Niven Soha Apr 16 '19 at 12:59
  • @NivenSoha [Access to the path is denied when using Directory.GetFiles(…)](https://stackoverflow.com/q/4986293/1115360) has an example which you could try to translate from C# to VB.NET - even an on-line translator will probably do a good job. Then you can adapt it to only look for .pdf files and copy them. – Andrew Morton Apr 16 '19 at 15:20
  • You just can't do this for the C:\ directory, you will always run into an inaccessible directory named "System Volume Information". That's where restore point data is stored. Lots and lots of questions about iterating the directory tree yourself, you don't need one more hit. – Hans Passant Apr 16 '19 at 16:59

0 Answers0