0

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

  • 1
    The second `FOR` command variable is `%%i`. Why aren't you using that with the `DEL` command? – Squashman Mar 28 '18 at 17:53
  • 1
    Also if you read the help file for the `FINDSTR` command your search string is really 4 search strings. If you need it as one search string then you need to use the `/C` option with `FINDSTR`. – Squashman Mar 28 '18 at 17:57
  • 1
    You should consider using the `FORFILES` command directly to get the files older than 10 days and then the command you execute could use the FINDSTR logic you have above with conditional execution to delete the file. – Squashman Mar 28 '18 at 18:02
  • Hi Squashman, I am somewhat amateur at writing batch files. I get few opportunities to do so. I appreciate the suggestions. My intent with FINDSTR is to use the four search strings (in my process I will be entering multiple different files names to keep as they are logs). Thanks – Cory Gillin Mar 28 '18 at 18:20
  • Take a look at this thread: [Batch file to delete files older than N days](https://stackoverflow.com/q/51054). – aschipfl Mar 28 '18 at 18:54

2 Answers2

0

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.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • Hi Bill,That looks interesting. I will try that as well. Thank you for the suggestion, never worked in powershell before. – Cory Gillin Mar 28 '18 at 18:28
  • This code removes files whose _content_ does not contain the specified string. If you want to exclude files whose _names_ do not contain strings, you would use the `-Exclude` parameter of `Get-ChildItem` instead of using `Select-String`. – Bill_Stewart Mar 30 '18 at 14:41
  • Hi Bill, Thanks again. Because of the requirements to remove some files that are nn Days old and others that are n Days old I did this: Get-ChildItem "C:\Users\jhenry\Documents\TEMP\Junk2\*" -exclude *.log, *Batch.txt -recurse| ForEach-Object { if ($_.LastWriteTime -lt (Get-Date).AddDays(-10)) { Remove-Item $_ } } Get-ChildItem "C:\Users\jhenry\Documents\TEMP\Junk2\*" -include *Batch-Filtered-Messages*, MTMSM* -recurse| ForEach-Object { if ($_.LastWriteTime -lt (Get-Date).AddDays(-5)) { Remove-Item $_ } } – Cory Gillin Apr 03 '18 at 18:59
0

This batch code below is not an answer, just a modification to what you've posted.

@Echo Off
Set "MyTargetDir=%UserProfile%\Documents\TEMP\Junk"
Set "MySearchStrings=123 abc nyu texas"
CD /D "%MyTargetDir%" 2>Nul || Exit /B
For /F "Delims=" %%A In ('Dir /B/A-D-S-L^|FindStr /IV "%MySearchStrings%"'
) Do Echo %%A
Pause

For the remaining part of your task, you should obviously modify the Echo command, to either Call a label with your specific code or run command directly; using %%A. If there are many files in the target directory, you may even want to do the last written, or date created check first. I cannot currently help you with that because you haven't attempted it yourself, (this isn't a code to order service), and you've provided insufficient information about what you mean by 10 days old.

For determining the file age I'd suggest using RoboCopy or ForFiles as alternatives. There are many examples of doing so on this site; please read through some answers and attempt to modify them to your specific requirements, updating your question accordingly if you do not achieve your goal.

Because you are an 'amateur' at batch files, you may be better advised to learn and use PowerShell, which could perform this task much better, instead.

Here's a possible PowerShell example:

Get-ChildItem '$Env:UserProfile\Documents\TEMP\Junk\*.*' -Exclude *123*, *abc*, *nyu*, *texas* |
 Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } | Remove-Item -WhatIf
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Hi Compo, I agree this isn't a coding service. Oddly, I managed to put together similar code to your powershell code (I'm pretty sure you did it in a much more efficient manner than I, esp being VERY new to powershell). The -exclude does a good job of letting me expand out the excluded content (as that will change). I appreciate the confirmation and advice. – Cory Gillin Mar 29 '18 at 18:58
  • When you put in your question "files that do NOT contain a string," I assumed you meant file _content_ (that's what "contain" usually means), not file _names_. Those are two different concepts. – Bill_Stewart Mar 30 '18 at 14:22