Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have several files named like yyyymmdd-hhmiss.JobActivity2Reportyyyymmdd.csv.

I need a Windows command (a single command preferably, not a CMD file) that deletes files with this naming format that are older than 14 days. I would put this command into a Tidal(Scheduler) job to keep the Archive directory cleaned up. Also, I want to use the timestamps on the file (and not the timestamps encoded into the file name) to decide what to delete – it’s possible multiple files could come in on the same day for different days – for example, we may get Saturday, Sunday, and Monday files on Monday.

Any help will be appreciated.

Thanks a lot.

share|improve this question

migrated from stackoverflow.com Apr 5 '13 at 20:15

This question came from our site for professional and enthusiast programmers.

    
Can the "single command" be a PowerShell invocation? –  Kevin Richardson Apr 5 '13 at 17:15
    
I don't have admin privileges to change the execution policy for windows powershell. so cannot use powershell cmds. –  user2249541 Apr 5 '13 at 17:27
    
Even with a 'Restricted' (default) execution policy, you should be able to run commands, e.g. powershell -command "& Get-ChildItem | Where-Object {($_.LastWrite -gt '4/1/2013') -and ($_.Name.Contains('JobActivity2Report'))}" | Remove-Item -force –  Kevin Richardson Apr 5 '13 at 18:01
    
Thanks for the reply, Its saying Remove-Item is not recognized as an internal command. Also where will I define the path of the folder where I will be deleting files, like \\ProcessFolders\I_STAGE\SrcFiles\Archive. Thanks Again. –  user2249541 Apr 5 '13 at 18:21
    
Sorry about that. I put the ending quote in the wrong place. I added the corrected version (with a specific directory) as an answer. –  Kevin Richardson Apr 5 '13 at 18:45
add comment

1 Answer

up vote 0 down vote accepted

I don't think you can make this happen in one command using standard shell tools.

Try:

powershell -command "& Get-ChildItem 'C:\MyStuff' | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-14)) -and ($_.Name.Contains('JobActivity2Report'))} | Remove-Item -force"
share|improve this answer
    
Thanks a lot, I later figured out ending quote was issue, but then I got stuck with path, I was adding "-path C:\MyStff" after Get-Children which was not working. But the one you posted works great.thanks again. –  user2249541 Apr 5 '13 at 18:51
    
One last question I will have to delete files older than 14 days everytime the scheduler run. Do I need to change '4/1/2013' everytime, or can I use something like adDays ? to make it automated –  user2249541 Apr 5 '13 at 18:53
    
I had just used that date as an example. I updated the answer to use Get-Date so that you don't have to worry about changing anything. –  Kevin Richardson Apr 5 '13 at 19:20
    
Thank You so much –  user2249541 Apr 5 '13 at 19:33
    
No problem. If the answer was useful, please check it as accepted. –  Kevin Richardson Apr 5 '13 at 19:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.