Filter on weekday

Post Reply
Jarmer
Newbie
Posts: 12
Joined: Mon Feb 23, 2015 3:47 pm

Filter on weekday

Post by Jarmer »

Hi

How can I make a flow with one input folder and 7 output folders, the files that comes true the flow must output in a current name of the day.

Files comming in on Monday lands in Monday folder and files enters Tuesday coming Tuesday folder etc.

René
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Filter on weekday

Post by gabrielp »

Hi Jarmer,
It would be nice if Switch's Switch.Date supported Week Dates, but it does not. That would give you a number from 1-7 corresponding to Monday-Sunday.

So I think what I would do to make this work is to write a script to resolve the current day of the week using the Date() class with the getDay() method and write that to private data.

Code: Select all

var d = new Date();
var day_of_the_week = d.getDay(); 
If you do not have access to the scripting module, then perhaps you can using the "Execute command" flow element to run something like this which will return the full day of the week:

Code: Select all

date '+%A'
You could then route your files to the 7 folders based on that response.

Please refrain from posting duplicate questions on the forum.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
Jarmer
Newbie
Posts: 12
Joined: Mon Feb 23, 2015 3:47 pm

Re: Filter on weekday

Post by Jarmer »

Hi Gabrielp,

I'm not that good to script, but I have treid this, unsuccessfully, can you see the error ?:

var d = new Date();
var day_of_the_week = d.getDay();


var day_of_the_weekValue = 0;

{
switch (day_of_the_week) {


case "Monday":
day_of_the_weekValue =1;
break;



default:



day_of_the_weekValue;

}
}

day_of_the_weekValue;
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Filter on weekday

Post by gabrielp »

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var d = new Date();
	var day_of_the_week = d.getDay();
	
	job.setPrivateData( 'ProccessWeekDate', day_of_the_week );
	
	job.sendToSingle( job.getPath() );
}
Use the script above and have it go to an output folder. Now in your output folder, your jobs will have a private data key of "ProcessWeekDate" as the week date. So you can set connections out of this outgoing folder to go to the Monday folder if ProcessWeekDate == 1, Tuesday if ProcessWeekDate = 2, etc...
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
Jarmer
Newbie
Posts: 12
Joined: Mon Feb 23, 2015 3:47 pm

Re: Filter on weekday

Post by Jarmer »

Hi Again,

I still have problems with this, I can not get it to work.

But I have found another solution.

I will use the "Set hierarchy parth", with "single-line text with variables defined", using " [Switch.Date:Format="dddd",TimeZone="UTC"].

This will give me a weekday in "Archive hierachy".
lombert
Member
Posts: 167
Joined: Fri Feb 04, 2011 2:31 pm
Location: Sweden

Re: Filter on weekday

Post by lombert »

Jarmer wrote:Hi Again,

I still have problems with this, I can not get it to work.

But I have found another solution.

I will use the "Set hierarchy parth", with "single-line text with variables defined", using " [Switch.Date:Format="dddd",TimeZone="UTC"].

This will give me a weekday in "Archive hierachy".
Very good to knew!

I have not tested 3 or 4 "d".
3 gives "Fri"
4 gives "Friday"
Between jobs!
User avatar
Terkelsen
Advanced member
Posts: 300
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Filter on weekday

Post by Terkelsen »

That's an excellent workaround to actually get the weekday. It even returns the name of the day depending on the language your system is running, so I get the names in Danish :-)

I have another case, where I need to sort files on week-numbers. ISO 8601 does mention using W to retrieve the week-number, but it doesn't seem to be implemented in Switch.date. Does anyone have a solution for this?
bens
Advanced member
Posts: 253
Joined: Thu Mar 03, 2011 10:13 am

Re: Filter on weekday

Post by bens »

Week numbers are more complicated than you would think. For example, if January 1st is a Friday (as in 2016), then everything up until January 3rd is actually the last week of the previous year. The first week of that year doesn't start until January 4th (the first Monday). But if January 1st is a Thursday (as in 2015), then December 29th, 30th and 31st are all part of week 1. Confused yet? ;-) The rule is actually rather simple: the first week is the one with more than half its days in the new year.

That is the official (ISO) version anyway. Some people want a different algorithm, where the first week of the year is always the first *full* week. And then some people want the week to start on Monday, others on Sunday. It's all a big mess.

So the first thing to do is decide exactly what you want: the official ISO way, or something else. Once you know that, you'll probably need some scripting, because I don't think the built-in date objects in Switch have functionality to get the week number.
User avatar
Terkelsen
Advanced member
Posts: 300
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Filter on weekday

Post by Terkelsen »

I'm fully aware of the different ways of counting week numbers, but since there is an ISO-standard I see no reasons not to stick to that.
bens
Advanced member
Posts: 253
Joined: Thu Mar 03, 2011 10:13 am

Re: Filter on weekday

Post by bens »

Terkelsen wrote:I'm fully aware of the different ways of counting week numbers, but since there is an ISO-standard I see no reasons not to stick to that.
Good to hear; imo too many people cling to their regional traditions instead of adopting a common standard.

Code: Select all

    var theNow = new Date(); // or a test date
    var theYear = theNow.getYear();    
    var theWeek = -1; // initialise to an invalid number
    
    // some helpful "special" days
    var theDec31st = new Date( theYear, 12, 31 );
    var theJan1st = new Date( theYear, 1, 1 );

    // special case for the last 3 days of the last week
    if ( theNow.getMonth() == 12 && theNow.getDate() > 28
         && theDec31st.getDay() < 4 && theNow.getDay() <= theDec31st.getDay() )
    {
        // if Dec 31st is a Monday, Tuesday or Wednesday
        // and today is still in the same week, we're already
        // in the first week of the next year.
        ++theYear;
        theWeek = 1;
    }
    else // all other cases: calculate week number based on days passed since first week
    {
        // special case for the first 3 days of the first week
        if ( theNow.getMonth() == 1 && theNow.getDate() < 4 )
        {
            // if Jan 1st is a Friday, Saturday or Sundsy, and today is
            // still in the same week, we're still in the last week
            // of the previous year.
            if ( theJan1st.getDay() > 4 && theNow.getDay() >= theJan1st.getDay() )
            {
                theNow = new Date( --theYear, 12, 31 );
                // also fix the Jan 1st that we use as reference
                theJan1st = new Date( theYear, 1, 1 );
            }
        }
        // the actual calculation: the number of days between today and Jan 1st,
        // divided by 7, and rounded up.
        theWeek = Math.ceil((1+(theNow.getTime() - theJan1st.getTime())/1000/60/60/24)/7);
    }
Post Reply