**Hi,

How would I write a Windows Command to delete all folders recurvsively with a specific wildcard name ({*}) under a specific path that is older than 10 days? I would like to run this on the command prompt and I also want to run it as a batch file.

set deletepath = "C:\test\"
set days = 10

Delete all subfolders under deletepath that is older than 10 days and begins with "{" and ends with "}" i.e. {*}**

Dani AI

Generated

A focused, testable pair of solutions: a small CMD batch (uses built-in forfiles) and a PowerShell one-liner. These can be scheduled as suggested, or run manually; 's script shows the same idea can be automated.

Save this as a .bat (change values first):

set "deletepath=C:\test"
set "days=10"

forfiles /p "%deletepath%" /s /m "{*}" /d -%days% /c "cmd /c if @isdir==TRUE rmdir /s /q @path"

Notes: test before deleting by replacing rmdir /s /q @path with echo rmdir @path to preview matched folders. forfiles /d uses the item LastWriteTime (modification date). @path is returned quoted so rmdir works with spaces.

PowerShell alternative (more robust, can use CreationTime or LastWriteTime):

Get-ChildItem -Path 'C:\test' -Directory -Recurse -Filter '{*}' |
  Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } |
  Remove-Item -Recurse -Force -WhatIf

Remove -WhatIf after you confirm the preview.

Cautions and tips: run the preview first; if forfiles is not present on older systems use PowerShell; directory timestamps change when contents change so choose LastWriteTime vs CreationTime deliberately; schedule either script with Task Scheduler and run under an account with the necessary permissions.

Recommended Answers

All 5 Replies

i would suggest you scheduled tasks

I wrote this script a few years ago to do just that. You can modify it to restrict it to specific types of names. See attached.

Usage:

  delete_older_than #days [folder] [-s] [-l] 

  where 

    #days       number of days to keep 
    folder      name of folder containing files to delete 
                (default is current)
    -s          delete in sub-directories also (optional)
    -l          list files to delete withoout deleting them (optional)

Thanks for that. Is there anyway I could do this using MS-Dos commands that I can run occasionally or put into a batch file to run as a schedule job?

Is there anyway I could do this using MS-Dos commands

You can run it as a batch job. That's what scripts are for. vbs files are executed either by cscript.ece or wscript.exe. One is for command line (c) mode and the other is from windowed (w) mode. One difference is that under cscript.exe, output goes to the console window and under wscript.exe, output is to a MessageBox. You can set console as the default by

cscript //h:cscript //s

To run it automatically you can set up a task in the Task Scheduler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.