Change extention of file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaiful
    New Member
    • Oct 2007
    • 89

    Change extention of file

    Hi all I have a problem with extention of file. such as:

    Dim s, s1 As String
    s = "Test.doc"
    i want get the value of s is in s1 as "Test.txt" such as :

    s1 = "Test.txt"

    so, how i can get that solution in easy way pls? Is there any bulit in function to saperate a string by any special char such as (.) dot, or change the extention only? thx advance
  • sloanthrasher
    New Member
    • Jan 2008
    • 1

    #2
    You can use the instr function to locate the ".", and then the left function to return just the name part (less the extention).

    Dim s, s1

    s = "test.xxx"
    s1 = left(s & ".",instr(s & ".",".")-1)
    ' s1 = "test" at this point
    s1 = s1 & ".txt"
    ' s1 = "test.txt" here.

    Hope that helps....

    Comment

    • shaiful
      New Member
      • Oct 2007
      • 89

      #3
      Thanks very much for ur solution, my problem actually with string split such as

      dim s ,s1(100) as string
      s='x1.txt;x2.tx t;x3.txt;x4.txt "
      Here semicolon is saperation of each file in a string, I want to split the file name from this string.
      output: s1(0)=x1.txt
      s1(1)=x2.txt ......

      Its mean how i can search the caharcter of a string? If ant idea which will be graet help, rthx again

      Comment

      • lotus18
        Contributor
        • Nov 2007
        • 865

        #4
        replace . with dot

        Originally posted by sloanthrasher
        You can use the instr function to locate the ".", and then the left function to return just the name part (less the extention).

        Dim s, s1

        s = "test.xxx"
        s1 = left(s & ".",instr(s & ".",".")-1)
        ' s1 = "test" at this point
        s1 = s1 & ".txt"
        ' s1 = "test.txt" here.

        Hope that helps....
        How about if the filename contains dot (e.g. MyFile.YourFile .doc)?

        Rey Sean

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          If you use FileSystemObjec t, the File object allows you to see the name and type. As for accessing the extension, I think you'll find that there's a variation of the Instr() function which will start from the right so you can easily find the last dot. I just can't remember it right now.

          Comment

          • vikas000000a
            New Member
            • Jan 2008
            • 46

            #6
            Easiest solution to this problem I have is:

            Code:
            s="text1.doc, text2.doc, xyz"
            s1=replace(s, ".doc", ".txt")
            That's it. Also, it solves the problem of a file having a dot(.) as part of its name because here we are replacing ".doc" with ".txt".

            Regards

            Comment

            • lotus18
              Contributor
              • Nov 2007
              • 865

              #7
              Hi

              Reminder:

              Keep in mind that changing of file extension of some files (e.g. jpg, mp3, etc) could make the file becomes useless.

              Rey Sean

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by vikas000000a
                ...That's it. Also, it solves the problem of a file having a dot(.) as part of its name because here we are replacing ".doc" with ".txt".
                However, it could still have problems with some odd filenames. For instance "This.document.name.will .be.destroyed.t xt”. I think Replace is too indiscriminate. Besides, what if you want to know what the extension was before the change?

                Comment

                • vikas000000a
                  New Member
                  • Jan 2008
                  • 46

                  #9
                  Originally posted by Killer42
                  However, it could still have problems with some odd filenames. For instance "This.document.name.will .be.destroyed.t xt”. I think Replace is too indiscriminate. Besides, what if you want to know what the extension was before the change?
                  My reply was as per the conditions originally imposed by Shaiful:
                  He wanted to get s1="Test.txt" given that s="Test.doc". This is achieved successfully with the solution I provided.

                  To change the extensions of very rare file names of the kind you provided, following modification can be done:

                  Code:
                  s = "This[U].doc[/U]ument.name.will.be.destroyed.doc”
                  s1 = left(s, len(s) - 4) + ".txt"
                  And if you want to know what the extension was before the change, you can use:

                  Code:
                  s = "This[U].doc[/U]ument.name.will.be.destroyed.doc”
                  s1 = s + ".txt"
                  This all depends on your requirement and the level of robustness you want in your code.

                  Regards

                  Comment

                  • vikas000000a
                    New Member
                    • Jan 2008
                    • 46

                    #10
                    Edited

                    Sorry, forgot to include split part of it. Following is entire code:

                    Code:
                        s = "1.doc;2.doc;3.doc;4.doc"
                        s1 = Split(Me.Text1.Text, ";")
                        For i = 0 To UBound(s1)
                            ' If you don't want to retain old file extensions:
                             s1(i) = left(s1(i), len(s1(i) - 4) + ".txt"
                    
                            ' If you want to retain old file extensions:
                            s1(i) = s1(i) + ".txt"
                        Next i
                    That's it.
                    Last edited by vikas000000a; Jan 3 '08, 08:19 AM. Reason: Forgot to insert code tags and some typographical error

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by vikas000000a
                      My reply was as per the conditions originally imposed by Shaiful...
                      Yes, the specs are quite vague.

                      Comment

                      Working...