Sign in to follow this  
Followers 0
RazerM

Return File Extension

29 posts in this topic

Is it possible to get the file extension of the file selected with a FileOpenDialog?

Thanks


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites



$File = FileOpenDialog

$ext = stringRight($File, 4)

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Thanks for helping me, I'm quite new to AutoIt also is there a way to get the filename because your script requires it to be a certain length.


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

no it doesn't require the string to be a certain length..

it gets the last 4 characters in the string $file name

the last 4 characters are ".ext"... ".txt", ".exe" etc

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

no i was asking if there is a way to get a filename because to get the extension it gets the last four characters but a filename could be any length.


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

StringBetween($file, "\", ".")

look for 'StringBetween' here

http://www.autoitscript.com/forum/index.ph...70&hl=Valuater#

8)

Hi or maybe use this func

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$TestPath = _PathSplit(@ScriptFullPath, $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath,"Demo _PathSplit()")

$TestPath[4] should do the trick.

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

SciTE is reporting an error, it doesnt recognise stringbetween


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites

SciTE is reporting an error, it doesnt recognise stringbetween

Hi, have you tried my little script?

Here is StringBetween()

Func stringbetween($str, $start, $end)
    $pos = StringInStr($str, $start)
    If Not @error Then
        $str = StringTrimLeft($str, $pos + StringLen($start) - 1)
        $pos = StringInStr($str, $end)
        If Not @error Then
            $str = StringTrimRight($str, StringLen($str) - $pos + 1)
            Return $str
        EndIf
    EndIf
EndFunc

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

the use is........

$File = OpenFileDialog()

