5 Replies - 1445 Views - Last Post: 07 January 2015 - 04:51 AM Rate Topic: -----

#1 MichaelK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-January 15

Delete all .txt files in MyDocuments subfolder

Posted 06 January 2015 - 01:31 PM

I've got some code that works great but it only deletes all txt files in the Documents folder (I'd like it to include a folder like "...Documents\Associated Files"). I've tried "SearchAllSubDirectories" but get an error "Access to 'C:\Users\...\Documents\My Music' is denied". I've looked around the forum and saw a lot of posts on deleting but nothing specific to what I'm trying to do. Any tips? Maybe I'm using the wrong verbiage?

My code:

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(
        My.Computer.FileSystem.SpecialDirectories.MyDocuments,
        Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

        My.Computer.FileSystem.DeleteFile(foundFile,
            Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
            Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently)
    Next




Is This A Good Question/Topic? 0
  • +

Replies To: Delete all .txt files in MyDocuments subfolder

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Delete all .txt files in MyDocuments subfolder

Posted 06 January 2015 - 01:35 PM

That's typical UAC/Windows security. Try running the app 'as admin'.
Was This Post Helpful? 1
  • +
  • -

#3 MichaelK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-January 15

Re: Delete all .txt files in MyDocuments subfolder

Posted 06 January 2015 - 03:58 PM

Unfortunately all computers that will be running this won't have admin access (nothing I can do to fix that). :(
Any chance there is a way around that?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Delete all .txt files in MyDocuments subfolder

Posted 06 January 2015 - 04:02 PM

Typically no.. I mean if any ol' malicious app is running around deleting things without user permissions then what's the point of the UAC, right?
Was This Post Helpful? 0
  • +
  • -

#5 MichaelK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-January 15

Re: Delete all .txt files in MyDocuments subfolder

Posted 06 January 2015 - 04:50 PM

True... I figured as much but never hurts to ask. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 MichaelK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-January 15

Re: Delete all .txt files in MyDocuments subfolder

Posted 07 January 2015 - 04:51 AM

Woohoo! I figured it out and it works great!

        'Set NTID to variable NTID
        Dim NTID, TXTPath
        NTID = System.Environment.UserName

        TXTPath = "C:\Users\" & NTID & "\Documents\Associated Files"

        Next

           'Deletes all .txt files        
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(
            TXTPath,
            Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

            My.Computer.FileSystem.DeleteFile(foundFile,
                Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
                Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1