Looking for Windows script to delete files older than 1 year - windows

I need a Windows script to delete files older than 1 year with:
forfiles /S /P "E:\" /M *.* /D -365 /C "cmd /c echo #path" >> "xxx_log"
I am not familiar with Windows scripting.
Can anyone please help me?

If you plan on using windows scripts/batch file, but you are not familiar with them, I really recommend you familiarize yourself first, otherwise it may be dangerous to play with, especially when deleting in bulk...
Fist of all open a Command Prompt and type FORFILES /? to read through the help for the FORFILES command and DEL /? for the DEL (delete) command.
The complete command you are looking for is something like this:
FORFILES /S /P "E:\" /M "*" /D -365 /C "CMD /C DEL #path"
Where E:\ is the top level directory, * is the wildcard you want to match (e.g. change to *.bak to delete only files with the extension .bak) and 365 is the number of days (the minus sign means delete anything older than 365 days).

As well as the invalid /Q option you had used (now corrected), I would also advise that there is no need to use the /M option, as it's default is already *. Also, you may also wish to include some other options for your DEL command, such as its /A and /F options. Additionally, since the intention is to delete files, you should omit possible directory paths, using #ISDIR too. Example: FORFILES /P "E:" /S /D -365 /C "CMD /C IF #ISDIR==FALSE DEL /A /F #PATH". – Compo yesterday
Thank you for your answer. Can I use the day to replace 365 ?
FORFILES /P "E:" /S /D -"01/01/2020" /C "CMD /C IF #ISDIR==FALSE DEL /A /F #PATH" ? Thank you.

I have a script to delete .zip files more than 30 days:
#echo off
FORFILES /p "c:\respaldos" /M *.zip /D -30 /C "cmd /c del #file"
I hope this help you, for more information write in CMD FORFILES /?

Related

Cannot figure out how to use 'FORFILES' to delete files only in sub/directories of 'WIP' folders

I cannot figure out how to get 'FORFILES' to delete files with a '.bak' extension that are 3 years old AND have it only look within sub/directories that are a WIP folder.
#ECHO OFF
setlocal enableextensions disabledelayedexpansion
forfiles /P "Y:\Africa" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO AFRICA - COMPLETED
Echo.
forfiles /P "Y:\Asia" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO ASIA - COMPLETED
Echo.
forfiles /P "Y:\Australia" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO AUSTRAILIA - COMPLETED
Echo.
forfiles /P "Y:\Europe" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO EUROPE - COMPLETED
Echo.
forfiles /P "Y:\North America\Canada" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO CANADA - COMPLETED
Echo.
forfiles /P "Y:\North America\Mexico" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO MEXICO - COMPLETED
Echo.
forfiles /P "Y:\North America\United States" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO UNITED STATES - COMPLETED
Echo.
forfiles /P "Y:\South America" /S /M *.bak /D -1095 /C "cmd /c Del #path"
ECHO SOUTH AMERICA - COMPLETED
Echo.
Pause
Goto :Selection
Currently the code is going into EVERY folder. searching for EVERY '.bak' file older than 3 years in each sub/directory. But I only wish for it to ONLY look in folders and its sub/directories if the folder is called WIP just to shave off half an hour from the scripts run time.
There is nothing to be deleted that applies to these criteria outside of the WIP folders as OTHER codes in the utility I am writing already take care of ‘.bak’ files in folders such as MASTERS (which we do not care if they are 3 years old or not, ‘.bak’ does not belong in there anyway nor is needed). However, if there IS something outside of the WIP folder that would apply to the criteria already set in the code, if its LOCKED (which we do not do for WIP folder files), it locks up the script and hangs at "Access is denied"...)
I will give an example
Africa has:
Y:\Africa\Egypt\wip
Y:\Africa\Kenya\WIP
Y:\Africa\South Africa\WIP
Asia has its many folders with individual countries like Africa has EXCEPT china has 4 folders that have WIP folders inside.
Many of folders labeled regionally are set up like this.
Is it possible to go into each path and skip into a sub/directory until it hits a WIP folder? like a '%%A wildcard' or something like you can do with 'FOR'?
You just need a mechanism to check that the current working directory ends in wip. There are a few different ways to do this but I chose to use a FOR /D /R command and then check to see if the child directory is equal to wip.
#echo off
for /d /r %%G in (wip*) do IF /I "%%~nxG"=="wip" (
PUSHD "%%G"
forfiles /M *.bak /D -1095 /C "cmd /c Del #path"
popd
)
You can put this in the root of your Y: drive and it will only process the directories that end in wip
EDIT:
If you can't run the batch file from the root directory of where you data is there are three ways to make the script change to a different directory.
If you just want to change to a different drive letter, you specify the driver letter with the colon
Y:
You can also use the CD command. Short for change directory.
CD mydirectory
If you need to change the directory and the drive letter at the same time you need to use the /D option with the CD command.
CD /D Y:\mydirecotry
And last but not least because it is my favorite way to do this is to use the PUSHD command.
PUSHD Y:\somedirectory
But if you use the PUSHD command you then need to use a POPD command to return to the original directory. Just like I am using in the code.

Windows batch script for searching files based on file name,checks if the files are older than N days, if true, it deletes those files

