-5

I am trying to create a Purgejob in SQL Server server which deletes the .txt files which are older than 15 days.

Get-ChildItem –Path  "D:\ABC\Project" -include *.txt –Recurse | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-15)} | Remove-Item

By running this query it is successfully deleting the .txt files inside the project folder, but it is also deleting the subfolder .txt files which are inside the project folder. I don't want to delete these files. All I want is to delete the .txt files inside project folder but not in the subfolder of the project folder.

morgb
  • 2,252
  • 2
  • 14
  • 14
VamRock
  • 1
  • 3
  • _"but not in the subfolder"_ than why do you use `-recurse` flag? O_o If you're about to say _"otherwise `-include` does not work"_ you should read this topic: http://stackoverflow.com/questions/28600923/difference-between-include-and-filter-in-get-childitem – n01d Jul 28 '16 at 10:40

1 Answers1

-1

Remove the recursion and just search in the main folder:

Get-ChildItem –Path "D:\ABC\Project\*.txt" | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-15)} | Remove-Item
morgb
  • 2,252
  • 2
  • 14
  • 14