batch script for deleting files 90 days older

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lalat06bag
Posts: 43
Joined: 10 Jan 2018 15:21

batch script for deleting files 90 days older

#1 Post by lalat06bag » 08 Nov 2018 14:29

Hi,

I want to delete the files in a folder which are more than 90 days older based on the date modified column in the folder in a batch script. Please help.

Thanks

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

Re: batch script for deleting files 90 days older

#2 Post by Squashman » 08 Nov 2018 14:30


lalat06bag
Posts: 43
Joined: 10 Jan 2018 15:21

Re: batch script for deleting files 90 days older

#3 Post by lalat06bag » 08 Nov 2018 15:17

Thank you Very much.

lalat06bag
Posts: 43
Joined: 10 Jan 2018 15:21

Re: batch script for deleting files 90 days older

#4 Post by lalat06bag » 08 Nov 2018 16:43

Hi,

I tried this with the local path, it worked perfectly fine. But when given a share drive path, it shows me an error as
Error: UNC paths (\\machine\share) are not supported.

here is What I am trying.

SET "NASS=\\abc"
SET "TEMPDIR=!NASS!\CU\OR"
forfiles /p "%TEMPDIR%\OR" /s /m *.* /c "cmd /c Del @path" /d -90

Can anyone please see?

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

Re: batch script for deleting files 90 days older

#5 Post by Squashman » 08 Nov 2018 18:50

lalat06bag wrote:
08 Nov 2018 16:43
Hi,

I tried this with the local path, it worked perfectly fine. But when given a share drive path, it shows me an error as
Error: UNC paths (\\machine\share) are not supported.

here is What I am trying.

SET "NASS=\\abc"
SET "TEMPDIR=!NASS!\CU\OR"
forfiles /p "%TEMPDIR%\OR" /s /m *.* /c "cmd /c Del @path" /d -90

Can anyone please see?
Nobody can magically make FORFILES work with UNC paths. FORFILES is LITERALLY telling you that it does not work with UNC paths. Not sure why you are using delayed expansion either.

Use PUSHD and POPD to work around the limitation.

Code: Select all

SET "NASS=\\abc"
SET "TEMPDIR=%NASS%\CU\OR"
PUSHD "%TEMPDIR%"
forfiles /s /m *.* /c "cmd /c Del @path" /d -90
POPD

lalat06bag
Posts: 43
Joined: 10 Jan 2018 15:21

Re: batch script for deleting files 90 days older

#6 Post by lalat06bag » 09 Nov 2018 15:15

Thank you a ton! This worked perfectly fine!

lalat06bag
Posts: 43
Joined: 10 Jan 2018 15:21

Re: batch script for deleting files 90 days older

#7 Post by lalat06bag » 16 Nov 2018 08:17

Hi,

While running this script, we figured out that we need to keep few files irrespective of their dates. I have below files which we need to keep. They are all ending with -en-us.

sorry for bugging. kindly see. All files older than 90 days will be removed except the below ones ending with -en-us.

ex:

XXXyy-en-us
ssxx-en-us
wtdt09-en-us

Thanks

penpen
Expert
Posts: 1622
Joined: 23 Jun 2013 06:15
Location: Germany

Re: batch script for deleting files 90 days older

#8 Post by penpen » 21 Nov 2018 18:11

You could use forfiles to echo the files you want to move (inclucing surrounding doublequotes) instead of deleting them and redirect the result to a temporary file. Then use findstr to filter the ones you want to keep.

penpen.

Post Reply