-
Notifications
You must be signed in to change notification settings - Fork 72
/
FC.ahk
2127 lines (1937 loc) · 66.6 KB
/
FC.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
FileEncoding UTF-16
; the constructor is at the very end, out of the way
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; use this to set your own default preferences
GetDefaultPreferences()
{
; only "editor" and "7zip" are working preferences right now.
return Object( "keep_sys", true ; When system files are found in the source list...
; true -> just do it!
; false -> don't touch those!
; "" -> ask me each time
, "merge", true ; When folders might be merged... [NOT YET USED]
; true -> just do it!
; false -> don't touch those!
; "" -> ask me each time
, "7zip", "G:\Workspace\Programs\7-zip\App\7-Zip\7z.exe" ; path to 7z.exe (for 7-zip)
, "catch", false ; set to true to automatically keep track of [NOT YET USED]
; "lost" items during operations. Otherwise,
; you'll have to use .enableCatch() and
; .takeCatch() manually as desired (recommended way)
, "editor", "G:\Workspace\Programs\Notepad++\Notepad++.exe -multiInst") ; path to your favorite text editor with
; desired flags before where the filepath
; will go
}
FC_enableCatch(f)
{
if !f._catch
f._catch := FC("catch")
}
FC_disableCatch(f)
{
f._catch := ""
}
; while catches shouldn't leak all over,
; catchability should spread quickly
FC_takeCatch(f,disable=false)
{
ret := f._catch
if disable
f.disableCatch()
return ret
}
;==== SOURCE PARSING ===================================================================================================
;=======================================================================================================================
FC_list(f, list, delimiters="`n", omit="`r")
{
return FC_listn(f,list,1,delimiters,omit)
}
FC_listn(f,list,n,delimiters="`n", omit="`r")
{
if delimiters contains % SubStr(list,0,1)
list := SubStr(list,1,-1)
Loop %n%
Loop Parse, list, %delimiters%, %omit%
f.add(A_LoopField)
return f
}
FC_array(f, array)
{
Loop % array.MaxIndex()
f.add(array[A_Index])
return f
}
; not satisfied with this yet
; only have to manipulate newly added items
FC_file(f, fpath, fix_dirs=false, cpi=1200) ; 1200 is UTF-16
{ ; rather than mess with line endings here...
FileRead, list, *P%cpi% %fpath%
f := FC_list(f,list)
if fix_dirs
f.manipulate("dir_fixer")
return f
}
; only have to filter newly added items
FC_pattern(f, pattern, folders=0, recurse=0, regexp="")
{
if folders != 2
Loop %pattern%, 0, %recurse% ; get the files first
f.add(A_LoopFileFullPath)
if folders ; then loop through folders (to add a slash)
Loop %pattern%, 2, %recurse%
f.add(A_LoopFileFullPath . "\")
if regexp
f.filterTF("RegExMatch",true,regexp)
return f
}
FC_regex(f,base,regexp, folders=1, recurse=1)
{
return FC_pattern(f,base . "*.*", folders, recurse, regexp)
}
;==== BASIC CONTAINER ==================================================================================================
;=======================================================================================================================
; stuff removed
; remove returns value removed now
FC_exclude(f,indexOrValue)
{
if indexOrValue is not digit
return f.remove(f.find(indexOrValue))
return f.remove(indexOrValue)
}
FC_clear(f)
{
return f.excludeInRange(1)
}
; default to minimize catch duplication
FC_getTemplate(f,takeCatch=false)
{
f_new := FC()
; copy construction parameters
Loop % f._params.MaxIndex()
f_new._params[A_Index] := f._params[A_Index]
; copy preferences
enum := f._prefs.NewEnum()
while enum[key,value]
f_new._prefs[key] := value
if takeCatch
f_new._catch := f.takeCatch()
return f_new
}
; default to minimize catch duplication
FC_getCopy(f,takeCatch=true)
{
if !takeCatch
catch := f.takeCatch()
ret := f.getTemplate().absorb(f)
if !takeCatch
f._catch := catch
return ret
}
FC_IsEqual(f,f2)
{
if f.len() != f2.len()
return false
return ( f.toList()==f2.toList() )
}
; could be more efficient, but really just for testing
FC_IsEquivalent(f,f2)
{
return !( f.len() != f2.len() or f.getWhereNotIn(f2).len() )
}
;==== SORTING ==========================================================================================================
;=======================================================================================================================
FC_sort(f,method="depthsort",f2="")
{
if IsObject(f2)
order := f.getOrder()
split := f.getSplit(method)
f.clear()
enum := split._NewEnum()
while enum[key,value]
f.absorb(value)
if IsObject(f2)
return f.updateLinked(f2,order)
return f
}
FC_rsort(f)
{
return f.sort("depthrsort")
}
FC_sortLinked(f,f2)
{
return f.sort("depthsort",f2)
}
FC_rsortLinked(f,f2)
{
return f.sort("depthrsort",f2)
}
FC_getOrder(f)
{
order := Object()
Loop % f.len()
order[f[A_Index]] := Object()
Loop % f.len()
order[f[A_Index]].Insert(A_Index)
return order
}
FC_updateLinked(f,f2,order)
{
f2_old := f2.getCopy(false) ; don't mess with the catch
f2.clear()
Loop % f.len()
f2[A_Index] := f2_old[order[f[A_Index]].Remove(1)]
return f
}
;==== UPDATING =========================================================================================================
;=======================================================================================================================
FC_refresh(f,param="__deFault__")
{
f.clear()
method := f._params[1]
source := f._params[2]
if (method="catch")
{
; override default catch preference for catches.
f._prefs["catch"] := false
return f
}
; is there a better way?
if (method="list" or method="listn" or method="array")
{
; no need to carry around arrays or lists
f._params[2] := method
; use given param as source if applicable
if (param!="__deFault__")
source := param
}
ret := f.extend(method, source, f._params[3], f._params[4], f._params[5], f._params[6], f._params[7], f._params[8])
return ret ? ret : f
}
; absorb takes catches
FC_absorb(f,p1="",p2="",p3="",p4="",p5="",p6="",p7="",p8="")
{
Loop 8 ; blank param = skip, not stop
{
Loop % (fi:=p%A_Index%).len() ; Loop for regular items
f.add(fi[A_Index])
f._catch.absorb(fi.takeCatch())
;Loop % (ci:=fi._catch).len() ; Loop for caught items
; f._catch.add(ci[A_Index])
}
return f
}
; will be even more usefull as long as I update other methods
FC_extend(f, method,source="", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
callf := CallPrep("","",p3,p4,p5,p6,p7,p8)
return %callf%("FC_" method, f, source, p3,p4,p5,p6,p7,p8)
;return FC_%method%(f,source,p3,p4,p5,p6,p7,p8)
;return f.absorb(FC(method,source,p3,p4,p5,p6,p7,p8))
}
; catch is propogated if applicable
FC_become(f, p1, p2="", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
if isObject(p1)
return f.clear().absorb(p1)
else if p1
return f.clear().extend(p1,p2,p3,p4,p5,p6,p7,p8)
else
return f.clear()
}
; strange, and not sure I like the name anymore
; catch is not propogated
FC_bud(f, p1, p2="", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
return f.getTemplate().become(p1,p2,p3,p4,p5,p6,p7,p8)
}
; consume() ? possibly faster to attach existing string buffers to current fc, then destroy consumed fc?
;==== Expanding ========================================================================================================
;=======================================================================================================================
FC_getExpansion(f,includeFolders="__deFault__",recursive="__deFault__",pattern="__deFault__",f2="__deFault__")
{
; default to original parameters if applicable
if (includeFolders == "__deFault__")
{
if (f._params[1]="pattern")
includeFolders := f._params[3]
else if (f._params[1]="regex")
includeFolders := f._params[4]
else
includeFolders := 1
}
if (recursive == "__deFault__")
{
if (f._params[1]="pattern")
recursive := f._params[4]
else if (f._params[1]="regex")
recursive := f._params[5]
else
recursive := 1
}
pattern := (pattern!="__deFault__") ? pattern : "*"
f_target := (f2!="__deFault__") ? f2 : f.getTemplate()
Loop % (folders := f.getFolders()).len()
f_target.extend("pattern",folders[A_Index] pattern,includeFolders,recursive)
return f_target
}
FC_expand(f,includeFolders="__deFault__",recursive="__deFault__",pattern="__deFault__")
{
; do any languages allow for automagically prespecifying the return variable? (instead of passing it in explicitly)
return f.getExpansion(includeFolders,recursive,pattern,f)
;return f.absorb(f.getExpansion(includeFolders,recursive,pattern))
}
FC_getExpanded(f,includeFolders="__deFault__",recursive="__deFault__",pattern="__deFault__")
{
return f.getCopy().expand(includeFolders,recursive,pattern)
}
;==========================================================================================================================================================================================
;= FILTERING ==============================================================================================================================================================================
;==========================================================================================================================================================================================
/*
; generalize built-in filters and rename filter to where for a more query-like syntax
; maybe. maybe.
FC_Where(f,func,keep_if_func_returns=false, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
}
FC_WhereTF(f,func,keep_if_func_returns=false, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
}
FC_getWhere(f,func,keep_if_func_returns=false, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
return f.getCopy().Where(f,func,p2,p3,p4,p5,p6,p7,p8)
}
FC_getWhereTF(f,func,keep_if_func_returns=false, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
return f.getCopy().WhereTF(f,func,p2,p3,p4,p5,p6,p7,p8)
}
*/
FC_getSplit(f,func, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
callf := CallPrep("",p2,p3,p4,p5,p6,p7,p8)
ret := Object()
Loop % f.len()
{
key := %callf%(func,f[A_Index],p2,p3,p4,p5,p6,p7,p8)
;key := Call(func,f[A_Index],p2,p3,p4,p5,p6,p7,p8)
if !ret[key].add(f[A_Index])
ret[key] := f.bud("list",f[A_Index])
}
return ret
}
FC_filter(f,func,keep_if_func_returns=false, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
return f.become(f.getSplit(func,p2,p3,p4,p5,p6,p7,p8)[keep_if_func_returns])
}
FC_filterTF(f,func,keep_if_return_is=false, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
callf := CallPrep("",p2,p3,p4,p5,p6,p7,p8)
c := 0
Loop % f.len()
;if (!xor(Call(func,f[A_Index],p2,p3,p4,p5,p6,p7,p8),keep_if_return_is))
if (!xor(%callf%(func,f[A_Index],p2,p3,p4,p5,p6,p7,p8),keep_if_return_is))
f[++c] := f[A_Index]
return f.excludeInRange(c+1)
}
FC_manipulate(f,func, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
callf := CallPrep("",p2,p3,p4,p5,p6,p7,p8)
c := 0
Loop % f.len()
if (ret := %callf%(func,f[A_Index],p2,p3,p4,p5,p6,p7,p8))
f[++c] := ret
return f.excludeInRange(c+1)
}
;==== BUILT-IN FILTERS =================================================================================================
;=======================================================================================================================
; callbacks are inefficient. But so very convenient. T_T
FC_excludeAttributes(f,attr)
{
return f.filter("hasAttributes", false, attr)
}
FC_includeAttributes(f,attr)
{
return f.filter("hasAttributes", true, attr)
}
FC_getWithAttributes(f,attr)
{
return f.getCopy().includeAttributes(attr)
}
FC_getWithoutAttributes(f,attr)
{
return f.getCopy().excludeAttributes(attr)
}
hasAttributes(item,list)
{
FileGetAttrib, attr, %item%
If attr contains %list%
return true
}
FC_excludeFiles(f)
{
return f.filter("IsFolder", true)
}
FC_excludeFolders(f)
{
return f.filter("IsFolder", false)
}
FC_getFiles(f)
{
return f.getCopy().excludeFolders()
}
FC_getFolders(f)
{
return f.getCopy().excludeFiles()
}
FC_excludeNotExist(f)
{
return f.filter("PathExist",1)
}
FC_getExist(f)
{
return f.getCopy().excludeNotExist()
}
FC_excludeBlanks(f)
{
c := 0
Loop % f.len()
if f[A_Index]
f[++c] := f[A_Index]
return f.excludeInRange(c+1)
}
; ? better or worse? !?
FC_get(f,func,p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
callf := CallPrep("",p2,p3,p4,p5,p6,p7,p8)
return %callf%(func,f.getCopy(),p2,p3,p4,p5,p6,p7,p8)
}
FC_excludeDuplicates(f)
{
c := 0, seen := Object()
Loop % f.len()
{
if seen[f[A_Index]]
continue
seen[f[A_Index]] := true
f[++c] := f[A_Index]
}
return f.excludeInRange(c+1)
}
;==== COMPARISON FILTERS ===============================================================================================
;=======================================================================================================================
FC_excludeWhereIn(f,f2)
{
return f.filter("finder",false, f2)
}
FC_excludeWhereNotIn(f,f2)
{
return f.filter("finder",true, f2)
}
FC_getWhereNotIn(f,f2)
{
return f.getCopy().excludeWhereIn(f2)
}
FC_getWhereIn(f,f2)
{
return f.getCopy().excludeWhereNotIn(f2)
}
; a manipulateLinked might be nice...
FC_excludeMatched(f1,f2)
{
}
FC_excludeNotMatched(f1,f2)
{
}
remove_unchanged(f1,f2)
{
c := 0
Loop % f1.len()
{
if (f1[A_Index] == f2[A_Index])
continue
f1[++c] := f1[A_Index]
f2[c] := f2[A_Index]
}
f1.excludeInRange(c+1)
f2.excludeInRange(c+1)
}
;==== RANGES ===========================================================================================================
;=======================================================================================================================
; assumes the indices are sorted low to high
; assumes that indices exist (may fix this later)
; are the numbers correct here?
; need a split version...
; should exclude return excluded instead?
; or rather make a take variant?
; or would a bunch of remove statements be faster? even better if I figure out how it is faster. something to look into
FC_excludeAt(f,indices)
{
num := indices.MaxIndex()
inew := indices[1]-1
Loop % num - 1 ; for each index ('cept the last)
Loop % indices[(c := A_Index)+1]-indices[c]-1 ; for # of items between this exclusion and the next
f[inew] := f[++inew+c] ; keep building container, selecting from an offset (= # items already excluded).
; fill in the last stretch
Loop % f.len() - indices[num]
f[inew] := f[++inew+num]
return f.excludeInRange(inew+1)
}
FC_excludeNotAt(f,indices)
{
len := indices.MaxIndex()
Loop % len
f[A_Index] := f[indices[A_Index]]
return f.excludeInRange(len+1)
}
FC_getAt(f,indices)
{
return f.getCopy().excludeNotAt(indices)
}
FC_getNotAt(f,indices)
{
return f.getCopy().excludeAt(indices)
}
FC_excludeInRange(f,start=1,end="")
{
end := end ? end : f.len()
if !(start < 1 or end > f.len())
f.Remove(start, end)
return f
}
FC_excludeNotInRange(f,start=1,end="")
{
return f.excludeInRange(end ? end+1 : f.len()).excludeInRange(1,start-1)
}
FC_getInRange(f,start=1,end="")
{
return f.getCopy().excludeNotInRange(start,end)
}
FC_getNotInRange(f,start=1,end="")
{
return f.getCopy().excludeInRange(start,end)
}
;==== TWEAKING =========================================================================================================
;=======================================================================================================================
; would be very nice to generalize these. Could be the foundation for a set of
; array-of-FileContainer focused methods
; removes paths that are already defined implicitly
FC_simplify(f)
{
;SetBatchLines -1 ; definitely speeds things up, but is it desirable?
split := f.getSplit("depthsort")
keys := Object()
lens := Object()
enum := split._newEnum()
while enum[key,fc]
{
keys[A_Index] := key
lens[A_Index] := fc.len()
num_levels := A_Index
}
f.clear()
; for each depth (but the last)
Loop % num_levels - 1
{
level := A_Index
fc := split[keys[level]]
; for each item in this depth
Loop % lens[level]
{
vi := fc[A_Index]
; if item is a folder
if (SubStr(vi,0,1)=="\")
{
required := true
vilen := StrLen(vi)
; for each lower depth
Loop % num_levels - level
{
level2 := A_Index + level
fc2 := split[keys[level2]]
; for each item in this lower depth
Loop % lens[level2]
{
if (SubStr(fc2[A_Index],1,vilen)!=vi)
continue
required := false
break
}
if !required
break
}
if !required
continue
}
f.add(vi)
}
}
return f.absorb(split[keys[num_levels]])
}
FC_getSimple(f)
{
return f.getCopy().simplify()
}
; removes subitems from the list, usually to avoid conflicts
FC_reduce(f)
{
split := f.getSplit("depthsort")
enum := split._newEnum()
keys := Object()
lens := Object()
while enum[key,fc]
{
keys[A_Index] := key
lens[A_Index] := fc.len()
num_levels := A_Index
}
f.clear()
; for each depth (but the last)
Loop % num_levels - 1
{
level := A_Index
fc := split[keys[level]]
f.absorb(fc)
; for each item in this depth
Loop % lens[level]
{
vi := fc[A_Index]
; if item is a folder
if (SubStr(vi,0,1)=="\")
{
vilen := StrLen(vi)
; for each lower depth
Loop % num_levels - level
{
level2 := A_Index + level
fc2 := split[keys[level2]]
; for each item in this lower depth
Loop % lens[level2]
{
if (fc2[A_Index] and SubStr(fc2[A_Index],1,vilen)!=vi)
continue
fc2[A_Index]:="" ; exclude item if it is contained in another item
}
}
}
}
}
return f.absorb(split[keys[num_levels]]).excludeBlanks()
}
FC_getReduced(f)
{
return f.getCopy().reduce()
}
; get real containing would be nice too... if you don't want to accidentally affect non contained existing items
FC_getContaining(f,default="")
{
fc := f.getReduced()
common := Path(fc[1])
Loop % fc.len()
{
target := fc[A_Index]
while (!InStr(target,common.path))
common := Path(common.dir)
if common.path
continue
ErrorLevel := "No Common Path Found"
break
}
return f.bud("list",common.path ? common.path : default)
}
;==========================================================================================================================================================================================
;= OUTPUT =================================================================================================================================================================================
;==========================================================================================================================================================================================
FC_toList(f,delim="`n")
{
Loop % f.len()
list .= f[A_Index] . delim
return list
}
FC_toArray(f)
{
ret := Object()
Loop % f.len()
ret.Insert(f[A_Index])
return ret
}
FC_toFile(f,fpath="", encoding="UTF-16", delim="`n", overwrite=false)
{
if (!fpath AND f._params[1] = "file")
fpath := f._params[2]
if (!fpath)
return ( ErrorLevel:="YOU NEED TO SPECIFY A FILENAME" )
file := FileOpen(fpath, overwrite ? 0x01 : 0x02, encoding)
file.Write(f.toList(delim))
file.Close()
return fpath
}
; saves a recursive simplified list to the clipboard. minimum text required to recreate the filestructure
; more for my own testing than anything else
FC_structureToClipboard(f)
{
return f.getExpanded().simplify().toClipboard()
}
FC_toClipboard(f,delim="`n")
{
return ( clipboard := f.toList(delim) )
}
FC_explore(f, base_only=false, default="", warn=10)
{
fc := base_only ? f.getContaining(default) : f.getFolders()
if ( ( (n := fc.len()) > warn ) and warn )
{ ; warning + YesNo
MsgBox, 52, Warning, This operation will open %n% folders... Would you like to continue?
IfMsgBox No
return
}
Loop %n%
Run % "explore " fc[A_Index]
}
;==========================================================================================================================================================================================
;= FILE OPERATIONS ========================================================================================================================================================================
;==========================================================================================================================================================================================
FC_create(f)
{
blank_file := getTemp("file", true)
blank_dir := getTemp("folder", true)
dest := f.getSimple()
source := FC()
Loop % dest.len()
source.add(IsFolder(dest[A_Index]) ? blank_dir : blank_file)
; operation -undo
return ShFO("FO_COPY", source, dest, "FOF_SIMPLEPROGRESS|FOF_MULTIDESTFILES|FOF_NOCONFIRMMKDIR|")
}
FC_delete(f, recycle=true, prompt=false, extra_flags="")
{
flags := (recycle ? "FOF_ALLOWUNDO|" : "") . (prompt ? "FOF_SIMPLEPROGRESS|FOF_WANTNUKEWARNING|" : "FOF_SILENT|FOF_NOCONFIRMATION|")
return ShFO( "FO_DELETE", f.getCopy().excludeDuplicates().reduce().excludeNotExist(),f.getTemplate(), extra_flags . flags)
}
FC_moveInto(f,p1,extra_flags="")
{
return f.move(p1,extra_flags,"into")
}
FC_moveOnto(f,p1,extra_flags="")
{
return f.move(p1,extra_flags,"onto")
}
FC_move(f,p1, extra_flags="", dest_mode="onto")
{
return FC_operation(f,"FO_MOVE",p1,extra_flags,dest_mode)
}
FC_copyInto(f,p1, extra_flags="")
{
return f.copy(p1,extra_flags,"into")
}
FC_copyOnto(f,p1, extra_flags="")
{
return f.copy(p1,extra_flags,"onto")
}
FC_copy(f,p1, extra_flags="", dest_mode="onto")
{
return FC_operation(f,"FO_COPY",p1,extra_flags,dest_mode)
}
; maybe work quick regex stuff back into this later
FC_rename(f,p1,extra_flags="")
{
return f.moveOnto(p1,extra_flags)
}
; need to update this for dest_mode stuff
/*
FC_moveContents(f,p1, extra_flags="")
{
source := f.getTemplate()
; make the mapping explicit (in case of single destination folder)
dest := IsObject(p1) ? p1 : FC("list",p1)
if (dest.len() < f.len())
Loop % f.len()
dest[A_Index] := dest[1]
; expand to list contents explicitly
Loop % f.len()
{
i := A_Index
new_source := FC("pattern",f[i] "*",1,0)
source.absorb(new_source)
Loop % new_source.len()
destination.add(dest[i])
}
return FC_operation(source,"FO_MOVE",destination,extra_flags)
}
*/
;==== Enfolder =========================================================================================================
;=======================================================================================================================
FC_enfolder(f,dest="")
{
if (dest := promptForPath(f,dest, "Please provide a name for the new folder."))
return f.moveInto(dest "\" )
}
;==== Spill ============================================================================================================
;=======================================================================================================================
;FC_spill(f,folders=1,recurse=0,delete=0)
FC_spill(f,folders=1,recurse=0,delete=0)
{
flatten := folders and recurse
folders := flatten ? 0 : folders
source := f.getTemplate(1) ; template, but with catch propogated
for_deletion := f.getTemplate()
for_creation := f.getTemplate()
dest := FC()
is_folder := f.getSplit("IsFolder")
unchanged := is_folder[false] ? is_folder[false] : f.getTemplate() ; any way to do this with default constructor?
folder_fc := is_folder[true]
Loop % folder_fc.len()
{
folder := Path(folder_fc[A_Index])
new_source := FC("pattern",folder.path "*.*",folders,recurse) ; obtain requested subitems of this folder
dest.extend("listn",folder.dir,new_source.len())
source.absorb(new_source) ; add new sources to the source FileContainer, assumed that folders don't overlap -> fast mode OK
if (delete) ; at the very least, the delete flag indicates that the spilt folder should be deleted
for_deletion.add(folder.path)
else
unchanged.add(folder.path)
if (flatten) ; if flattening, the folders need to be spilt too. But rather than moving them, just recreate them
for_creation.absorb(FC("pattern",folder.path . "*.*",2,1).manipulate("move_helper", folder.dir))
}
; could move the folders last... no need to recreate them [ can't undo after merge, so no point]
; could merge folders instead [can't undo after merge, so no point]
; isempty filter before rme is a bit odd, but quick way to prevent duplicates
return source.moveInto(dest).absorb(for_creation.create(), unchanged, for_deletion.filter("IsEmpty",true).removeEmptyFolders(0))
}
FC_leak(f)
{
return f.spill(0,0,0)
}
FC_dump(f,delete=0)
{
return f.spill(0,1,delete)
}
FC_flatten(f,delete=0)
{
return f.spill(1,1,delete)
}
;==== zip ==============================================================================================================
;=======================================================================================================================
; still no error handling for 7-zip stuff
FC_zip(f,destination="", prompt=true, hide=true, switches="-y")
{
if !(destination := promptForPath(f,destination,"Please provide a name for the new archive.", prompt))
return
TrayTip,, Now zipping...
f.run7z("a """ . destination . ".zip"" @""" . f.toFile(getTemp(),"UTF-8") . """ " . switches, hide)
TrayTip,, Done!
ret := f.getTemplate(1)
ret.add(destination ".zip")
return ret
}
; option to spill if only folders are present? What would I call that? Or just an option for here?
; delete option is a bit poor... need error handling for 7zip
FC_unzip(f, spill=false, shorten=true, remove_dir_ext=true, delete=false, hide=true, switches="-o* -aou -y")
{
new_f := f.GetTemplate(1)
files := f.getFiles()
TrayTip,, Now unzipping...
Loop % files.len()
{
item := Path(files[A_Index])
TrayTip,, % "Now unzipping '" item.path "'`n(" A_Index " of " files.len() ")"
f.run7z("x" . " """ . item.path . """ " . switches,item.dir, hide)
new_f.add(item.dir . item.nameNoExt . "\")
}
TrayTip,, Now processing...
if remove_dir_ext
new_f := new_f.doManip("remove_dir_ext")
if shorten
new_f := new_f.shorten(true,delete)
if spill
new_f := new_f.spill(1,0,delete)
if delete
new_f.absorb(files.delete())
TrayTip,, Done!
return new_f
}
FC_run7z(f, line, working_dir="", hide=true)
{
return f.runWait(f._prefs["7zip"] " " line, working_dir, hide ? "hide":"")
}
; assumes item is already known to be a folder
remove_dir_ext(item)
{
return RegExReplace(item,"\s*[.].*","\")
}
;==== Shorten ==========================================================================================================
;=======================================================================================================================
FC_shorten(f, recursive=false, delete=true)
{
is_folder := f.getSplit("IsFolder")
if recursive
is_folder[true].expand(2)
can_shorten := is_folder[true].getSplit("canShorten")
target_split := can_shorten[true].getSplit("depthrsort")
;dout_o(is_folder[true],"folder targets")
;dout_o(can_shorten[true],"shorten targets")
;dout_o(target_split,"target split")
dest := f.getTemplate(1).absorb(is_folder[false],can_shorten[false])
enum := target_split._newEnum()
while enum[depth,targets]
dest.absorb(targets.spill(1,0,1))
return dest.excludeNotExist()
}
shorten_helper(folder)
{
return Path(folder).dir
}
canShorten(item)
{
num_files := 0
loop %item%*.*
if (num_files := A_Index)
break
num_folders := 0
if !num_files
Loop %item%*.*,2
if ( (num_folders := A_Index) == 2 )
break
return (num_folders = 1 and num_files = 0)
}
;==== Remove Empty Folders =============================================================================================
;=======================================================================================================================
; recursive mode to remove folders that only have empty folders in them too ?
; ah, the return was blank if nothing was deleted! this is WRONG!!
FC_removeEmptyFolders(f,recursive=true)
{
is_folder := f.getSplit("IsFolder")
is_empty := is_folder[true].getSplit("IsEmpty")
if recursive
is_empty[true].absorb(f.getExpansion(2).filter("IsEmpty",true))
return f.getTemplate(1).absorb(is_empty[true].delete(1,0),is_folder[0],is_empty[0])
}
;==== Up ===============================================================================================================
;=======================================================================================================================
FC_up(f)
{
return f.doManip("up_helper")
}
up_helper(item)
{
p := Path(item)
return Path(p.dir).dir . p.name
}
;==== DoManip ==========================================================================================================
;=======================================================================================================================
FC_doManip(f,func, p2="__deFault__", p3="__deFault__", p4="__deFault__", p5="__deFault__", p6="__deFault__", p7="__deFault__", p8="__deFault__")
{
return f.moveOnto(f.getCopy().manipulate(func,p2,p3,p4,p5,p6,p7,p8))
}
;==== SetAttributes ====================================================================================================
;=======================================================================================================================
FC_setAttributes(f,attributes,recursive=false,catch=true)
{
c := 0
ret := f.getTemplate(1)
Loop % f.len()
{
path := f[A_Index]
FileSetAttrib, %attributes%, %path%, 1, %recursive%
if !ErrorLevel
ret.add(f[A_Index])
else if catch
ret._catch.add(f[A_Index])
}
return ret
}
;==== RenameAsText =====================================================================================================
;=======================================================================================================================
FC_renameAsText(f,editor="__deFault__")
{
if (editor == "__deFault__")
editor := f._prefs["editor"]
temp := getTemp()
f.toFile(temp)
f.runWait(editor " " temp)
ret := f.moveOnto(FC("file",temp))
FileDelete %temp%
return ret
}
;==========================================================================================================================================================================================
;= INFORMATION ============================================================================================================================================================================