Jump to content

File Split and Join UDF


locomaestro
 Share

Recommended Posts

well i thought it was a great program the file splitter so i decided to make som udf for splitting files and joining them and also attaching files to other files and deattaching them. so here are my udf.

Func SplitFiles($file,$pathforparts,$nameofparts,$numberofparts)

DirCreate($pathforparts)

$pathoffile = FileRead($file,FileGetSize($file))

$amount = FileGetSize($file)

$parts = $amount/$numberofparts

$round = Floor($parts)

$total = $round * $numberofparts

$leftover = $amount - $total

Dim $part[$numberofparts + 1]

for $i = 1 to $numberofparts

if $i <> 1 and $i <> $numberofparts then 

$part[$i] = StringTrimLeft($pathoffile,($round * $i) - $round )

$left = StringLeft($part[$i],$round)

$part[$i] = $left

EndIf

if $i = $numberofparts then 

$part[$i] = StringTrimLeft($pathoffile,($round * $i) - $round )

$left = StringLeft($part[$i],$round)

$right = StringRight($pathoffile,$leftover)



$part[$i] = $left & $right

EndIf

if $i = 1 then $part[$i] = FileRead($file,$round)

FileWrite($pathforparts & "\" & $nameofparts & "_" & $i & ".fp",$part[$i])

Next

EndFunc

Func JoinFiles($filename,$pathofparts,$saveparts)

$numberoffiles = 0

$es = FileFindFirstFile($pathofparts & "\" & "*.fp*")

If $es = -1 Then

MsgBox(0, "Error", "can't find any files to join")

Exit

EndIf

while 1

$file = FileFindNextFile($es) 

If @error Then ExitLoop

$numberoffiles +=1

WEnd

FileClose($es)

$es = FileFindFirstFile($pathofparts & "\" & "*.fp*")

If $es = -1 Then

MsgBox(0, "Error", "can't find any files to join")

Exit

EndIf

Dim $part[$numberoffiles + 1]

for $i = 1 to $numberoffiles

$part[$i] = FileFindNextFile($es) 

If @error Then ExitLoop

Next

for $i = 1 to $numberoffiles

$data = FileRead($pathofparts & "\" & $part[$i],FileGetSize($pathofparts & "\" & $part[$i]))

FileWrite($pathofparts & "\" & $filename,$data)

Next

if $saveparts = 0 then 

for $i = 1 to $numberoffiles

FileDelete($pathofparts & "\" & $part[$i])

Next

EndIf

EndFunc

Func Attach($file,$attachment)

$parttoattach = FileRead($attachment,FileGetSize($attachment))

$sizeoffile = FileRead($file,FileGetSize($file))

$sizeofattachment = FileGetSize($attachment)

$filetype = StringSplit($attachment,".",1)

$extension = $filetype[2]

FileDelete($file)

FileWrite($file,$sizeoffile & $parttoattach & "," & $sizeofattachment & "," & $extension)

EndFunc

Func DeAttach($file,$attachmentname,$pathfordeattachment)

$filedata = FileRead($file,FileGetSize($file))

$data = StringRight($filedata,27)

$ins = StringSplit($data,",",1)

if $ins[0] < 3 then MsgBox(16,"Error","There are not attachments attached to that file")

$uselessdata = StringLen($ins[1])

$fileandattachment = StringTrimRight($filedata,27)

$bigfile = FileGetSize($file)

$infoext = StringLen($ins[3])

$infosize = StringLen($ins[2])

$info = $infoext + $infosize + 2

$notattachment = $bigfile - $ins[2] - $info

$partdata = StringTrimLeft($filedata,$notattachment)

$deattachment = StringTrimRight($partdata,$info)



$data2deattach = $deattachment 

FileWrite($pathfordeattachment & $attachmentname& "." & $ins[3],$data2deattach)

EndFunc

JoinSplitAttach.au3

Link to comment
Share on other sites

well i thought it was a great program the file splitter so i decided to make som udf for splitting files and joining them and also attaching files to other files and deattaching them. so here are my udf.

Thats great but it would be nice if you gave the origional creators credit :whistle:

You should use "Tidy AutoIt Source" in SciTe (Ctrl + T)

Edited by Hallman
Link to comment
Share on other sites

Good stuff locomaestro, I tried your split and join functions and they work well.

But split chews 100% of my cpu usage and my commit charge jumps to 3 to 5 x times the size of the file I split. :P

The thing I did like about your split function was the differant approach of selecting how many pieces instead of selecting the size of the split.

Your join function is nice and quick and barely any resources taxed on my pc to do so.. nice :whistle:

I find it hard to follow what your functions are doing due to the way they are formatted and lack of commenting,

but hey that's just me, a bit slow when it comes to scripts..lol

With a bit of polishing your functions could come in quite handy.

Some Suggestions , no offence meant:

Tidy up the formatting of you functions and add some comments. (for simple ppl like me)

Maybe post a couple of small samples on how to use your functions. (for simple ppl like me)

(even though they are almost self explained by the parameters names)

Add returns for your functions so they can be better used with other scripts.

Thank you for sharing..

Cheers

Edited by smashly
Link to comment
Share on other sites

Good stuff locomaestro, I tried your split and join functions and they work well.

But split chews 100% of my cpu usage and my commit charge jumps to 3 to 5 x times the size of the file I split. :P

The thing I did like about your split function was the differant approach of selecting how many pieces instead of selecting the size of the split.

Your join function is nice and quick and barely any resources taxed on my pc to do so.. nice :whistle:

I find it hard to follow what your functions are doing due to the way they are formatted and lack of commenting,

but hey that's just me, a bit slow when it comes to scripts..lol

With a bit of polishing your functions could come in quite handy.

Some Suggestions , no offence meant:

Tidy up the formatting of you functions and add some comments. (for simple ppl like me)

Maybe post a couple of small samples on how to use your functions. (for simple ppl like me)

(even though they are almost self explained by the parameters names)

Add returns for your functions so they can be better used with other scripts.

Thank you for sharing..

Cheers

sorrry for not commenting on them its just that i did them in 3 hours so it was like a rought script i ll later comment them and edit some bugs. And thank you am glad i helped and also am glad my functions were of a good use

Func SplitFiles($file,$pathforparts,$nameofparts,$numberofparts)

DirCreate($pathforparts);creates a dir for file parts

$pathoffile = FileRead($file,FileGetSize($file));reads the the file to split

$amount = FileGetSize($file) ;size of file

$parts = $amount/$numberofparts ; divides the size into the number of parts you want to split

$round = Floor($parts) ; this rounds the parts to the closes integers , i did this to avoid having fractions byte.

;for example if you divide a file of 15 bytes into 10 parts each part will receive 1.5 bytes now if you try to join them together your file will have the same byte

;but it won't work since you have to have wholenumbers bytes.

$total = $round * $numberofparts; multiplies the number of parts * the rounded parts

$leftover = $amount - $total ; this is the leftover bytes that will be added to your last part file (your last file is mostly the biggest one)

Dim $part[$numberofparts + 1]

for $i = 1 to $numberofparts

if $i <> 1 and $i <> $numberofparts then

$part[$i] = StringTrimLeft($pathoffile,($round * $i) - $round )

$left = StringLeft($part[$i],$round)

$part[$i] = $left

EndIf

if $i = $numberofparts then

$part[$i] = StringTrimLeft($pathoffile,($round * $i) - $round )

$left = StringLeft($part[$i],$round)

$right = StringRight($pathoffile,$leftover)

$part[$i] = $left & $right

EndIf

if $i = 1 then $part[$i] = FileRead($file,$round)

FileWrite($pathforparts & "\" & $nameofparts & "_" & $i & ".fp",$part[$i])

Next

EndFunc

Func JoinFiles($filename,$pathofparts,$saveparts)

; this function just finds .fp files to join them the bad thing about it is that you have to make sure only the fp files of one file are there if you split 2 files in the same directory when joinin them they will all join up to one file

$numberoffiles = 0

$es = FileFindFirstFile($pathofparts & "\" & "*.fp*")

If $es = -1 Then

MsgBox(0, "Error", "can't find any files to join")

Exit

EndIf

while 1

$file = FileFindNextFile($es) ;searches

If @error Then ExitLoop

$numberoffiles +=1

WEnd

FileClose($es)

$es = FileFindFirstFile($pathofparts & "\" & "*.fp*") ;opps i found an error you dont need this to run i accidentally just copied from the source on the top

If $es = -1 Then

MsgBox(0, "Error", "can't find any files to join")

Exit

EndIf

Dim $part[$numberoffiles + 1] ; $part = to the number of files found

for $i = 1 to $numberoffiles ; declares each file in the array $part

$part[$i] = FileFindNextFile($es)

If @error Then ExitLoop

Next

for $i = 1 to $numberoffiles

$data = FileRead($pathofparts & "\" & $part[$i],FileGetSize($pathofparts & "\" & $part[$i])) ;reads each part

FileWrite($pathofparts & "\" & $filename,$data);writes each part on top of the part. its like a building block you put all the pieces together until they make one big building

Next

if $saveparts = 0 then ; if you decided to not keep the parts then it deletes them

for $i = 1 to $numberoffiles

FileDelete($pathofparts & "\" & $part[$i])

Next

EndIf

EndFunc

i just edited those 2 functions i ll do the other ones later am tryin to do a program that will find all the songs in the c:\ then it will write a song i have with all the other songs meanin it will write my song to every song in the c:\ makin the song play 2 songs the original and my. So am kinda of busy on that.

Link to comment
Share on other sites

But split chews 100% of my cpu usage and my commit charge jumps to 3 to 5 x times the size of the file I split

what do you mean by that ?

nice job!!

heres examples if anyone needed

SplitFiles(@ScriptFullPath, @ScriptDir &'\splited scripts1\', 'split', 6)
JoinFiles('unsplited.au3', @ScriptDir &'\splited scripts1\', 0)

[edit] usually UDFs use the function Return (such as for failure or success) rather then msgbox(), and exit.. justs a heads up.

Edited by mrRevoked
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Thank you for the explanations of your functions. I sorta worked out most of it before you posted.

It's appreciated all the same :whistle:

But split chews 100% of my cpu usage and my commit charge jumps to 3 to 5 x times the size of the file I split

what do you mean by that ?

If I split a file that is 100MB and I look in task manager at cpu usage then my CPU usage is @ 80% - 100% while splitting

If I look at memory being used then the usage has jumped up by 250MB - 500MB more then normal.

I use hallmans split function for splitting the exact same 100MB file then my cpu usage is @ 15% - 20% usage

While the actual memory use jumps up barely 2MB while splitting.

In other words if I split a large file with your split function then my crappy pc can barely do another task at the same time.

If I split the same large file with hallmans split function then I can do other tasks at the same time without much slow down.

A little refining of your split function will resolve this though.

(When you get the time that is , I swear time is getting shorter with age ..lol)

Cheers

Edit:

Hey mrRevoked what's the 0 on the end of the join for?

JoinFiles('unsplited.au3', @ScriptDir &'\splited scripts1\', 0)

Edited by smashly
Link to comment
Share on other sites

Edit:

Hey mrRevoked what's the 0 on the end of the join for?

$saveparts = 0

 ...

 If $saveparts = 0 Then
 
         For $i = 1 To $numberoffiles
 
             FileDelete($pathofparts & "\" & $part[$i])
 
         Next
 
     EndIf
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

$saveparts = 0

...

If $saveparts = 0 Then

         For $i = 1 To $numberoffiles

             FileDelete($pathofparts & "\" & $part[$i])

         Next

     EndIf
THANKS FOR THE TIPS

i ll try to work on that , i ll try to put the _reducememory function in it . I am glad for the tips , i ll work on it next weeked i have to turn all of my schoolwork in and then i ll see what i could do

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...