Re: Can this be written better?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Ron" <Ron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:3BA97A3D-12F7-495C-A7E6-86AEF18B220A@xxxxxxxxxxxxxxxx

A string from my program includes a file ext which I want
to exclude, so I have written this code, can this be done better?
Do Until xCount = xLen1 + 1 ' Run until fullstop is found or end of string

Running through the string character by character is very slow. VB has various functions which will search for a substring inside a string and return its position for you. There are two main such functions, one of which searches from the start of the string looking forwards and the other searches from the end looking backwards. Check out the Instr and the IntrRev functions (type one of those names into some code and hit the F1 key). There are also functions that will return a specified number of characters of a string for you, from either the left side, right side or somewhere in the middle. Have a look at the Left$, Right$ and Mid$ functions.

Also, you haven't actually made it quite clear what you are trying to do. Your description implies that you want to remove a file's extension whatever it is, but your code is specifically looking for the .jpg extension and will leave other extensions alone. Which do you want to do? If you only want to remove the specific extension ".jpg" and leave other extensions alone then you could use:

If Right$(xPart2, 4) = ".jpg" Then
xPart2 = Left$(xPart2, Len(xPart2) - 4)
End If

However, if you want to remove the file extension no matter what it is then you need to do it differently and you also need to take into account that on modern versions of Windows a file extension can have more (or less) than three characters. Here is some code that will remove a file extension for you, no matter what it is (subject to the limitations mentioned in the next few lines). The code is something I've just knocked up very quickly and it takes into account only what "I have in my own head at the moment" about file extensions, so you will need to work on it. For example, I know that a space is not permitted in a file extension, so my code takes that into account, but there are almost certainly other characters that are not permitted in a file extension so you will need to deal with them as well. It'll be a good exercise for you. By the way, there will almost certainly be an API routine that will do this job for you, but looking at your sample code I imagine that you will be far more interested in seeing how it is done using standard VB functions so I have not bothered with the API stuff.

p1 = InStrRev(xPart2, ".")
p2 = InStrRev(xPart2, " ")
If p1 > 0 And p2 < p1 Then
xPart2 = Left$(xPart2, p1 - 1)
End If

Mike



.



Relevant Pages

  • Re: separate filename characters and numbers
    ... I saw strtok,strcmp,strfind,regexp but as i understood it requires a character or a set of characters to compare the entire string. ... Then use the FILEPARTS command to split each file name into ... path, filename and file extension. ... matches the files you look for, use a regular expression to ...
    (comp.soft-sys.matlab)
  • Re: Automate task w/VBA ?
    ... last 6 characters before the file extension which function ... setup Excel to automatically place a "Total Files ... >> notepad, strip these characters and manually count the ...
    (microsoft.public.word.vba.general)
  • Re: Reading a very large text file
    ... I have attached a sample file. ... All lines may not contain the same number of characters. ... The pattern is as follows: ... The file extension is .xls in this example. ...
    (comp.lang.labview)
  • Re: Cannot Update
    ... dot fds file extension when we went to NT too. ... Importing or Linking a Text File Fails for a File That Does Not ... Be sure that the path string is properly constructed. ... The length of the path and file name string cannot exceed 64 characters. ...
    (microsoft.public.access.modulesdaovba)
  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)