Page 1 of 1

batch script for deleting files 90 days older

Posted: 08 Nov 2018 14:29
by lalat06bag
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

Re: batch script for deleting files 90 days older

Posted: 08 Nov 2018 14:30
by Squashman

Re: batch script for deleting files 90 days older

Posted: 08 Nov 2018 15:17
by lalat06bag
Thank you Very much.

Re: batch script for deleting files 90 days older

Posted: 08 Nov 2018 16:43
by lalat06bag
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?

Re: batch script for deleting files 90 days older

Posted: 08 Nov 2018 18:50
by Squashman
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

Re: batch script for deleting files 90 days older

Posted: 09 Nov 2018 15:15
by lalat06bag
Thank you a ton! This worked perfectly fine!

Re: batch script for deleting files 90 days older

Posted: 16 Nov 2018 08:17
by lalat06bag
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

Re: batch script for deleting files 90 days older

Posted: 21 Nov 2018 18:11
by penpen
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.