Need check if file exist older than n days then do a delete.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
forumHelp
Posts: 3
Joined: 09 May 2011 09:42

Need check if file exist older than n days then do a delete.

#1 Post by forumHelp » 09 May 2011 10:00

I'm not familiar in writing .bat scripting at all. I'm trying to do the following and need help!

I know there is a command ForFiles that you can use to delete file older than n days. BUT I want to be able to check to make sure there are files to delete first (like do a file count) before actually using the ForFiles command. I am having a job scheduler (ActiveBatch) to execute this batch file. What I find out is that if I only used the one line ForFiles to do the delete, it will fail my job because if there isn't a file to delete, the batch file reports a failure to the job scheduler. That is what I'm trying to avoid.

I would like:
1. Find files with a specific extension in a directory that is older than n days, if exists.
2. If so, use the ForFiles command to delete the files that is older than n days.
3. If not, then don't do anything.
pseudo:
if exists (count of files older than n days > 1 ) then
forfiles ---delete files older than n days
else -- if there are no files that fits that criteria then
exit script.

Please help me script this out. Your help is greatly appreciated!

Thank you in advance.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Need check if file exist older than n days then do a del

#2 Post by !k » 09 May 2011 12:19

if exists (count of files older than n days > 0 ) then

Code: Select all

set /a days=30
set "folder=c:\folder"
set "files=*.txt"
forfiles /P "%folder%" /M %files% /D -%days% &&forfiles /P "%folder%" /M %files% /D -%days% /C "cmd /c del /q @path" ||exit /b 0

forumHelp
Posts: 3
Joined: 09 May 2011 09:42

Re: Need check if file exist older than n days then do a del

#3 Post by forumHelp » 09 May 2011 12:58

I tried replacing the "Set" to something to test with. I put some files in the c:\test folder and expected a few files to delete because it is older than 2 days. BUT, nothing was deleted. Did I do this wrong? Can you explain each "/P", "/M" etc -- what does it mean?

Code: Select all

set /a days=2
set "folder=c:\test"
set "files=*.xls"

forfiles /P "%folder%" /M %files% /D -%days% && forfiles /P "%folder%" /M %files% /D -%days% /C "cmd /c del /q @path" ||exit /b 0



Thanks!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Need check if file exist older than n days then do a del

#4 Post by !k » 09 May 2011 14:15

Hmm... It works for me
Image

> what does it mean?

Code: Select all

    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

forumHelp
Posts: 3
Joined: 09 May 2011 09:42

Re: Need check if file exist older than n days then do a del

#5 Post by forumHelp » 09 May 2011 15:08

Is the older than day looking at the date created or modified date?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need check if file exist older than n days then do a del

#6 Post by aGerman » 09 May 2011 15:15

It's the Date Last Modified as you could figure out by yourself if you would type FORFILES /? into a cmd window.

Regards
aGerman

bergeron
Posts: 1
Joined: 13 Jul 2011 09:23

Re: Need check if file exist older than n days then do a del

#7 Post by bergeron » 13 Jul 2011 09:29

Im trying to use that script to delete 2days older files on a remote machine. I receive this message

ERROR: UNC paths (\\machine\share) are not supported.

here is my script

set /a days=2
set "folder=\\ip address of remote machine\d$\Symantec\Definitions"
set "files=*.zip"
forfiles /P "%folder%" /M %files% /D -%days% &&forfiles /P "%folder%" /M %files% /D -%days% /C "cmd /c del /q
@path" ||exit /b 0
pause

Anyone can help me on this ? Is that another way to use it on a remote machine?

Post Reply