how do i split and join files?

Discuss and share scripts and script files...
PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: how do i split and join files?

Post by PeterH »

admin wrote:
tiago wrote:Here goes your script but some notes first:

1. SC readfile won't stop if numbytes value exceeds the remaining data to be read, thus generating files which are multiples of numbytes. This issue is fixed in my script.

2. :arrow: bug warning :!: SC writefile won't properly write unicode files as it completely loses formating when writing portions which contain unicode text.
Both nonsense. I stopped reading further. :evil:
Maybe this has something to do with a wrong calculated number for start-byte? (0-based instead of 1-based!)

If with start-byte = 0 XY ReadFile corrects this to 1, it will read (with values of above script) bytes 1-500.000. The next will request bytes 500.000-999.999, i.e. byte 500.000 is read (and then written) twice.

If I'm right the bug is in the script...

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: how do i split and join files?

Post by neutrox »

I modified the code following PeterH's guidance and trying to fix the problem myself. Maybe it's a noob dumbness but the problem still remains ie a 1.132 bytes file wont split in 2 x 500 + 132 but in 3 x 500.

the splitting point is now fixed. please notice: the following code is meant for tests only to try to show the problem which really happens.

p.s. hey scripting is fun :mrgreen:

Code: Select all

      end(compare("<xyver>", '10.90.0002', 'v') < 0, "Written for XYplorer v10.90.0002+.");
      end(getinfo("CountSelected") < 1), "Pick a file first!";

      $file = "<curitem>";
      $basenm = regexreplace($file, ".+\\(.+)\..*", "$1");
      $tdir = regexreplace($file,  "(.+\\).+", "$1");
      $ext = regexreplace($file, ".+\.(.*)", "$1");

      $max = filesize($file);
      $read = 500000; //amount which will be read; user def; default: 100.000 bytes (x 10 = 1 MB)

      $factor = $max \ $read;
      $factorF = $factor * $read;
      $mod = $max - $factorF;

      $incr = 1; //starting amount; FIXED

      WHILE ($incr < $max)                   {
      $data = readfile(, , $read, , $incr);
      $incr = $incr + $read;

      writefile ("$tdir" . "$basenm" . " - Splitted." . "$ext", $data, r);
                                                 }

      echo "file splitted!";

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: how do i split and join files?

Post by PeterH »

tiago wrote:Here goes your script but some notes first:

1. SC readfile won't stop if numbytes value exceeds the remaining data to be read, thus generating files which are multiples of numbytes. This issue is fixed in my script.
This point is verified :(

For a first test I happened to take a .txt-file of 20 lines, each 100 chars + CRLF, that is 2040 bytes.
Read 3 times 700 bytes and wrote each to separate file.
File 1 & 2 OK: each 700 byte.
File 3 strange: it is shown as "binary" :?: :?:
Check of this file shows: it's 700 byte long ( :!: ) and the bytes missing in source are filled with hex 00! :!: I think that's the reason why this file is shown as binary!

This means: trying to read more bytes than are left in the file leads to appended hex 00 to fill the requested size!

I hope this is not intended?

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: how do i split and join files?

Post by admin »

Damn, yes! Shame on me, but Tiago was right. :oops: The combination of numbytes and start indeed allowed to apparently read characters beyond EOF. (In fact the read buffer was not redimensioned correctly.) Fix comes.

Post Reply