none
Delete Files listed on a Text file across many directories RRS feed

  • Question

  • I've got a txt file with approx 60 files names and I need to delete the file across approx 300 folders.  The folders are all located under a single data directory (ex C:\data).

    I found the below code a ran it, but it took over an hour to run.  Is there a better and faster way to accomplish this?

    gc C:\cleanup.txt  | % {gci "C:\data" -Include $_ -Recurse} | % {Remove-Item $_ }

    Thanks,

    Kevin

    Saturday, January 9, 2016 6:48 PM

Answers

  • We can search for all files at the same time like this

    cd c:\data
    Get-ChildItem -include (gc C:\cleanup.txt) -recurse |Remove-Item WhatIf


    \_(ツ)_/



    • Edited by jrv Saturday, January 9, 2016 7:10 PM
    • Marked as answer by bakerkr944 Monday, January 11, 2016 10:43 AM
    Saturday, January 9, 2016 7:06 PM
  • You can only detect SYMLINKS like this:

    Get-ChildItem  |?{$_.Attributes -match 'ReparsePoint'}


    \_(ツ)_/

    • Proposed as answer by jrv Saturday, January 16, 2016 12:39 PM
    • Marked as answer by bakerkr944 Saturday, January 16, 2016 12:46 PM
    Saturday, January 16, 2016 12:29 PM

All replies

  • We can search for all files at the same time like this

    cd c:\data
    Get-ChildItem -include (gc C:\cleanup.txt) -recurse |Remove-Item WhatIf


    \_(ツ)_/



    • Edited by jrv Saturday, January 9, 2016 7:10 PM
    • Marked as answer by bakerkr944 Monday, January 11, 2016 10:43 AM
    Saturday, January 9, 2016 7:06 PM
  • yeah, that works!  Got it down to 6 mins
    Saturday, January 9, 2016 7:59 PM
  • I've got a few files in my list that may or may not be symbolic link files.  My thought to delete the file and not the link was to try and use the file type.  Is this possible?  For example, if I have a file called test.txt, the file type would be Text Document, but in the next folder the test.txt could be a file type of .symlink, so I would not want to delete this file.

    Thanks,

    Kevin

    Saturday, January 16, 2016 9:52 AM
  • You can only detect SYMLINKS like this:

    Get-ChildItem  |?{$_.Attributes -match 'ReparsePoint'}


    \_(ツ)_/

    • Proposed as answer by jrv Saturday, January 16, 2016 12:39 PM
    • Marked as answer by bakerkr944 Saturday, January 16, 2016 12:46 PM
    Saturday, January 16, 2016 12:29 PM
  • THANK YOU!!!!!
    Saturday, January 16, 2016 12:33 PM
  • Good luck.

    \_(ツ)_/

    Saturday, January 16, 2016 12:40 PM