Changing File Extension

Changing File Extension

Question:
not having programmed in any of the BASIC languages, I am wondering if Visual Basic’s string variable is in anyway similar to what a string variable is in C (i.e. is it defined as an array of characters in visual basic?)The reason I am asking this is that – I’d like to take the content of a text box (a file name) and then change the extension on the same file name to something else and put it in another text box – and I am not quite sure how to go about this in visual basic (the help file on the string variable was a bit brief…)

Answer:
VB has a special datatype for String. You don’t have to define it as an array of characters…just do “Dim temp as String” and it’s a String.

The following code assumes:

  • You know that the “divider” character in your filename is a period
  • You don’t know how many characters are in the extension (it will correctly parse file.c, file.au, file.txt, file.html, etc.)

    To test this code, make a form with two textboxes (leave the default names of Text1 and Text2). Paste the following code into the double-click event of Text2. Run the project and try it with various things (or nothing!) in Text1.

    I’ve commented the code heavily, hope everything’s clear.

    Dim flag As Integer  ‘flag is used for the condition in the Do/LoopDim x As IntegerDim temp As String’x is being used as a “counter” variable’we set its length herex = Len(Text1)’next line errortraps to make sure that it’s not an’empty textboxIf x > 0 ThenDo While Not flag ‘as long as flag is False…’next line picks out one character from the string.’the first pass through the loop will get the last’character in the string, the next pass will get’the next-to-the-last, etc. because we decrement’x by one in the next “If/End If” statementtemp = Mid$(Text1, x, 1)If temp = “.” Then  flag = True  x = x – 1Else  x = x – 1End If’the next line gracefully exits the loop if’there’s no period in Text1 at allIf x = 0 Then flag = TrueLoop’the next line first tests to make sure that’there was a period found in Text1… if so’it adds everything to the left of the period’and “.ext” and puts the resultant string into’Text2. You can remove the “If x > 0 Then” if’you think there might be an “extensionless”‘filename in Text1.If x > 0 Then Text2 = Left$(Text1, x) & “.ext”End If

  • Share the Post:
    Heading photo, Metadata.

    What is Metadata?

    What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

    XDR solutions

    The Benefits of Using XDR Solutions

    Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

    AI is revolutionizing fraud detection

    How AI is Revolutionizing Fraud Detection

    Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

    AI innovation

    Companies Leading AI Innovation in 2023

    Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several