I am attempting to write a batch file process that will first find all files that Do NOT contain a string (I've got that part) then it will check to determine if the file is older than 10 days (This is my sticking point). I have previously setup deletions to remove files that are 10 days old but I am stuck trying to figure out how to combine these.
@echo off
set "MySearchString=123 abc nyu texas"
for %%a in (C:\Users\xyz\Documents\TEMP\Junk\*.*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i /v "%MySearchString%"') do echo "%%~fa"
The last "echo "%%fa" is where I would expect to put the delete code. Any thoughts on how I could take the results of the string exclusion for the deletion?
Thank you
This is far simpler and more straightforward in PowerShell. Here's one approach:
Get-ChildItem "C:\Users\xyz\Documents\TEMP\Junk\*.*" |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } |
ForEach-Object {
if ( (Select-String "123 abc nyu texas" $_ | Measure-Object).Count -eq 0 ) {
Remove-Item $_ -WhatIf
}
}
You can remove the -WhatIf
parameter to make sure the files to be removed are what you expect.
Keep in mind that Select-String
uses a regular expression unless you tell it not to do so using the -SimpleMatch
parameter.