FileSplit — File Browser

FileSplitv0.1 / FileSplitv0.1 / filesplit.mrc

filesplit.mrc — 16.3 KB — Download this file

; FileSplit v0.1
; by Scab, script@scab.se

; /fsplit    <- for the dialog
; /fsplit <x:\the.file> <part_size> [b|kb|mb|gb|tb|pb|eb]
;
; Ex: /fsplit c:\what.ever 100 kb        - splits file in 100 kB parts.
;     /fsplit c:\what.ever 1 gb          - splits file in 1 GB parts
;     /fsplit c:\what.ever 1024          - splits file in 1 kB parts.
;     /fsplit c:\what.ever 10            - splits file in 10 B parts.
;
; The last, optional parameter specifies which format you've
; specified the part_size in. Default is B (bytes).

alias fsplit {
  if (!$1) {
    dialog $iif($dialog(fsplit),-v,-m) fsplit fsplit
    return
  }
  var %a = $1
  if ($left(%a,1) == $chr(34)) {
    var %a = $+(",$gettok($1-,1,34),")
    tokenize 32 $gettok($1-,$+($numtok(%a,32),-),32)
  }
  if ($2 isnum) var %b = $2
  var %c = $iif($3,$3,b), %i
  if (%a) && (%b) && (%c) {
    if ($isfile(%a)) {
      .signal -n fsplit start
      var %d = 0, %e = $lof(%a), %f = $calc($replace(%c,kb,1024,mb,$calc(1024^2),gb,$calc(1024^3),tb,$calc(1024^4),pb,$calc(1024^5),eb,$calc(1024^6),b,1) * %b), %g = $+($nofile(%a),$gettok($nopath(%a),$+(1-,$calc($numtok($nopath(%a),46) -1)),46))
      if (%f > %e) var %n = 1
      while (%d <= %e) {
        inc %i
        if (%i == 1) var %h = %d + $calc(%f - 16)
        else var %h = %d + %f
        if (%h > %e) var %h = %e
        bread %a %d %h &x
        var %j = $+(",%g,.,$str(0,$calc(3 - $len(%i))),%i,")
        if ($isfile(%j)) {
          var %k = $input(File $+(",%j,") already exists. $+ $str($crlf,2) $+ Overwrite?,ignv,File exists)
          if (%k == $cancel) {
            if ($show) echo -at * Aborted.
            .signal fsplit abort
            return
          }
          elseif (%k == $no) {
            if ($show) echo -at * Skipped: %j
            .signal -n fsplit skip %i %j
            goto x
          }
          elseif (%k == $yes) {
            .remove %j
            .signal -n fsplit removed %i %j
          }
        }
        var %l = %f, %m = 0
        if (%l > %e) var %l = %e
        if (%i == 1) {
          var %x = $gettok($nopath(%a),-1,46), %y = $+($str(0,$calc(16 - $len(%x))),%x)
          bwrite %j 0 15 %x
          var %m = 16
          if (!%n) var %l = %l - 16
        }
        bwrite %j %m %l &x
        bunset &x
        .signal -n fsplit wrote %i %j
        :x
        inc %d $iif(%i == 1,$calc(%f - 16),%f)
      }
      .signal fsplit done %i
      if ($show) echo -at * Done writing %i files.
    }
    else {
      .signal fsplit error 001 %a
      if ($show) echo -at * No such file: %a
    }
  }
  else {
    .signal fsplit error 002
    if ($show) echo -at * Too few parameters.
  }
}

; /fmerge <x:\first.file> [number_of_files]
;
; /fmerge x:\what.001        - attempts to merge what.001 with all of the following files (what.xxx)
; /fmerge x:\what.001 2      - attempts to merge what.001 and what.002
; /fmerge x:\what.001 45     - attempts to merge files what.001 to what.045

