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:
    Share on facebook
    Share on twitter
    Share on linkedin

    Overview

    The Latest

    microsoft careers

    Top Careers at Microsoft

    Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

    your company's audio

    4 Areas of Your Company Where Your Audio Really Matters

    Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

    chrome os developer mode

    How to Turn on Chrome OS Developer Mode

    Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS