Link to home
Start Free TrialLog in
Avatar of Scan_25
Scan_25

asked on

function to remove file extensions

I need advice on where to start on making a program in vb that will auto take off file extensions. You see I need to point it at a specified directory and have it take all the file extensions off the files. I have tif files that are named like this 01000001.001.tiff all of them have eight then three then the file extension. The interfacing program doesn’t like the file extensions so I have to manually take each one off as they get uploaded to us. I’m trying to make a little vb program to do this for me. I just can’t see it, I know it should be easy and I have went over my vb books and can not find anything useful there “for this”. So what I'm looking for is a simple example function to do something like this. If there is a dos cammand i can use. That would work :-)
Avatar of 1mak
1mak

is DOS:

rename *.tif *.

basically renames all files that end in ".tif" to have no extension...
is DOS:

rename *.tif *.

basically renames all files that end in ".tif" to have no extension...
(sorry for the double post? not sure why it happened?)
Hi Scan_25,

This is quite simple:

Dim strFile As String
strFile = Dir("c:\myfolder\*.tif")
Do While strFile <> ""
  Name strFile, Left(strFile,Len(strFile) - 1)
  strFile = Dir()
Loop

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
Avatar of Mikal613

Private Sub Command1_Click()
      Text1.Text = GetFileNAme(Text1.Text)
End Sub

Private Function GetFileNAme(Str As String) As String
        GetFileNAme = Left(Str, InStrRev(Text1.Text, ".") - 1)
End Functio
ASKER CERTIFIED SOLUTION
Avatar of wsteegmans
wsteegmans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scan_25

ASKER

All of your answers are great you guys a really great I don&#8217;t know if I can split points but wsteegmans gave me the most complete answer so I think I should give them to him are there any objections? Going ones!
I will do this in the next ten minutes if you other guys with the other great answers don&#8217;t mind.
My only complaint with that solution is that it uses the FileSystemObject. This is generally a *BAD* thing! FSO is designed for VBScript where there are no alternatives. With VB you have the native file functions and the opportunity to use API calls. FSO cannot be guaranteed to be available as it is installed by Internet Explorer and can be disabled by a system administrator. Relying on the FSO to provide file-level access is not recommended.
Avatar of Scan_25

ASKER

TimCottee,
 Can you explain how wsteegmans example can be achieved without using FSO?
I can follow the comment of TimCottee ... (a bit)

There are indeed several disadvantages to the FSO. It works for example only with text files. To manipulate binary files, you must use pointers to an address in memory, or byte arrays, which are not supported by the object.

When you read or write a large amount of content, the information stored in the buffer may create a big memory hit. Finally, you cannot manage permissions or file and folder attributes.

But, the big advantage is that it's very easy to work with (for example to create structured Example Code ;-))
It is indeed not guaranteed that it's available on every system, but in the year 2003, this number will be very small.

I use it always to browse through folders, or to copy/move ... without any problem.
Dim strFile As String
strFile = Dir("c:\myfolder\*.tif")
Do While strFile <> ""
  FileCopy strFile, "c:\MyNewFolder\" & Left(strFile,Len(strFile) - 1)
  strFile = Dir()
Loop

Is all that is required to create the copies:

Dim strFile As String
strFile = Dir("c:\myfolder\*.tif")
Do While strFile <> ""
  Kill strFile
  strFile = Dir()
Loop

To delete the originals

Dim strFile As String
strFile = Dir("c:\mynewfolder\*.tif")
Do While strFile <> ""
  FileCopy strFile, "c:\MyFolder\" & Left(strFile,Len(strFile) - 1)
  strFile = Dir()
Loop

To move the renamed ones back again
Avatar of Scan_25

ASKER

Ok, I just ran in to a little more trouble. I&#8217;m going to accept wsteegmans answer but I going to post a new question that is related to this one for further support. TimCottee, if you have time and can answer it award you some extra points from this question.