The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Bieb on March 28, 2005, 11:18:35 PM

Title: Appending File Extension
Post by: Bieb on March 28, 2005, 11:18:35 PM
What's the best way to check and see if a string has a given extension (like .bmp) at the end of it and append that extension if it doesn't?
Title: Re: Appending File Extension
Post by: Faiseur on March 28, 2005, 11:56:02 PM
Hi,

It is simple if you use the API "PathAddExtension":


fn PathAddExtension, addr String, ".bmp"


Note:

a.if the String have an extension, no extension is added.

b. If the String no have extension, extension is added.

:-)


Add:
include shlwapi.inc
includelib shlwapi.lib


If you want to remove the extension (if there is...) use "PathRemoveExtension" (invoke PathRemoveExtension,addr String)





Title: Re: Appending File Extension
Post by: Bieb on March 29, 2005, 01:26:13 AM
Wow, that's great.  Thanks.
Title: Re: Appending File Extension
Post by: Tedd on March 29, 2005, 10:21:47 AM
Hacky way:
- get string length
- check that last four characters = ".bmp"
- if not, append them :wink
Title: Re: Appending File Extension
Post by: sluggy on March 29, 2005, 11:10:09 AM
Quote from: Robert Bieber on March 28, 2005, 11:18:35 PM
What's the best way to check and see if a string has a given extension (like .bmp) at the end of it and append that extension if it doesn't?
Ahhhh, but what is an extension? "yourfile.bmp.bmp.bmp" is a perfectly legal name, as is "yourfile.areallylongextension", as is "afilenamewithnoextensionatall".

Sorry, couldn't resist going all Zen, "keepin' it real" as Ali G would say  :cheekygreen: :eek
Title: Re: Appending File Extension
Post by: Tedd on March 29, 2005, 11:16:04 AM
Quote from: sluggy on March 29, 2005, 11:10:09 AM
Quote from: Robert Bieber on March 28, 2005, 11:18:35 PM
What's the best way to check and see if a string has a given extension (like .bmp) at the end of it and append that extension if it doesn't?
Ahhhh, but what is an extension? "yourfile.bmp.bmp.bmp" is a perfectly legal name, as is "yourfile.areallylongextension", as is "afilenamewithnoextensionatall".

Sorry, couldn't resist going all Zen, "keepin' it real" as Ali G would say :cheekygreen: :eek

Aaah, but the question is, little grasshopper, whether the file has a particular extension - "blah.bmp.bmp.blah.bmp" is a perfectly legitimate filename, and it ends with ".bmp" which makes it a bmp file (as far as windows is concerned.)
Title: Re: Appending File Extension
Post by: P1 on March 29, 2005, 03:05:33 PM
Not every file that ends in .bmp is a bmp format file. 

Regards,  P1  :8)
Title: Re: Appending File Extension
Post by: Tedd on March 29, 2005, 03:20:49 PM
Quote from: P1 on March 29, 2005, 03:05:33 PM
Not every file that ends in .bmp is a bmp format file.

That's why I said "(as far as windows is concerned)"  :P

Obviously you can't tell until you actually open it and check its format.
Title: Re: Appending File Extension
Post by: RuiLoureiro on March 29, 2005, 08:55:07 PM
Quote
Ahhhh, but what is an extension? "yourfile.bmp.bmp.bmp" is a perfectly legal name, as is "yourfile.areallylongextension", as is "afilenamewithnoextensionatall".

Sluggy, but he can test all .bmp begining at the end until the last «.». Why not ?
Title: Re: Appending File Extension
Post by: pbrennick on March 30, 2005, 02:33:41 AM
I have a telephone in the parlor and another in  the bedroom.  The one in the bedroom is an extension.

Paul
Title: Re: Appending File Extension
Post by: thomasantony on March 31, 2005, 02:59:42 AM
Hi,
  Since it is 4 chars get the last DWORD from the buffer i na register. Do a

cmp eax,'pmb.' ; .bmp in reverse due to intel byte ordering


Thomas
Title: Re: Appending File Extension
Post by: QvasiModo on March 31, 2005, 08:21:23 PM
There's also PathRemoveExtension and PathReplaceExtension to prevent duplicated extensions.