Results 1 to 5 of 5

Thread: [RESOLVED] Delete data from end of file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    72

    Resolved [RESOLVED] Delete data from end of file

    Hello

    I have JPG images with data stored at the end of them. My goal is to simply remove this data from the end of the file. The extra data is in plain text. The JPG images can still be viewed as normal with this data in them.

    The plain text in the end of the file starts with the beginning mark of
    "--------data---------", a line break, and then the data. Then there is
    the end mark of the string
    "-------enddata-------"

    How can I remove all of this this data without destroying the file? Is there a way to do it without completely copying the file?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Delete data from end of file

    I think something like this should do:
    vb.net Code:
    1. Dim FileData As String
    2. FileData = IO.File.ReadAllText("C:\myfile.jpg")
    3. If FileData.Contains("--------data---------") Then
    4.     FileData = FileData.Substring(FileData.IndexOf("--------data---------"))
    5. End If
    6. IO.File.WriteAllText("C:\myfile1.jpg", FileData)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    72

    Re: Delete data from end of file

    Thanks for the reply, but it doesn't do what I need.

    While your post successfully replaces the JPG with just the text, I was looking to delete the text from the JPG while preserving the JPG data in the file. My biggest issue is that the data somehow changes in the process leaving a unusable JPG file.

    Any more tips would be great!

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Delete data from end of file

    Quote Originally Posted by Bohlas_DuBhunkus View Post
    Thanks for the reply, but it doesn't do what I need.

    While your post successfully replaces the JPG with just the text, I was looking to delete the text from the JPG while preserving the JPG data in the file. My biggest issue is that the data somehow changes in the process leaving a unusable JPG file.

    Any more tips would be great!
    If you remove that text using notepad (or any other text editor), does it work?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    72

    Re: Delete data from end of file

    No. Using notepad and saving somehow kills the file.

    I figured out how to get what I need done. Heres the code. Thanks to the original author of the byte/string conversion functions.

    code Code:
    1. Public Sub DeleteINFO(ByVal InputtedFileName As String)
    2. ' Sepp is the ---------metadatastart---------- string (simply run through the string to byte array function
    3.         Dim Sepp As String = "45,45,45,45,45,45,45,45,45,45,109,101,116,97,100,97,116,97,115,116,97,114,116,45,45,45,45,45,45,45,45,45,45,45"
    4.         Dim EntireFileBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(InputtedFileName)
    5.         Dim EntireFileText As String = ArrayToString(EntireFileBytes)
    6.  
    7.             EntireFileText = Strings.Left(EntireFileText, InStr(EntireFileText, Sepp))
    8.             My.Computer.FileSystem.WriteAllBytes(InputtedFileName, StringToArray(EntireFileText), False)
    9.  
    10.     End Sub
    11.  
    12. '--------------------------------------------------------------
    13. ' byte array to string, and string to byte array functions
    14.     Private Shared Function ArrayToString(ByVal bytes() As Byte, Optional ByVal format As String = Nothing) As String
    15.         If bytes.Length = 0 Then Return String.Empty
    16.         Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
    17.         For Each b As Byte In bytes
    18.             sb.Append(b.ToString)
    19.             sb.Append(",")
    20.         Next
    21.         sb.Length -= 1
    22.         Return sb.ToString()
    23.     End Function
    24.  
    25.     Private Shared Function StringToArray(ByVal s As String, Optional ByVal style As System.Globalization.NumberStyles = Nothing) As Byte()
    26.         If s.Length = 0 Then Return New Byte() {}
    27.         Dim values() As String = s.Split(","c)
    28.         Dim bytes(values.Length - 1) As Byte
    29.         For index As Integer = 0 To values.Length - 1
    30.             bytes(index) = Byte.Parse(values(index), style)
    31.         Next
    32.         Return bytes
    33.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width