alias fmerge {
  var %a = $1
  if ($left(%a,1) == $chr(34)) {
    var %a = $+(",$gettok($1-,1,34),")
    tokenize 32 $gettok($1-,$+($numtok(%a,32),-),32)
  }
  if ($2) {
    if ($2 isnum) var %b = $2
    else {
      .signal fmerge error 002
      if ($show) echo -at * Invalid number of files.
      return
    }
  }
  if ($isfile(%a)) {
    .signal -n fmerge start
    var %c = %a, %i = 1
    while ($isfile(%c)) {
      bread %c 0 $lof(%c) &x
      var %e = 1
      if (%i == 1) {
        var %d = $remove($bvar(&x,1,16).text,$chr(32)), %e = 17, %f = $+(",$nofile(%a),$puttok($nopath(%a),%d,-1,46),")
        if ($isfile(%f)) {
          if ($input(File %f already exists. $+ $str($crlf,2) $+ Overwrite?,ig,File exists)) {
            .remove %f
            .signal -n fmerge remove %i %f
          }
          else {
            if ($show) echo -at * Aborted.
            .signal fmerge abort
            return
          }
        }
      }
      bcopy &y 1 &x %e -1
      bwrite %f -1 &y
      bunset &x &y
      .signal -n fmerge wrote %i %f
      if (%b) && (%i == %b) {
        .signal -n fmerge stop %b
        if ($show) echo -at * Merge stopped at number_of_files ( $+ %b $+ ).
        return
      }
      inc %i
      var %c = $+(",$nofile(%a),$puttok($nopath(%a),$str(0,$calc(3 - $len(%i))),-1,46),%i,")
    }
    var %i = %i - 1
    .signal fmerge done %i %f
    if ($show) {
      echo -at * Done, merged %i $iif(%i > 1,files,file) to %f $+ .
    }
  }
  else {
    .signal fmerge error 001
    if ($show) echo -at * No such file: %a
  }
}

;Signals
on *:signal:fsplit:{
  if ($dialog(fsplit)) {
    if ($1 == error) {
      if ($2 == 001) { _fsplit_info fsplit 8 ERROR: File does not exist. }
      if ($2 == 002) { _fsplit_info fsplit 8 ERROR: Missing/invalid settings. }
    }
    elseif ($1 == wrote) { _fsplit_info fsplit 8 Wrote file $+($chr(35),$2) $+ : $3- }
    elseif ($1 == done) { _fsplit_info fsplit 8 Done writing $2 files. }
    elseif ($1 == abort) { _fsplit_info fsplit 8 Aborted. }
    elseif ($1 == start) { _fsplit_info fsplit 8 Started split... }
  }
}
on *:signal:fmerge:{
  if ($dialog(fsplit)) {
    if ($1 == error) {
      if ($2 == 001) { _fsplit_info fsplit 18 ERROR: File does not exist. }
      if ($2 == 002) { _fsplit_info fsplit 18 ERROR: Missing/invalid settings. }
    }
    elseif ($1 == wrote) { _fsplit_info fsplit 18 Merged $+($chr(35),$2) $+ : $3- }
    elseif ($1 == done) { _fsplit_info fsplit 18 Done, merged $2 files. }
    elseif ($1 == abort) { _fsplit_info fsplit 18 Aborted. }
    elseif ($1 == start) { _fsplit_info fsplit 18 Started merge... }
    elseif ($1 == stop) { _fsplit_info fsplit 18 Stopped at range limit $+ : $2 $+ . }
  }
}
alias -l _fsplit_info {
  var %x = $did($1,$$2).lines + 1
  did -a $1 $2 $$3-
  did -c $1 $$2 %x
  did -u $1 $$2 %x
}
alias -l _fsplit_finfo {
  did -o $dname $1 1 Size: $bytes($file($$2-).size).suf
}
alias -l _fsplit_last5 {
  if ($1 isnum) {
    var %x = $gettok(%fsplit_lastsplits,$1,124)
    return $nopath(%x) $+ :run $+(",%x,")
  }
}

