Batch command to delete folders with specific name and age - windows

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.

Related

I want to delete a stale files which is >365 days along with print the list of files are deleted in cmd via batchfile(.bat)

forfiles /p "C:\Testing" /s /m *.* /D -365 /C "cmd /c del #path"
above code just deleting the files which is >365 and didn't show/print anything

Delete a folder with file xy.z that is older then X days

I want to delete all folders with an old file.
I try to solve it with forfiles to find my old files and remove the whole folder.
The problem i get is, when i found the file forfiles is in the folder what i want to delete and i get the "acess denied, is being used by another process" error.
forfiles /p "C:\Users\abc\Desktop\tmp\test" /s /m xy.z /d -1 /C "cmd /V:on /C if #isdir==FALSE set tmpPath=#path&set tmpPath=!tmpPath:xy.z=!&cmd /c rd /s /q %tmpPath% "

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.

Batch file to recursively delete files in a folder older than N number of days

I'm using a batch file now to delete all files ending in .snp that are older than 180 days. The code below works to delete all files ending in .snp under the root folder
C:\Program Files\Snapshots
But I recently discovered that within the Snapshots folder there are folders organized by date
"1-10-2014, 12-20-2014, 10-15-2014 etc.."
and that the below line of code doesn't work to recursively search through each directory and is therefore not deleting.
What changes should I make to this code to have it recursively search through folders within a root folder and delete files that are greater than 180 days?
forfiles /M *.snp /P "C:\Program Files\Snapshots" /S /D -180 /C "cmd /c del /F /Q #path"
Without the /D (Date) it workes for sub-folders
forfiles /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q #path
but you obviously want the date to be there
then in CMD
forfiles /D -180 /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q #path
The /D before the Pathname selects all files that have been changed more than 180 days ago
The best option for highest reliability is to combine the strengths of the For command with the FORFILES commands to allow each one to do what they do best.
Set str_Ext=*.snp
Set int_Age=-180
For /R "%~dp0" %%D IN (.) DO (
For /F "usebackq tokens=*" %%F IN (`FORFILES /P "%%~D" /m %str_Ext% /D %int_Age% 2^>nul`) DO (
Call :s_Del_File "%%~D" "%%~F"
)
)
Goto :EOF
:s_Del_File
Set "str_DIR=%~1"
Set "str_FIL=%~2"
Set "str_DIR=%str_DIR:~0,-1%"
DEL /F/Q/A "%str_DIR%%str_FIL%"
Goto :EOF
Within the second FOR command, the backquote (~ key) contains the FORFILES command and uses the console output to call a batch subroutine to delete the specified file.
Spaces in folder and file names will not slow this beast down, and the double quotes ["] around the Set commands will allow the process to work with folders and files that have parentheses in them or other exotic, but allowable characters.

Delete file longer then 30 days old for long path and long file name

I tried to make batch file in windows by using task schedule once a month to find *.bak. With condition more then 30 old. I create two condition full path name and non-8dot3 path name. For those unable to delete it be recorded to a TXT file.
Here the command I found:
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /C del #path"
Here is my command for delete using non-8dot3 file names:
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /C for %A in (#path) do #echo del %~sA
For file can't be delete because it to long path and file name, it recorded to a TXT file with long full path file name and non-8dot3:
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /c echo #path >> list.txt"
forfiles /P E:\WP /S /M *.bak /D -30 /C "cmd /c for %A in (#path) do #echo %~sA" >> list.txt
These 4 command are working well if I just copy and paste into dos command prompt.
But when I put them into batch file e.g. "del30days.bat", it does not working for non-8dot3 file names:
#echo off
:: Set host computer name
set host=%COMPUTERNAME%
:: Set save list path
set list_path=F:\LogFiles
:: Set min age of files and folders to delete
set file_list=ListCantDeleted-%host%-%date:~10,4%-%date:~7,2%-%date:~4,2%.txt
:: Set target folder path
set target_path=E:\WP
:: Set min age of files and folders to delete
set max_days=30
:: Set what kind files or extension
set file_ext=*.bak
:: Delete files from target path
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C del #path"
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C for %A in (#path) do #echo del %~sA"
:: Record files from target path
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /c echo #path >> %list_path%\%file_list%"
forfiles /P %target_path% /S /M %file_ext% /D -%max_days% /C "cmd /C for %A in (#path) do #echo %~sA >> %list_path%\%file_list%"
I got this error on non-8dot3 files name but not on long file name on record to a TXT file, by mean it can't delete file using non-8dot3 file names as well.
~sA" was unexpected at this time.
Any reason why?
The goal I create this is to delete *.bak file more then 30 days without any issue on long path and file name. Anyone got simplify solution for this?
You're going to slap yourself. The problem is that in a batch file, for loop variables need a double percent. %%A and %%~sA.

Resources