none
Problems using Exclude for folders RRS feed

  • Question

  • Hi there!

    I'm trying to write a simple script to delete some audio/video files from a drive, but letting Managers folder intact:

    Remove-Item E:\* -Recurse -Exclude Managers\* -include *.mp1,*.mp2,*.mp3,*.mp4,*.mpa,*.mpe,*.mpeg,*.mpeg2,*.mpeg3,*.mpg -Verbose -Force

    It's not working :(
    I'm trying to avoid problems with the Managers :)

    Where is the catch?

    Thanks in advance!


    Emilio MANSUR - MCSE:Security; MCSE+I, MCT - http://www.mansur.eti.br

    Tuesday, February 2, 2016 7:42 PM

Answers

  • Hi,

    I don't believe you can use -Exclude to filter out files within a specific folder. You can do something like this though:

    Get-ChildItem E:\* -Recurse -Include *.mp1,*.mp2,*.mp3,*.mp4,*.mpa,*.mpe,*.mpeg,*.mpeg2,*.mpeg3,*.mpg | 
        Where { $_.FullName -notlike '*\Managers\*'} |
            Remove-Item -WhatIf


    • Marked as answer by Emilio MANSUR Tuesday, February 2, 2016 8:42 PM
    Tuesday, February 2, 2016 7:54 PM

All replies

  • Hi,

    I don't believe you can use -Exclude to filter out files within a specific folder. You can do something like this though:

    Get-ChildItem E:\* -Recurse -Include *.mp1,*.mp2,*.mp3,*.mp4,*.mpa,*.mpe,*.mpeg,*.mpeg2,*.mpeg3,*.mpg | 
        Where { $_.FullName -notlike '*\Managers\*'} |
            Remove-Item -WhatIf


    • Marked as answer by Emilio MANSUR Tuesday, February 2, 2016 8:42 PM
    Tuesday, February 2, 2016 7:54 PM
  • It's working!

    Thank you!


    Emilio MANSUR - MCSE:Security; MCSE+I, MCT - http://www.mansur.eti.br

    Tuesday, February 2, 2016 8:42 PM
  • Cheers, you're very welcome.

    Tuesday, February 2, 2016 9:19 PM