; GUI Code
on *:dialog:fsplit:*:*:{
  if ($devent == init) {
    did -a $dname 7,34 B
    did -a $dname 7,34 kB
    did -a $dname 7,34 MB
    did -a $dname 7,34 GB
    did -a $dname 7,34 TB
    did -a $dname 7,34 PB
    did -a $dname 7,34 EB
    did -c $dname 7 $iif(%fsplit_lastformat,$ifmatch,3)
    did -c $dname 34 $iif(%fsplit_lastcalcformat,$ifmatch,3)
    did -b $dname 34
    did -m $dname 12,39
    var %x = 1
    while (%x <= 5) {
      var %y = $gettok(%fsplit_lastsplits,%x,124), %z = $gettok(%fsplit_lastmerges,%x,124), %a = $gettok(%fsplit_lastcalced,%x,124)
      if ($isfile(%y)) {
        if (%x == 1) _fsplit_finfo 6 $+(",%y,")
        did -a $dname 2 %y
      }
      if ($isfile(%z)) { did -a $dname 15 %z }
      if ($isfile(%a)) {
        if (%x == 1) _fsplit_finfo 29 $+(",%a,")
        did -a $dname 26 %a
      }
      inc %x
    }
    did -c $dname 2,15,26 1
    did -a $dname 31 %fsplit_lastchunksz
  }
  elseif ($devent == sclick) {
    if ($did == 2) {
      if ($isfile($did($dname,2))) { _fsplit_finfo 6 $+(",$did($dname,2),") }
    }
    elseif ($did == 3) {
      var %x = $$sfile($iif(%fsplit_lastdir,$ifmatch,$left($mircdir,3)))
      if ($isfile(%x)) {
        set %fsplit_lastdir $+(",$nofile(%x),")
        set %fsplit_lastsplits $+(%x,$chr(124),%fsplit_lastsplits)
        if ($numtok(%fsplit_lastsplits,124) > 5) set %fsplit_lastsplits $gettok(%fsplit_lastsplits,1-5,124)
        did -i $dname 2 1 %x
        did -c $dname 2 1
        _fsplit_finfo 6 $+(",%x,")
      }
    }
    elseif ($did == 7) || ($did == 34) { set $+(%,fsplit_last,$replace($did,7,format,34,calcformat)) $replace($did($dname,$did),kb,2,mb,3,gb,4,tb,5,pb,6,eb,7,b,1) }
    elseif ($did == 9) {
      .fsplit $+(",$did($dname,2),") $did($dname,5) $lower($did($dname,7))
    }
    elseif ($did == 11) {
      if ($did($dname,11).state == 1) did -n $dname 12
      else did -m $dname 12
    }
    elseif ($did == 13) {
      .fmerge $+(",$did($dname,15),") $iif($did($dname,11).state == 1,$did($dname,12))
    }
    elseif ($did == 14) {
      var %x = $input(The range limit sets the maximum number of files that the script will merge. If you for some reason want to merge only file 001-010 of 020 $+ $chr(44) set the range limit to 10.,iog,Help)
    }
    elseif ($did == 16) {
      var %x = $$sfile($iif(%fsplit_lastdir,$ifmatch,$left($mircdir,3)))
      if ($isfile(%x)) {
        set %fsplit_lastdir $+(",$nofile(%x),")
        set %fsplit_lastmerges $+(%x,$chr(124),%fsplit_lastmerges)
        if ($numtok(%fsplit_lastmerges,124) > 5) set %fsplit_lastmerges $gettok(%fsplit_lastmerges,1-5,124)
        did -i $dname 15 1 %x
        did -c $dname 15 1
      }
    }
    elseif ($did == 20) {
      var %x = $input(FileSplit v0.1 $+ $chr(44) first release version. $+ $str($crlf,2) $+ Author: $+ $str($chr(9),2) $+ Scab $+ $crlf $+ Thanks to: $+ $chr(9) $+ dohcan $+ $crlf $+ $str($chr(9),2) $+ (Dialog Studio) $+ $str($crlf,2) $+ Contact: $+ $chr(9) $+ script@scab.se,iog,About)
    }
    elseif ($did == 21) {
      var %x = $+(",$scriptdirfilesplit.txt,")
      if ($isfile(%x)) run notepad.exe %x
      else var %x = $input(Could not locate the script documentation.,wog,Error)
    }
    elseif ($did == 22) {
      var %x = $input(When opening the first file (.001) the script will attempt to locate and merge the others aswell. If you don $+ $chr(39) $+ t want this $+ $chr(44) use the range limit below. Do not attempt to start with another file than the first $+ $chr(44) it contains various script data.,iog,Help)
    }
    elseif ($did == 23) {
      var %x = $input(This is the size of each split chunk. Precisely what you want to use depends entirely on what type of media you are going to fit them on. Here are some common sizes: $+ $str($crlf,2) $+ 1440000 B $+ $chr(9) - Standard floppy disk $+ $crlf $+ 100000000 B $+ $chr(9) - Standard Zip disk $+ $crlf $+ 128000000 B $+ $chr(9) - Standard 128MB USB memory $+ $str($crlf,2) $+ Please note that 1 kB is not 1000 B but rather 1024 B and that 1 MB is 1024^2 rather than 1000^2 $+ $chr(44) and so on. Take this into account when setting custom part sizes.,iog,Help)
    }
    elseif ($did == 24) {
      var %x = $input(The source file that you wish to split up. The bigger the file $+ $chr(44) the longer time it takes to split it up. Also $+ $chr(44) use the size to estimate how many disks you will need to fit the chunks. Alternatively use the calculator.,iog,Help)
    }
    elseif ($did == 26) {
      if ($isfile($did($dname,26))) { _fsplit_finfo 29 $+(",$did($dname,26),") }
    }
    elseif ($did == 27) {
      var %x = $$sfile($iif(%fsplit_lastdir,$ifmatch,$left($mircdir,3)))
      if ($isfile(%x)) {
        set %fsplit_lastdir $+(",$nofile(%x),")
        set %fsplit_lastcalced $+(%x,$chr(124),%fsplit_lastsplits)
        if ($numtok(%fsplit_lastcalced,124) > 5) set %fsplit_lastcalced $gettok(%fsplit_lastcalced,1-5,124)
        did -i $dname 26 1 %x
        did -c $dname 26 1
        _fsplit_finfo 29 $+(",%x,")
      }
    }
    elseif ($did == 28) {
      var %x = $input(The source file that you wish to split up. Only used here to get the total file size. Note that 16B of merge data will be added to the total chunk size.,iog,Help)
    }
    elseif ($did == 32) {
      var %x = $input(The number of files that you want to split the source file into.,iog,Help)
    }
    elseif ($did == 33) {
      var %x = $input(Double click the "part size for" line to copy the file size. Remember to set the size format to B before splitting.,iog,Help)
    }
    elseif ($did == 38) {
      var %x = $input(Target part size is used to set a fixed chunk size. Splitting a 300 MB file in 3 would normally generate three 100 MB chunks $+ $chr(44) but with a target size of 128 MB it will generate 128+128+44 MB files. This leaves 84MB of free space on the last 128MB disk rather than 28MB on each one. $+ $str($crlf,2) $+ Please note that this calculator incorrectly counts 1 kB as 1000B to comply with (also incorrect) media sizes. A target part size of 128 MB will produce a file of about 122 MB since this is all a 128 MB disk can really store.,iog,Help)
    }
    elseif ($did == 36) {
      var %a = $+(",$did($dname,26),"), %b = $did($dname,31), %ok = 0
      set %fsplit_lastchunksz %b
      if ($did($dname,41).state == 1) var %ok = 1
      elseif (%b isnum) var %ok = 1
      if ($isfile(%a)) && (%ok) {
        var %c = $lof(%a) + 16
        ; fyi +16 is for script data to be stored in .001
        _fsplit_info fsplit 37 - %a
        if ($did($dname,41).state == 1) {
          var %d = $did($dname,39), %e = $did($dname,34), %f = $calc($replace(%e,kb,1000,mb,$calc(1000^2),gb,$calc(1000^3),tb,$calc(1000^4),pb,$calc(1000^5),eb,$calc(1000^6),b,1) * %d), %g = 0, %h = %c, %i, %bk = %f
          if (%d) && (%e) && (%f) && (%g != $null) {
            while (%g <= %c) {
              inc %i
              if (%f > %h) {
                var %f = %h
                inc %g 1
              }
              _fsplit_info fsplit 37 Chunk %i $+ : %f B
              dec %h %f
              inc %g %f
            }
            _fsplit_info fsplit 37 Part size for %i chunks: %bk B
          }
          else {
            _fsplit_info fsplit 37 ERROR: Missing/invalid target part size/format.
          }
        }
        else {
          _fsplit_info fsplit 37 Part size for %b chunks: $ceil($calc(%c / %b)) B
        }
      }
      else {
        _fsplit_info fsplit 37 ERROR: Missing/invalid settings.
      }
    }
    elseif ($did == 41) {
      if ($did($dname,41).state == 1) {
        did -e $dname 34
        did -m $dname 31
        did -n $dname 39
      }
      else {
        did -b $dname 34
        did -n $dname 31
        did -m $dname 39
      }
    }
  }
  elseif ($devent == dclick) {
    if ($did == 37) {
      var %a = $did($dname,$did,$did($dname,$did).sel)
      if (*part*size*for*?*chunks*:*? iswm %a) { clipboard $gettok(%a,6,32) }
    }
  }
  elseif ($devent == edit) {
    if ($did == 2) {
      if ($isfile($did($dname,2))) { _fsplit_finfo 6 $did($dname,2) }
    }
    elseif ($did == 26) {
      if ($isfile($did($dname,26))) { _fsplit_finfo 29 $did($dname,26) }
    }
  }
}

