Delete files older than 15 days & ignore file names with "Month" string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Delete files older than 15 days & ignore file names with "Month" string

#1 Post by SIMMS7400 » 20 Jan 2017 11:25

Hi Team -

I'm having trouble with a script. Here is the requirement:

Delete files older than 15 days but ignoring any file names containing the string "String".

Here is what I have so far, ignoring file names with "Month", but I can't seem to finish it up.

%%i below contains the potential list of files to delete, but I need to parse that list and only delete files older than 15 days.

Code: Select all

@echo off
setlocal disableDelayedExpansion
for /f "usebackq tokens=*" %%i in (`dir /a:-d /b /s %Target_PATH%*.* ^| findstr /v "[\\][^\\]*Month[^\\]*$"` ) do (

::code here

)
endlocal
PAUSE


Thanks!

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

Re: Delete files older than 15 days & ignore file names with "Month" string

#2 Post by SIMMS7400 » 20 Jan 2017 12:17

Here is a solution I came up with but I'm certain it could be simplified.

Code: Select all

@ECHO OFF

SET Target_PATH=C:\Hyperion_Batch\Files\
SET TEMPDIR=TEMP
SET PATTERN=*Month*.zip

::-- Move all files containing "Month" in file name to TEMP directory --::
FOR %%A IN ("%Target_PATH:~0,-1%\%PATTERN%") DO (

::-- Make TEMP directory --::
CD ..\..
IF NOT EXIST TEMP MKDIR TEMP
CD %Target_PATH%

::-- Move all files containing "Month" in file name to TEMP directory --::
MOVE /Y "%%~A" "..\%TEMPDIR%"

)

::-- Delete files older than 15 days --::
forfiles /p "%Target_PATH:~0,-1%" /s /m *.* /c "cmd /c ECHO Del @path" /d -15

::-- Move back "Month" files and remove TEMP directory --::
PUSHD %Target_PATH%
MOVE /Y "..\%TEMPDIR%\*.*" "%Target_PATH%"
rmdir "..\%TEMPDIR%" /s /q
POPD

PAUSE

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Delete files older than 15 days & ignore file names with "Month" string

#3 Post by Compo » 20 Jan 2017 13:45

You could probably use a quicker method:

Code: Select all

@Echo Off
Set "_target=C:\Hyperion_Batch\Files"
Set "_daysage=15"
Set "_pattern=*Month*.zip"
Set "_scratch=%TEMP%\~%random%"

MD "%_scratch%"
RoboCopy "%_target%" "%_scratch%" /XF %_pattern% /Move /MinAge:%_daysage%
RD/S/Q "%_scratch%"
Adjust your variables to suit

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

Re: Delete files older than 15 days & ignore file names with "Month" string

#4 Post by SIMMS7400 » 20 Jan 2017 14:51

Fantastic, thank you Compo!

:lol:

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Delete files older than 15 days & ignore file names with "Month" string

#5 Post by pieh-ejdsch » 20 Jan 2017 16:46

Compo wrote:You could probably use a quicker method:


more quicker than is this
RoboCopy "%_target%" "%_scratch%" /XF %_pattern% /create /Move /MinAge:%_daysage%
RD/S/Q "%_scratch%"


Phil

Post Reply