Page 1 of 1

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

Posted: 03 Oct 2016 23:21
by abhishekkumarrai3
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

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

Posted: 04 Oct 2016 00:13
by ShadowThief
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.

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

Posted: 04 Oct 2016 00:27
by abhishekkumarrai3
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

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

Posted: 04 Oct 2016 01:42
by SIMMS7400
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!"
)

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

Posted: 04 Oct 2016 06:38
by Squashman
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

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

Posted: 06 Oct 2016 14:37
by foxidrive
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.

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

Posted: 07 Oct 2016 01:23
by abhishekkumarrai3
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