dialog -l fsplit {
  title "FileSplit v0.1"
  size -1 -1 314 325
  option pixels
  tab "Split", 1, 2 2 308 294
  combo 2, 12 32 290 100, tab 1 size edit drop
  button "&Browse", 3, 226 56 75 23, tab 1
  text "Part size:", 4, 13 97 45 16, tab 1
  edit "", 5, 90 93 136 23, tab 1
  text "", 6, 12 58 201 20, tab 1
  combo 7, 227 94 73 120, tab 1 size drop
  list 8, 11 130 291 124, tab 1 size
  button "&Split", 9, 226 256 75 25, tab 1
  link "?", 23, 62 97 10 16, tab 1
  link "?", 24, 214 57 10 16, tab 1
  tab "Merge", 10
  check "Set file range", 11, 13 232 84 20, tab 10
  edit "", 12, 12 256 135 23, tab 10
  button "&Merge", 13, 226 256 75 25, tab 10
  link "?", 14, 139 235 10 16, tab 10
  combo 15, 12 32 290 100, tab 10 size edit drop
  button "&Browse", 16, 226 56 75 23, tab 10
  text "Open the first file here (normally .001).", 17, 12 58 190 20, tab 10
  list 18, 11 93 291 133, tab 10 size
  link "?", 22, 214 57 10 16, tab 10
  tab "Calculator", 25
  combo 26, 12 32 290 100, tab 25 size edit drop
  button "&Browse", 27, 226 56 75 23, tab 25
  link "?", 28, 214 57 10 16, tab 25
  text "", 29, 12 58 201 20, tab 25
  text "Number of chunks:", 30, 13 97 90 16, tab 25
  edit "", 31, 143 93 158 23, tab 25
  link "?", 32, 108 96 10 16, tab 25
  button "&Calculate", 36, 226 157 75 23, tab 25
  list 37, 11 185 291 97, tab 25 size
  check "Target part size", 41, 12 130 98 20, tab 25
  link "?", 38, 112 132 10 15, tab 25
  edit "", 39, 143 128 84 23, tab 25
  combo 34, 228 129 73 120, tab 25 size drop
  button "&Close", 19, 236 297 75 25, ok
  button "&About", 20, 31 297 75 25
  button "?", 21, 3 297 25 25
  link "?", 33, 13 169 10 15, tab 25
}


; on load/unload
on *:load:{
  echo -aet * FileSplit v0.1 loaded successfully.
}
on *:unload:{
  unset %fsplit_*
  echo -aet * Unloaded FileSplit v0.1.
}

; menu crap
menu menubar {
  -
  FileSplit
  .Launch:fsplit
  .-
  .Recent
  ..$submenu($_fsplit_last5($1))
  .-
  .Unload:if ($input(This will unload FileSplit v0.1. $+ $str($crlf,2) $+ Continue?,wyg,Unload)) .unload -rs $+(",$script,")
}