0

I am using this command via ansible to delete old files

win_shell: forfiles -p C:\Logs\ -s -m *.log* /D -15 /C "cmd /c del /q @path"

How do I make sure to run this only if there is any file matching criteria

Found this similar question but it suppresses error instead of handling it proper way. i.e. only delete if exists.

Suppress FORFILES "no files found" error

Cannon
  • 2,725
  • 10
  • 45
  • 86
  • Why did you tag `PowerShell`? – iRon Jun 07 '21 at 16:22
  • You don't. You use `win_find` to get the list of files you want to delete, and `win_file` to delete them. – Jack Jun 07 '21 at 16:23
  • win_find fails when you have too many files or file name is too big. – Cannon Jun 07 '21 at 16:26
  • Add a task before this task that runs a find and registers the output in a variable. Then set a when condition on this task dependent on the registered variable not being null. – Colyn1337 Jun 07 '21 at 16:51
  • @Cannon Then just suppress the error, and perhaps add a `failed_when` to the task if you like. – Jack Jun 07 '21 at 17:01

0 Answers0