I have a file test.txt and it contains text on several lines
Code: Select all
1. test1
2. test2
3. test3
3. test4
4. test5
I need to move the first 10 kilobytes to the end of the file and then return to the original state before the change
Thanks!
Code: Select all
1. test1
2. test2
3. test3
3. test4
4. test5
Code: Select all
Function savefile(filename As String,p As String) As String
Dim As Long n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,p
Close
Else
Print "Unable to save " + filename:Sleep:End
End If
Return filename
End Function
Function loadfile(filename As String) As String
Dim As Long f=Freefile
If Open (filename For Binary Access Read As #f)=0 Then
Dim As String text
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
Return text
Else
Print filename;" not found":Sleep:End
End If
End Function
Function pre_pend(filename As String,txt As String) As String
Dim As String s=loadfile(filename)
If Len(s) Then savefile(filename,txt+s)
Return filename
End Function
Function ap_pend(filename As String,txt As String) As String
Dim As String s=loadfile(filename)
If Len(s) Then savefile(filename,s+txt)
Return filename
End Function
'==========================================================
Sub startToend(filename As String,size As Long)
Var L=loadfile("teststring")
Var fpart=Mid(L,1,size)
Var epart=Mid(L,size+1)
savefile("teststring",epart)
ap_pend("teststring",fpart)
End Sub
Sub endTostart(filename As String,size As Long)
Var L=loadfile("teststring")
Var fpart=Mid(L,1,Len(L)-size)
Var epart=Mid(L,Len(L)-size+1)
savefile("teststring",fpart)
pre_pend("teststring",epart)
End Sub
Dim As String s
For n As Long=1 To 1000
s+=Str(n)+". "+"test"+Str(n)+Chr(10)
Next
savefile("teststring",s)
startToend("teststring",10000)
Shell "notepad teststring"
endTostart("teststring",10000)
Shell "notepad teststring"
Sleep
Kill ("teststring")
Thanks!dodicat wrote: ↑Sep 06, 2022 19:07 Keep it simple with strings I would say.Code: Select all
Function savefile(filename As String,p As String) As String Dim As Long n=Freefile If Open (filename For Binary Access Write As #n)=0 Then Put #n,,p Close Else Print "Unable to save " + filename:Sleep:End End If Return filename End Function Function loadfile(filename As String) As String Dim As Long f=Freefile If Open (filename For Binary Access Read As #f)=0 Then Dim As String text If Lof(f) > 0 Then text = String(Lof(f), 0) Get #f, , text End If Close #f Return text Else Print filename;" not found":Sleep:End End If End Function Function pre_pend(filename As String,txt As String) As String Dim As String s=loadfile(filename) If Len(s) Then savefile(filename,txt+s) Return filename End Function Function ap_pend(filename As String,txt As String) As String Dim As String s=loadfile(filename) If Len(s) Then savefile(filename,s+txt) Return filename End Function Dim As String s For n As Long=1 To 1000 s+=Str(n)+". "+"test"+Str(n)+Chr(10) Next savefile("teststring",s) Var L=loadfile("teststring") Var fpart=Mid(L,1,10000) Var epart=Mid(L,10001) savefile("teststring",epart) ap_pend("teststring",fpart) Print "Length of chunk to append ";Len(fpart) Shell "notepad teststring" Sleep 'savefile("teststring",L) '' original if required Kill ("teststring")
Only you need to return the modified file to its original state and not create and just save itdodicat wrote: ↑Sep 06, 2022 19:07 Keep it simple with strings I would say.Code: Select all
Function savefile(filename As String,p As String) As String Dim As Long n=Freefile If Open (filename For Binary Access Write As #n)=0 Then Put #n,,p Close Else Print "Unable to save " + filename:Sleep:End End If Return filename End Function Function loadfile(filename As String) As String Dim As Long f=Freefile If Open (filename For Binary Access Read As #f)=0 Then Dim As String text If Lof(f) > 0 Then text = String(Lof(f), 0) Get #f, , text End If Close #f Return text Else Print filename;" not found":Sleep:End End If End Function Function pre_pend(filename As String,txt As String) As String Dim As String s=loadfile(filename) If Len(s) Then savefile(filename,txt+s) Return filename End Function Function ap_pend(filename As String,txt As String) As String Dim As String s=loadfile(filename) If Len(s) Then savefile(filename,s+txt) Return filename End Function Dim As String s For n As Long=1 To 1000 s+=Str(n)+". "+"test"+Str(n)+Chr(10) Next savefile("teststring",s) Var L=loadfile("teststring") Var fpart=Mid(L,1,10000) Var epart=Mid(L,10001) savefile("teststring",epart) ap_pend("teststring",fpart) Print "Length of chunk to append ";Len(fpart) Shell "notepad teststring" Sleep 'savefile("teststring",L) '' original if required Kill ("teststring")
I have modified the code (added two subs to automate the process)