Sarch - execute programs using times and dates incl. offset in your parameters

Submit command line tools that you find here.
Post Reply
Message
Author
User avatar
lintalist
Posts: 317
Joined: Sat Apr 19, 2014 12:52 am
Contact:

Sarch - execute programs using times and dates incl. offset in your parameters

#1 Post by lintalist » Tue Nov 03, 2020 1:16 pm

Echodate reminded me of Sarch by AnalogX - various other softwares available at https://analogx.com/ (software in the menu)

Sarch allows you to archive logs, delete files using times and dates in your wildcards, and more. Sarch is basically a command executer with a limited number of variables that can be output as parameters.

Website: https://analogx.com/contents/download/S ... eeware.htm
Download: https://analogx.com/files/sarch.zip
Documentation (copied below): https://analogx.com/contents/download/S ... tation.htm

Sarch is like most command line utilities out there; from the command prompt you can type the name by itself to get a list of options:

Code: Select all

Usage:   Sarch [options]
Options:   /TARGET [path]      Destination for files
           /SOURCE [path]      Source for files (def: current)
           /FILENAME [name]    Destination filename
           /WILDCARD [match]   String to use to find matches (ie; ex12*.log)
           /OFFSET [time]      Time to offset (ie; 1d=1day, 10h=10hours)
           /EXECUTE [...]      Command to execute
Here's a brief description of each option:
  • TARGET This is the path where you want the files to be copied to. It
    supports both relative (..\) paths or literal paths (C:\). I
    would recommend using literal whenever possible to eliminate
    any possible ambiguity, unless you are comfortable with using
    relative paths.
  • SOURCE This is the directory it is going to be working in. If no
    source directory is specified, then it will use the current
    mounted path.
  • FILENAME This specifies the destination filename - this is purely
    optional, but can sometimes help to make things a bit more
    intuitive when you are using the same filenames for multiple
    operations.
  • WILDCARD This is another optional option (imagine that). As with the
    FILENAME, it's purpose is to help clean up multiple use
    operations.
  • OFFSET This is the amount of time to offset the current time by.
    Different units may be used (h = hour, d = day, m = month,
    y = year), and mixed units may be specified, so 1d5h is a
    valid offset.
  • EXECUTE This is the actual string that will be executed.
Now, before we move on to some examples of how you use this program, we need to cover what variables are an how they're used. The primary purpose of this program is to help in using time values in execution strings, so almost all the variables are time-related. Let's assume you have a variable named HOUR that's set to the number 5 (for 5 hours), you would execute this:

echo {HOUR}

which would in turn execute this:

echo 5

See? The {HOUR} is translated into whatever it is set to before it does anything. It's also important to point out that all variables MUST start with a { and end with a }. Also, while they are not case sensitive, they must be punctuated exactly as noted in these docs. Here's the list of variables supported:
  • Time.Month Displays the month in numeric form, 1-12
  • Time.MonthPad Same as Time.Month, but will always be two digits,
    so 03 instead of 3.
  • Time.Day Displays the day in numeric form, 1-32
  • Time.DayPad Same as Time.Day, but will always be two digits,
    so 09 instead of 9.
  • Time.Year2 Displays the year in two digit for (2001 = 01)
  • Time.Year4 Displays the year in numeric form
  • Config.Filename The processed filename
  • Config.Source The processed source directory
  • Config.Target The processed target directory
  • Config.Wildcard The processed wildcard specification
So let's use this in an example. Let's say that you're running IIS with logs turned on, and set to be created weekly (so you end up with 4 or 5 logs per month. Normally IIS names it's logs something like 'ex010704.log', which means extended (EX) 2001 (01) July (07) fourth week (04), got it? So what we want to do is to zip these up using WinZip and put them in another directory:

Code: Select all

