Multi Commander Support Forum

Multi Commander => Script => Topic started by: Herrie on August 19, 2016, 13:57:54

Title: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Herrie on August 19, 2016, 13:57:54
Hello guys,

I am a bit familiar to vba scripting. What I would like to see/accomplished is that approx. 500 files are rearanged into specific (sub)folders named with the weeknumber of the files.
Example:
NOW:
file[whatever]{date = 01-01-2016}  /week 1
file[whatever]{date = 08-01-2016}  /week 2
file[whatever]{date = 15-01-2016}  /week 3
file[whatever]{date = 01-22-2016}  /week 4
file[whatever]{date = 01-29-2016}  /week 6
etc.

NEW situation
MainFolder/Week_1/file[whatever]{date = 01-01-2016}
MainFolder/Week_1/file[whatever]{date = 02-01-2016}
MainFolder/Week_1/file[whatever]{date = 03-01-2016}
MainFolder/Week_1/file[whatever]{date = 04-01-2016}

So, I need to calculate the weeknumber of each file,
use this number to:
Create a new folder with the name"Week" &[weeknumber] /one time operation for files concerned
Move the files within this weeknumber period to the newly created folder.

Jumping through the whole list of files in the [main] folder.

Q:
is it possible?
How to accomplish?

Any help would be appreciated.

Perhaps it might be an extra 'tool' as well for MC
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Mathias (Author) on August 19, 2016, 16:07:10
There is no script function that will give you the weeknum so you have to calculate that your self and that will not be easy.
The date formatting function only allows for formatting dates d/m/y in different ways.

If if you solve that it should work, You can create folders and copy files from script
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Herrie on August 20, 2016, 18:24:16
Afterall I simplpy did the operation by hand.
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Herrie on March 27, 2017, 18:55:13
At this moment (27-03-2017) I am back to a situation as I started with.
Mathias' suggestion did not really help me.
How can  I calculate the weeknumber?

Any simple idea to script both the 'Move' action and the 'weeknum' calculation?
I would be very pleased
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: AlanJB on March 28, 2017, 18:26:55
Herrie,
  you say that you have some scripting experience.

If so, this may be of some help:
http://www.dostips.com/forum/viewtopic.php?t=7125 (http://www.dostips.com/forum/viewtopic.php?t=7125)

Also, are you trying to convert the file date to the ISO week number (where week 1 is the first week containing Jan 4th), or the American system (where Jan 1st is the beginning of week 1), or something else...?
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Jungle on April 10, 2017, 18:42:34
Here comes The Magic! :) Below is the script function calculating week number in accordance with ISO 8601. The code was taken here (https://forums.ni.com/t5/Example-Program-Drafts/Calculating-week-number-according-to-ISO8601/ta-p/3495098) and converted to MultiScript. MC v7.x is required.

I haven't done much testing, but results for all days in 2001-2010 are the same as in Excel.

Code: [Select]
function IsLeapYear( $year )
{
  return IsAnyTrue( IsAllTrue( mod( $year, 4 ) == 0, !( mod( $year, 100 ) == 0 ) ), mod( $year, 400 ) == 0 );
}

function WeekNumIso8601( $day, $month, $year )
{
  @var  $YY, $C, $G, $I, $J, $DayOfYearNumber, $Jan1Weekday, $Weekday, $YearNumber;
  @var  $LeapYear = 0, $precLeapYear = 0, $WeekNumber = 0;
  @var  $Mnth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };

  // 1. Find if Y is LeapYear
  if ( IsLeapYear( $year ) )
  {
    $LeapYear = 1;
  }

  // 2. Find if Y-1 is LeapYear
  if ( IsLeapYear( $year - 1 ) )
  {
    $precLeapYear = 1;
  }

  //3. Find the DayOfYearNumber for Y M D
  $DayOfYearNumber = ( $day ) + ( $Mnth[ $month - 1 ] );
  if ( IsAllTrue( $LeapYear > 0 , $month > 2 ) )
  {
    $DayOfYearNumber++;
  }

  // 4. Find the Jan1Weekday for Y (Monday=1, Sunday=7)
  $YY = mod( $year - 1, 100 );
  $C = ( $year - 1 ) - $YY;
  $G = $YY + ( $YY / 4 );
  $I = mod( $C / 100, 4 );
  $Jan1Weekday = 1 + mod( ( $I * 5 ) + $G, 7 );

  // 5. Find the Weekday for Y M D
  $I = $DayOfYearNumber + ( $Jan1Weekday - 1 );
  $Weekday = 1 + mod( $I - 1, 7 );

  if ( $Weekday == 0 )
  {
    $Weekday = 7;
  }

  // 6. Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53
  if ( IsAllTrue( $DayOfYearNumber <= ( 8 - $Jan1Weekday ), $Jan1Weekday > 4 ) )
  {
    $YearNumber = $year - 1;
   
    if ( IsAnyTrue( $Jan1Weekday == 5, IsAllTrue( $Jan1Weekday == 6, $precLeapYear == 1 ) ) )
    {
      $WeekNumber = 53;
    }
    else
    {
      $WeekNumber = 52;
    }
  }
  else
  {
    $YearNumber = $year;
  }

  // 7. Find if Y M D falls in YearNumber Y+1, WeekNumber 1
  if ( $YearNumber == $year )
  {
    if ( $LeapYear == 1 )
    {
      $I = 366;
    }
    else
    {
      $I = 365;
    }
    if ( ( $I - $DayOfYearNumber ) < ( 4 - $Weekday ) )
    {
      $YearNumber = $year + 1;
      $WeekNumber = 1;
    }
  }

  // 8. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
  if ( $YearNumber == $year )
  {
    $J = $DayOfYearNumber + ( 7 - $Weekday ) + ( $Jan1Weekday - 1 );
    $WeekNumber = $J / 7;

    if ( $Jan1Weekday > 4 )
    {
      $WeekNumber--;
    }
  }
 
  return $WeekNumber;
}
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Mathias (Author) on April 10, 2017, 22:59:53
Here comes The Magic! :) Below is the script function calculating week number in accordance with ISO 8601. The code was taken here (https://forums.ni.com/t5/Example-Program-Drafts/Calculating-week-number-according-to-ISO8601/ta-p/3495098) and converted to MultiScript. MC v7.x is required.

I haven't done much testing, but results for all days in 2001-2010 are the same as in Excel.


Wow I'm impressed, You really use the script engine more than I every indented it for.
Not sure I would have the patience to write all that. very cool
Title: Re: Script to rearrange files based on weeknumber into folders with weeknumber
Post by: Herrie on July 09, 2017, 15:25:01
Although the function  looks K, I still wonder how to embed it in a script that:

runs through all files in a folder, moives them to a new folder, created beforehand
e.g.
:mainFolder\ file_x.txt date= 10-10-2017
should land in folder w41 (being the weeknumber)
OR
run through the filelist in a folder and ADD the weeknumber attribute (?) to the file