By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Change extention of file

P: 89
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
Jan 2 '08 #1
Share this Question
Share on Google+
10 Replies


P: 1
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....
Jan 2 '08 #2

P: 89
Thanks very much for ur solution, my problem actually with string split such as

dim s ,s1(100) as string
s='x1.txt;x2.txt;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
Jan 2 '08 #3

lotus18
100+
P: 866
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
Jan 2 '08 #4

Expert 5K+
P: 8,434
If you use FileSystemObject, 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.
Jan 2 '08 #5

P: 46
Easiest solution to this problem I have is:

Expand|Select|Wrap|Line Numbers
  1. s="text1.doc, text2.doc, xyz"
  2. s1=replace(s, ".doc", ".txt")
  3.  
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
Jan 2 '08 #6

lotus18
100+
P: 866
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
Jan 2 '08 #7

Expert 5K+
P: 8,434
...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.txt. I think Replace is too indiscriminate. Besides, what if you want to know what the extension was before the change?
Jan 3 '08 #8

P: 46
However, it could still have problems with some odd filenames. For instance "This.document.name.will.be.destroyed.txt. 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:

Expand|Select|Wrap|Line Numbers
  1. s = "This.document.name.will.be.destroyed.doc
  2. s1 = left(s, len(s) - 4) + ".txt"
And if you want to know what the extension was before the change, you can use:

Expand|Select|Wrap|Line Numbers
  1. s = "This.document.name.will.be.destroyed.doc
  2. s1 = s + ".txt"
This all depends on your requirement and the level of robustness you want in your code.

Regards
Jan 3 '08 #9

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

Expand|Select|Wrap|Line Numbers
  1.     s = "1.doc;2.doc;3.doc;4.doc"
  2.     s1 = Split(Me.Text1.Text, ";")
  3.     For i = 0 To UBound(s1)
  4.         ' If you don't want to retain old file extensions:
  5.          s1(i) = left(s1(i), len(s1(i) - 4) + ".txt"
  6.  
  7.         ' If you want to retain old file extensions:
  8.         s1(i) = s1(i) + ".txt"
  9.     Next i
That's it.
Jan 3 '08 #10

Expert 5K+
P: 8,434
My reply was as per the conditions originally imposed by Shaiful...
Yes, the specs are quite vague.
Jan 4 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.