sarch /source "C:\Logs\" /target "{Config.Source}archive\{Time.Year4}\" /wildcard "ex{Time.Year2}{Time.MonthPad}??.log" /OFFSET -1m /filename "{Time.MonthPad}{Time.Year4}.zip" /execute "C:\WinZip\wzzip -ex {Config.Target}{Config.Filename} {Config.Source}{Config.Wildcard}"
sarch /source "C:\Logs\" /target "{Config.Source}archive\{Time.Year4}\" /wildcard "ex{Time.Year2}{Time.MonthPad}??.log" /OFFSET -1m /filename "{Time.MonthPad}{Time.Year4}.zip" /execute "del {Config.Source}{Config.Wildcard}"
WHEW! Those are some long commands! So here's what we're basically saying; Look inside the C:\Logs directory, for any files that start off with ex and the current year and month, minus one month. Notice we pick Year2 since we need a two digit year, and MonthPad since we need to ensure the month is always two digits as well. The ?? simply tells DOS to match any two characters. This then get's translated into the WinZip string (make sure you have the WinZip command line option installed), which is output into the directory C:\Logs\archive\2001\ (assuming the year is 2001). The second command is exactly the same as the first, but instead of executing WinZip, it executes the DOS command DEL to delete the files which were compressed.

If you need to enclose something in quotes, such as a path with spaces in it, and use it as a parameter, then you must enclose it in double quotes:

BAD: sarch /execute "C:\WinZip\wzzip -ex C:\Program Files\Whatever.exe"
GOOD: sarch /execute "C:\WinZip\wzzip -ex ""C:\Program Files\Whatever.exe"""

These 'escaped' quotes will be passed through the program.

If for some reason either the source or destination directories do not exist, the program will create them before executing the command. This is to help ensure that the executed command doesn't fail because it doesn't gracefully handle non-existent directories.
Last edited by lintalist on Wed Nov 04, 2020 10:08 am, edited 1 time in total.

User avatar
vevy
Posts: 752
Joined: Tue Sep 10, 2019 11:17 am

Re: Sarch - execute programs using times and dates incl. offset in your wildcards

#2 Post by vevy » Tue Nov 03, 2020 1:58 pm

Thanks for the tool and the site.

I know they may sound weird coming from me, but what problem was this tool trying to solve?

Don't get me wrong; one more tool in the arsenal. A choice that may prove itself later.

It is just that the length of the commands and the learning curve beg the question of why not just use a fairly simple batch script? :?

Honest question.
Help make the comprehensive CLI database happen:
                    Vote for filters/badges!

User avatar
lintalist
Posts: 317
Joined: Sat Apr 19, 2014 12:52 am
Contact:

Re: Sarch - execute programs using times and dates incl. offset in your wildcards

#3 Post by lintalist » Wed Nov 04, 2020 8:53 am

The idea is to indeed use it in a batch script :)

Think of any cli app where a date format is a parameter, for example xcopy with /d (date) parameter. (Or zip or...)

I've used sarch to "always xcopy the files from the last 5 days" to make a backup at the end of the day on Friday. So you can use sarch to offset todays date with 5 days and bob's your uncle, you never have to think "what is today's date" - or zip all files from this month or 3 days ago. Throw in a additional option to the batch file (the offset) and you have a pretty flexible solution with minimal effort.

It can all be done using standard dos commands, but date math in batch isn't really easy and usually takes several lines and remains (to me) rather cryptic, here it remains all on one line with some very simple to understand variables e.g.

{Time.DayPad}{Time.MonthPad}{Time.Year}

looks better to me as

%DATE:~7,2%%DATE:~4,2%%DATE:~-4%

and again, offset for the win here if you ask me

User avatar
vevy
Posts: 752
Joined: Tue Sep 10, 2019 11:17 am

Re: Sarch - execute programs using times and dates incl. offset in your wildcards

#4 Post by vevy » Wed Nov 04, 2020 9:12 am

I guess you have a point. People approach things differently.
Help make the comprehensive CLI database happen:
                    Vote for filters/badges!


Post Reply