How to delete files older than 7 days using a BAT file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abhishekkumarrai3
Posts: 12
Joined: 25 Aug 2016 04:20

How to delete files older than 7 days using a BAT file

#1 Post by abhishekkumarrai3 » 03 Oct 2016 23:21

Hi All,

i have to delete files older than 7 days using a BAT file,

i have used below command but this command is deleting all files, but i want it should delete files older than 7 days.

forfiles /p "C:\Users\YOURUSERNAME\Downloads" /s /m *.* /c "cmd /c Del @path" /d -7


kindly please help for this.

Thanks,
Abhi

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to delete files older than 7 days using a BAT file

#2 Post by ShadowThief » 04 Oct 2016 00:13

This may seem like a dumb question, but do you have any files in the folder that are not older than 7 days? I don't see anything wrong with the command you're using.

abhishekkumarrai3
Posts: 12
Joined: 25 Aug 2016 04:20

Re: How to delete files older than 7 days using a BAT file

#3 Post by abhishekkumarrai3 » 04 Oct 2016 00:27

ShadowThief wrote:This may seem like a dumb question, but do you have any files in the folder that are not older than 7 days? I don't see anything wrong with the command you're using.



Hi ShadowThief,

Yeah i have files in the folder that are not older than 7 days, the issue is its also deleting the file which is only 3 days old.


please help me for this.

Thanks,
Abhi

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to delete files older than 7 days using a BAT file

#4 Post by SIMMS7400 » 04 Oct 2016 01:42

I use this option quite frequently :

Code: Select all

ROBOCOPY C:\source C:\destination /mov /minage:7
del C:\destination /q


ROBOCOPY supports UNC paths unlike Forfiles unless you utilize NET USE:

Code: Select all

net use Z: \\unc\path\to\my\folder
if %errorlevel% equ 0 (
    forfiles /p Z: /s /m *.log /D -7 /C "cmd /c del @path"
) else (
    echo "Z: is already in use, please use another drive letter!"
)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to delete files older than 7 days using a BAT file

#5 Post by Squashman » 04 Oct 2016 06:38

abhishekkumarrai3 wrote:
ShadowThief wrote:This may seem like a dumb question, but do you have any files in the folder that are not older than 7 days? I don't see anything wrong with the command you're using.



Hi ShadowThief,

Yeah i have files in the folder that are not older than 7 days, the issue is its also deleting the file which is only 3 days old.


please help me for this.

Thanks,
Abhi

Could you show us the output of the DIR command before you run the FORFILES command.

Thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to delete files older than 7 days using a BAT file

#6 Post by foxidrive » 06 Oct 2016 14:37

abhishekkumarrai3 wrote:
ShadowThief wrote: I don't see anything wrong with the command you're using.


the issue is its also deleting the file which is only 3 days old.


please help me for this.

It's been a while. Did you sort this out?

Squashman was trying to determine your problem too.

abhishekkumarrai3
Posts: 12
Joined: 25 Aug 2016 04:20

Re: How to delete files older than 7 days using a BAT file

#7 Post by abhishekkumarrai3 » 07 Oct 2016 01:23

foxidrive wrote:
abhishekkumarrai3 wrote:
ShadowThief wrote: I don't see anything wrong with the command you're using.


the issue is its also deleting the file which is only 3 days old.


please help me for this.

It's been a while. Did you sort this out?

Squashman was trying to determine your problem too.



Thank you so much to everyone..

its been sorted out...

and sorry for late reply..

Thanks,
Abhi

Post Reply