How to split a array to several based on create date and modified date of .MTS video files

102 views Asked by At

i want to make automatic create folder based on Regularity of current fodler's MTS video files.

i could get some right info by declare -a CreateArray10digits=$(stat -f "%B" * | cut -c1-10) declare -a ModifiedArray10digits=$(stat -f "%m" * | cut -c1-10)

junked file have difference 0second or 1second.

so finally clip-1 folder : 00000.MTS 00001.MTS 00002.MTS 00003.MTS 00004.MTS 00005.MTS 00006.MTS 00007.MTS 00008.MTS 00009.MTS 00010.MTS 00011.MTS

clip-2 folder :00012.MTS

clip-3 folder :00013.MTS 00014.MTS

clip-4 folder :00015.MTS

clip-5 fodler :00016.MTS

if there are no Regularity on each MTS file in current folder, then just make clip-1 to clip-16 and move to each folder .. that's it

what will be best script for bash shell on a mac ?

================ here my trying code ==============

# first , get files list as array based on Create-date time(seconds base) for current directory for MTS files

declare -a  CreateArray10digits=$(stat -f "%B" *  | cut -c1-10)

# make new array for CreateArray10digits

indexOfcreateArray10digits=($CreateArray10digits)

# seconds, get files list as array based on Modified-date time for current directory for .MTS files

declare -a  ModifiedArray10digits=$(stat -f "%m" * | cut -c1-10)

# make new array for ModifiedArray10digits

indexOfmodifiedArray10digits=($ModifiedArray10digits)

# and get the array's length for CreateArray10digits

indexOfcreateArray10digitsLenth=${#indexOfcreateArray10digits[*]}

indexOfmodifiedArray10digitsLenth=${#indexOfmodifiedArray10digits[*]}

# make new array for filename list of current directory

declare -a  ArrayFullFileNameWithExtension=$(stat -f "%N" *)
indexOfModifiedArrayFullFileNameWithExtension=($ArrayFullFileNameWithExtension)

declare -a  ArrayFullFileNameWithoutExtension=$(stat -f "%N" * | cut -c1-5 )
indexOfModifiedArrayFullFileNameWithoutExtension=($ArrayFullFileNameWithoutExtension)

finally , i made severals arrays and these array finally have same length

my code below

CNT=$((${#indexOfcreateArray10digits[*]}-1)) # 17-1 = 16
last_file_index=$CNT
current_dir=1
make_clip_dir $current_dir
move_clip 0 $current_dir  # what mean 0 ?  00000.MTS ? 


# when not junked then will be make new folder by for - loop
function make_clip_dir {
    mkdir "clip-$1"
}

# is this move_clip function right ? 
function move_clip {
   mv ${indexOfModifiedArrayFullFileNameWithExtension[$1]} "./clip-$2"
}

# please check below code is right or not .. very i am confused :-) 
    function connected_junks {
    if [[ $((${indexOfcreateArray10digits[$2]} - ${indexOfmodifiedArray10digits[$1]})) -eq 0 ]];
    then

        #echo "$variable1"
        #echo "$variable2"
        echo " true !!"

    elif [[ $((${indexOfcreateArray10digits[$2]} - ${indexOfmodifiedArray10digits[$1]})) -eq 1 ]];
    then

        echo " true !!"


    else
        echo " false!!!!!!!!!!!!!!!!!!!!! "

    fi
}
echo "difference:"$((${indexOfcreateArray10digits[14]} - ${indexOfmodifiedArray10digits[13]}))
connected_junks  14 13
echo "1:"${indexOfcreateArray10digits[14]}
echo "0:"${indexOfmodifiedArray10digits[13]}
=============== results 
difference:1
 false!!!!!!!!!!!!!!!!!!!!!  # i have no idea why this is false 
1:1524058477
0:1524058395
============================================================






# for loop statement 
for i in seq 1 $last_file_index
do
    if [[ ! connected_junks $((i-1)) $i ]]; 
    then
        current_dir=$((current_dir+1))
        make_clip_dir $current_dir
    fi
    move_clip $i $current_dir 
done

# but won't works 
# only clip-1 folder was created and 00000.MTS moved into this folder 
# 
########################### results error
/twoArrayCompare.sh: line 170: conditional binary operator expected
/twoArrayCompare.sh: line 170: syntax error near `$((i-1))'
/twoArrayCompare.sh: line 170: `    if [[ connected_junks $((i-1)) $i ]];
1

There are 1 answers

19
battlmonstr On BEST ANSWER

You've almost got it on your sketch image. The idea of an algorithm is to follow your red arrows.

At first, you create the first directory and put the first clip file in there.

Then you can iterate from 2nd to last, and if there's an arrow to the previous item, you put that file into the same directory as the previous one. Otherwise if there's no arrow, you make a new directory.

Here's the meat of this program for you:

last_file_index=16

current_dir=1
make_clip_dir $current_dir
move_clip 0 $current_dir

for i in $(seq 1 $last_file_index)
do
    if ! connected_junks $((i-1)) $i
    then
        current_dir=$((current_dir+1))
        make_clip_dir $current_dir
    fi
    move_clip $i $current_dir       
done

You need to add move_clip, make_clip_dir and connected_junks functions to this.

The simplest is make_clip_dir:

function make_clip_dir {
    mkdir "clip-$1"
}

For connected_junks you can utilize your create/modified arrays and some if conditions, and then return 1 or 0.

For move_clip you need to format the index to form a name "000NN.MTS" and mv a source file to the new destination.