-1

enter image description hereI have a directory that contains .exe executable and required input files. For each individual request, a separate directory (of the format Output_) is created in the same path and the respective output files are directed there.

Now i am in need of purging the older output directories. Can somebody explain how to achieve this in windows. I went through the forfiles documentation and other stack overflow answers but did not find any information on providing name pattern for folders that need to be deleted although this option is available for file deletion.

I am trying the below code. But i want to specify the folder pattern as well so that i do not delete other files.

forfiles /p "C:\work\Analysis\" /d +7 /c "cmd /c IF @isdir == TRUE rd /S /Q @path"
AsteriK
  • 39
  • 1
  • 9
  • 1
    Please [edit your question](https://stackoverflow.com/posts/47388853/edit) and post what did you tried as code until now ! – Hackoo Nov 20 '17 at 09:39
  • share a screenshot on how the folder structure looks like – Baljeetsingh Sucharia Nov 20 '17 at 09:40
  • this might be helpful in your question: [how to add date created to folder sort option](https://superuser.com/questions/434577/how-do-i-add-the-date-created-sort-by-option-to-the-shell-in-windows-7) – Oleg Alexandrov Nov 20 '17 at 09:40
  • Possible duplicate: [How to remotely delete subfolders older than x days using forfiles](https://stackoverflow.com/q/33537632) – aschipfl Nov 20 '17 at 09:42

2 Answers2

0

Here an example in Powershell

$Now = Get-Date
$Hours = "4"
$TargetFolder = **!Folder in which old data exists!**
$LastWrite = $Now.AddHours(-$Hours)

$Folders = get-childitem -path $TargetFolder | 
Where {$_.psIsContainer -eq $true} | 
Where {$_.LastWriteTime -le "$LastWrite"} 

 foreach ($Folder in $Folders)
    {
    write-host "Deleting $Folder" -foregroundcolor "Red"
    Remove-Item $TARGETFOLDER\$Folder -recurse -Force
    }
Herm L
  • 37
  • 1
  • 8
  • @All: My challenge is to delete folder having names of particular pattern. I do not want to touch any other file/ folders in the same path. For example, as seen in the screenshot, i need to delete only guid_* folders and nothing else. – AsteriK Nov 20 '17 at 13:03
0

Below command did the job for me. /M works with directories as well. Thanks to JJS's answer on https://superuser.com/q/764348

forfiles /P C:\work\Analysis\ /M guid_* /d +5 /C "cmd /c if @isdir==TRUE rmdir /s /q @file"
AsteriK
  • 39
  • 1
  • 9