Negation for Path Condition with Token Text

I have a macro that saves a backup file with today's date in the filename. I'm trying to get this macro to also delete any previous copies of the same backup, ie. files with otherwise the same title but ending in any date other than today's. Is there a way to do negation for a Path condition?

I'd rather not post the macro/path for privacy reasons, but basically what I'm going for is:

After saving

/path/of/backup/file %ICUDateTime%yyyy-MMM-d%.txt

If: At this path: A file exists:

/path/of/backup/file (any date other than today's).txt

Move it to the Trash

Thank you!

Are these the only files in that folder? I ask because I have a similar macro that automatically deletes any files that don’t contain todays date using the ICUDateTime token…

Hey @82Taylor,

Use a For Each action with a Files-In-A-Directory condition.

Split Path action.

If file name starts with <text> then trash.

Or you could use AppleScript:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/12/29 20:52
# dMod: 2021/12/29 20:52 
# Appl: Finder
# Task: Trash Files in a Specific Folder Whose Name Starts with a Given String.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Trash, @Files, @Name, @Starts-With
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

try
   
   set targetFolderPath to "~/test_directory/Keyboard_Maestro_Test_Folder/"
   set targetFolderPath to ((current application's NSString's stringWithString:targetFolderPath)'s stringByExpandingTildeInPath) as text
   set targetFolderAlias to alias POSIX file targetFolderPath
   
   set nameStartString to "Some Test File"
   
   tell application "Finder"
      delete (files of targetFolderAlias whose name starts with nameStartString)
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg with title ¬
               "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

--------------------------------------------------------

-Chris

Interesting– Not the only file in that folder.

So – all you need to do is trash the files whose name doesn't contain todays date in that format?

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/12/29 20:52
# dMod: 2021/12/29 22:45
# Appl: Finder
# Task: Trash Files in a Specific Folder Whose Name Does NOT Contain Today's Date.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Trash, @Files, @Name, @Starts-With
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

try
   
   set targetFolderPath to "~/test_directory/Keyboard_Maestro_Test_Folder/TEMP_DELETE/"
   set targetFolderPath to ((current application's NSString's stringWithString:targetFolderPath)'s stringByExpandingTildeInPath) as text
   set targetFolderAlias to alias POSIX file targetFolderPath
   
   set dateStr to my formatDate:(current date) usingFormat:"y-MMM-d"
   
   tell application "Finder"
      delete (files of targetFolderAlias whose name does not contain dateStr)
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg with title ¬
               "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on formatDate:theDate usingFormat:formatString
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
   theFormatter's setDateFormat:formatString
   set theString to theFormatter's stringFromDate:theDate
   return theString as text
end formatDate:usingFormat:
--------------------------------------------------------

For Keyboard Maestro native actions:

  • Split path.
    • If file name does not contain today's date then delete.

I personally prefer to use AppleScript for this sort of things, because it's undoable.

-Chris

This worked– Thank you!

1 Like