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.
Need check if file exist older than n days then do a delete.
Moderator: DosItHelp
Re: Need check if file exist older than n days then do a del
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
Re: Need check if file exist older than n days then do a del
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?
Thanks!
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!
Re: Need check if file exist older than n days then do a del
Hmm... It works for me
> what does it mean?
> 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 '*' .
Re: Need check if file exist older than n days then do a del
Is the older than day looking at the date created or modified date?
Re: Need check if file exist older than n days then do a del
It's the Date Last Modified as you could figure out by yourself if you would type FORFILES /? into a cmd window.
Regards
aGerman
Regards
aGerman
Re: Need check if file exist older than n days then do a del
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?
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?