I am a newbie to windows batch scripting. I have researched through the web and the site and tried out the solutions but none seem to give me the desired results.
This is what I want to achieve:
Search for files in a folder using a specific filename
show found files
Check the found files if they are older than 1 day
If true,delete those files
Else return message(Found files not older than 1 day)
From my research I was able to write a batch code that searches for file using a string, but unable to do step 2,3,4 and 5.
Kindly assist.
Here is my batch code:
#echo off & setlocal
set "MySearchString=Scheduled_error"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"
Seems like a perfect task for FORFILES!
forfiles /p c:\SomePath\ /s /m *.* /c "cmd /c echo #path" will show you all files older than one day. You can modify the filter replacing *.* with the file name you are looking for.
To delete these files simply replace echo #path with del /y #path and add the /d -1 parameter:
forfiles /p c:\SomePath /s /m *.* /d -1 /c "cmd /c del /y #path"
The age of the files to delete is specified with the /d -1 switch where -1 means 1 day and older.

.Bat File - deleting 4 days old backup if atleast 3 days old file already exist

This is my first post & take in mind my native language isn't English.
.bat file so far:
C:
Cd\
forfiles /p Z:\SQL /s /m *.bak /d -4 /c "cmd /c del /q #path"
forfiles /p Z:\SQL /s /m *.trn /d -4 /c "cmd /c del /q #path"
What I want with this file:
I want it to delete 4 days old backup & log files. This .Bat file works with that - the risk comes now - if the backup/log program for some reason stop and the person in charge is sick for 4 days - the .bat file will delete the only valid backup I have.
So what I ask for:
Is there someone out there that knows a way to switch in the .bat file so that it looks for at least 3 days worth of files - NOTE Not just 3 files, but it has to be from 3 previous days in a row.
Example: The system takes backup Monday (1), system crashes Thursday (2). When the scheduler runs the backup at Tuesday (4) it will just delete the Monday backup (which is my only valid one) because its 4 days old.
That's where it comes in that I want it to check: Do I have any other .bak/.trn files the last 3 days Yes/No?
No = Don't delete.
Yes = Delete everything over 4 days old.
Hope I have explained myself somewhat understandably.
Thanks for reading, hope you can help me out!
You can count them ahead of time to make the decisions. Here's one way to do it. Unfortunately FORFILES doesn't allow you to easily find files newer than 4 days ago, so we find the older ones and subtract from the total.
Loop through the files and add the ones that match your deletion criteria to a text file:
forfiles /p Z:\SQL /s /m *.bak /d -4 /c "CMD /c ECHO #fname" > BAKfiles.tmp
forfiles /p Z:\SQL /s /m *.trn /d -4 /c "CMD /c ECHO #fname" > TRNfiles.tmp
Next, use the FIND /c command to count the lines that have quotation marks in them, and use FOR /F to parse the total from the output after the colon. You need to do this for each file type, but I'm only showing one for the general form.
FOR /F %%C IN ('dir *.BAK^| find "File(s)"') do SET BAKTOTAL=%%C
FOR /F %%C IN ('dir *.TRN^| find "File(s)"') do SET TRNTOTAL=%%C
FOR /F "tokens=2* delims=:" %%C IN ('cmd /c find /c """" BAKfiles.tmp') DO SET BAKDEL=%%C
FOR /F "tokens=2* delims=:" %%C IN ('cmd /c find /c """" TRNfiles.tmp') DO SET TRNDEL=%%C
SET /A BAKCOUNT=%BAKTOTAL% - %BAKDEL%
SET /A TRNCOUNT=%TRNTOTAL% - %TRNDEL%
Then evaluate the count with IF to see if you have enough files to meet your threshold, and run your deletion commands.
IF %BAKCOUNT% GEQ 3 IF %TRNCOUNT% GEQ 3 GOTO DELETEFILES
ECHO Not enough files were found. Skipping deletion.
GOTO ENDING
:DELETEFILES
forfiles /p Z:\SQL /s /m *.bak /d -4 /c "cmd /c del /q #path"
forfiles /p Z:\SQL /s /m *.trn /d -4 /c "cmd /c del /q #path"
:ENDING
del /q BAKfiles.tmp
del /q TRNfiles.tmp

Batch command to delete folders with specific name and age

I already have a script (see below) to recursively delete files with a specific extensions and older than x number of days.
I'm looking for the command to recursively delete empty folders where the name begins with { and ends with } and is older than x number of days. Any ideas?
set deletepath="C:\test"
set days=10
for %G in (.log, .dat, .sts, .mdn, .req, .err, .edi, .xml.filename, .xml) do FORFILES /P %deletepath% /S /M *%G /D -%days% /C "cmd /c del #path"
This should work. Test it on sample folders first.
FORFILES /P %deletepath% /S /M "{*}" /D -%days% /C "cmd /c if #isdir==TRUE rd #path 2>nul"
It will only remove the folder if it is empty - the 2>nul removes harmless error messages when there are files inside the folder.

How do I delete directories for a specific time period (say older than one month) using dos command?

I am trying to delete directories older than one month from a batch file.
I am able to list directory names using:
dir D:* /A:D-H
But not sure how can I delete them for a specific time.
Can somebody help me with this?
Use forfiles command.
Syntax:
FORFILES [/p Path] [/m Mask] [/s] [/c Command] [/d [+ | -] {dd/MM/yyyy | dd}]
The following command
forfiles /p "C:\source_folder" /s /m *.* /c "cmd /c Del #path" /d -30 will erase files in c:\source_folder older than 30 days.
See more examples and explications here
SET FOLDERPATH="F:\TestEnvir\bin\backup\MarketData\D"
rem FORFILES /p %FOLDERPATH% /S /D 30 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"

Resources