0

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 ]];
cool jobs
  • 103
  • 9

1 Answers1

0

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.

battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • thanks for your answer. please read added more detail explanation . and let me get more detail how to do . – cool jobs May 19 '18 at 16:57
  • problem is , 00000.MTS file also possible standalone file , not junked . so maximum standalone MTS file not junked file can be exist as many as array length , in this case have make 16folders and put the each MTS files .. so some complex – cool jobs May 19 '18 at 17:32
  • This case is handled in my solution. Please read it and tell me if you don't understand something, I'll explain. – battlmonstr May 19 '18 at 18:05
  • function make_clip_dir { mkdir "clip-$1" } – cool jobs May 19 '18 at 18:56
  • function move_clip { mv ${indexOfcreateArrayFullFileNameWithExtension[$1]} "clip-$1" } – cool jobs May 19 '18 at 18:56
  • function connected_junks { if [[ $((${indexOfcreateArray10digits[$1]} - ${indexOfmodifiedArray10digits[$2]})) -eq 0 || $((${indexOfcreateArray10digits[$1]} - ${indexOfmodifiedArray10digits[$2]})) -eq 1 ]]; then echo 0 else echo 1 fi } – cool jobs May 19 '18 at 18:56
  • CNT=$((${#indexOfcreateArray10digits[*]})) last_file_index=$CNT 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 – cool jobs May 19 '18 at 18:56
  • i made three function and for loop, but won't works – cool jobs May 19 '18 at 18:57
  • You're in the right direction. From the obvious, try using "return" instead of "echo" in connected_junks like here: https://stackoverflow.com/questions/5431909/returning-a-boolean-from-a-bash-function Also inside move_clip instead of `$(make_clip_dir $2)` just use "clip-$2" Other than that learn how to debug by echo-ing intermediate variables, and post more separate questions on details. – battlmonstr May 19 '18 at 21:02
  • and what this mean move_clip 0 $current_dir ? – cool jobs May 19 '18 at 21:38
  • It moves file with index 0 (thus "00000.MTS") to current_dir=1 at this point, which is "clip-1", so it runs essentially `mv "00000.MTS" "clip-1"`. In general it is supposed to move the file with index $1 into the existing directory with index $2 (the directory is pre-created by make_clip_dir). – battlmonstr May 19 '18 at 21:44
  • Add lines like `echo "$variable1"` to print all your variables (also inside if-s and loops), and then you can find what your problem is. It now all depends on your array data being correct. I can explain how to make a fish rod, but I won't catch fish for you. – battlmonstr May 19 '18 at 21:49
  • if you give me a fish ,then i can study more much things inside of fish :-) – cool jobs May 19 '18 at 22:10
  • very strange , when i check on terminal , then i call echo ${indexOfModifiedArrayFullFileNameWithExtension[1]} then gave me the right "00001.MTS" BUT when i call move_clip 1 1 THEN was moved 00002.MTS into clip-1 folder . – cool jobs May 19 '18 at 22:39
  • Add more echo-s into the function :) – battlmonstr May 19 '18 at 22:42
  • function i have test for connected_junk , it looks like need iterator for array . i have updated question field . i have no idea . for iterator – cool jobs May 19 '18 at 23:44
  • hello teacher. just remained only to implement for loop when i see just make problem on for loop you made :-). – cool jobs May 20 '18 at 05:26
  • hi. I have fixed the loop: it needs `$(...)` around seq, and no need to have `[[...]]` around connected_junks if (this fixes "conditional binary operator expected" error). Also in your code you forgot to add "return 0" (after "echo true") and "return 1" (after "echo false"). See this: https://stackoverflow.com/questions/5431909/returning-a-boolean-from-a-bash-function – battlmonstr May 20 '18 at 11:52
  • NICE ! THANKS YOU TEACHER ! WORKS PERFECT – cool jobs May 20 '18 at 15:50