stringbetween($File, "\", ".")

Func stringbetween($str, $start, $end)

$pos = StringInStr($str, $start)

If Not @error Then

$str = StringTrimLeft($str, $pos + StringLen($start) - 1)

$pos = StringInStr($str, $end)

If Not @error Then

$str = StringTrimRight($str, StringLen($str) - $pos + 1)

Return $str

EndIf

EndIf

EndFunc

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I want to join in the fun!! :o

$fz_FilePath = FileOpenDialog('Choose a file', @ScriptDir, 'All Files (*.*)')

Local $Extension = Split_Path_Get_Extension($fz_FilePath)

MsgBox(0, 'Test', 'Extension was: ' & $Extension)

Func Split_Path_Get_Extension($sFilePath)
    Local $BackSlashLast = StringInStr($sFilePath, '\', 0, -1)
    Local $ForwardSlashLoc = StringInStr($sFilePath, '/', 0, -1)
    If $ForwardSlashLoc > $BackSlashLast Then $BackSlashLast = $ForwardSlashLoc
    $Sections = StringTrimLeft($sFilePath, $BackSlashLast)
    Return StringTrimLeft($Sections, StringInStr($Sections, '.'))
EndFunc

Edit:

Chit, you just want the file extension, not change it!! :geek:

Edit2:

Ok, not as much fun as the other ;)... fixed for extension.

Edited by SmOke_N
1 person likes this

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I'm becoming a big fan of StringSplit. Try the following.

The first line splits up the file path into an array of drive, foldernames,filename,extension and then the last two lines pick out the extension and the filename.

$parts = StringSplit($file,"\.",0)

$ext = $parts[$parts[0]]

$filename = $parts[$parts[0] - 1]

Edited by SlimJim

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I'm becoming a big fan of StringSplit.

$parts = StringSplit($file,"\.",0)

$ext = $parts[$parts[0]]

$filename = $parts[$parts[0] - 1]

To make the above useful, you need to extend it to be able to put it back together if UNC pathnames (\\), directory names with periods, or filenames with multiple periods .

i.e. \\server\sharename\path1\path2.ext\yyyymmdd.hhmm.log

When you've got something that works, you should be able to have a StringFormat() line that puts it back together exactly as you took it apart w/o having to refer back to $file.

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

To make the above useful, you need to extend it to be able to put it back together if UNC pathnames (\\), directory names with periods, or filenames with multiple periods .

i.e. \\server\sharename\path1\path2.ext\yyyymmdd.hhmm.log

When you've got something that works, you should be able to have a StringFormat() line that puts it back together exactly as you took it apart w/o having to refer back to $file.

Hmm, mine was the same way... this should work though:
$fz_FilePath = FileOpenDialog('Choose a file', @ScriptDir, 'All Files (*.*)')
Local $Extension = _Extension($fz_FilePath)

MsgBox(0, 'Test', 'Extension was: ' & $Extension)

Func _Extension($sz_File)
    Local $LastDotLoc = StringInStr($sz_File, '.', 0, -1)
    Local $BackSlashLast = StringInStr($sz_File, '\', 0, -1)
    Local $ForwardSlashLoc = StringInStr($sz_File, '/', 0, -1)
    If $ForwardSlashLoc > $BackSlashLast Then $BackSlashLast = $ForwardSlashLoc
    If $BackSlashLast > $LastDotLoc Then Return ''
    Return StringTrimLeft($sz_File, $LastDotLoc - 1)
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Why are you re-inventing the wheel? Somebody has already pointed out to using _PathSplit() but nobody is realizing it's designed to do the grunt work for you and it's not really suitable for being called directly:

#include <File.au3>

Func _FileGetExt($sPath)
    Local $NULL, $sExt
    _PathSplit($sPath, $NULL, $NULL, $NULL, $sExt)
    Return $sExt
EndFunc

That's it, you're done. UNC paths are supported. Both forward and backward slashes are supported. The _PathSplit() function has been in use for a couple years so it's stable and proven.

It disturbs me that the veterans who are constantly helping out people couldn't jump to the conclusion that instead of writing a new function from scratch, re-use an existing function to do the hard-stuff.

Share this post


Link to post
Share on other sites

Why are you re-inventing the wheel? Somebody has already pointed out to using _PathSplit() but nobody is realizing it's designed to do the grunt work for you and it's not really suitable for being called directly:

#include <File.au3>

Func _FileGetExt($sPath)
    Local $NULL, $sExt
    _PathSplit($sPath, $NULL, $NULL, $NULL, $sExt)
    Return $sExt
EndFunc

That's it, you're done. UNC paths are supported. Both forward and backward slashes are supported. The _PathSplit() function has been in use for a couple years so it's stable and proven.

It disturbs me that the veterans who are constantly helping out people couldn't jump to the conclusion that instead of writing a new function from scratch, re-use an existing function to do the hard-stuff.

That's the 2nd time I've forgotten that existed, I've written my own 2 or 3 times for various things... So I'm guilty as charged, saw it written even above and it didn't register... just thought I'd jump in the cock fight and got roosted ... I mean roasted :o !

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Why are you re-inventing the wheel? Somebody has already pointed out to using _PathSplit() but nobody is realizing it's designed to do the grunt work for you and it's not really suitable for being called directly:

#include <File.au3>

Func _FileGetExt($sPath)
    Local $NULL, $sExt
    _PathSplit($sPath, $NULL, $NULL, $NULL, $sExt)
    Return $sExt
EndFunc

That's it, you're done. UNC paths are supported. Both forward and backward slashes are supported. The _PathSplit() function has been in use for a couple years so it's stable and proven.

It disturbs me that the veterans who are constantly helping out people couldn't jump to the conclusion that instead of writing a new function from scratch, re-use an existing function to do the hard-stuff.

I'm neither a veteran nor an expert and read these forums in the hope of learning something. I have tried the function above (copy and paste) and get an unknown function error for _PathSplit. I have checked my version of file.au3and that does not mention _PathSplit. Can somebody tell me what the problem is please?

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I'm neither a veteran nor an expert and read these forums in the hope of learning something. I have tried the function above (copy and paste) and get an unknown function error for _PathSplit. I have checked my version of file.au3and that does not mention _PathSplit. Can somebody tell me what the problem is please?

Hi,

what about that?

#include <file.au3>
#include <array.au3>
Dim $szDrive, $szDir, $szFName, $szExt 
Dim $pathArray = _ArrayCreate("C:\folder 1\folder 2\autoit.exe", "d:\hello.jpg", "e:\test\test.mpg", "\hello\hello.hello", "/slash/slash.dot")

for $i = 0 To UBound($pathArray)-1
$array = _PathSplit ($pathArray[$i], $szDrive, $szDir, $szFName, $szExt)
MsgBox(0, "Demo _PathSplit() Nr " & $i & " ", $szExt)
Next

Just to test the function.

Edit: Changed the endterm of the For Next from 4 to UBound($pathArray)-1

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

I'm neither a veteran nor an expert and read these forums in the hope of learning something. I have tried the function above (copy and paste) and get an unknown function error for _PathSplit. I have checked my version of file.au3and that does not mention _PathSplit. Can somebody tell me what the problem is please?

I think _PathSplit() is in the Beta File.au3... I could be wrong because Valik had said it had been around a while, but I had not noticed it until like 2 months ago. (Probably why I forgot it, because I already made my own by the time I noticed it).

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

It's only in the beta. However, the function has existed for far longer than that as I've used it for years.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0