Jump to content

[solved] get file extension from a file open dialogue choice


Recommended Posts

How can I get the .ext from a file that is chosen from a fileopendialogue? I know I could use the . as a delimiter some how, but I do not know how to just get that.

Lets say they choose the file c:\users\testuser\desktop\my great screensaver.scr

How can I make a variable that stores just the .scr part, to get what ever that last .something is?

Thanks

Edited by Morthawt
Link to comment
Share on other sites

Something like this ?

ConsoleWrite ( "Extension : " & 'c:\users\testuser\desktop\my great screensaver.scr' & @Crlf )

Func _GetExtByFullPath ( $_FullPath=@ScriptFullPath )
    $_FileName = StringSplit ( $_FullPath, '.' )
    If Not @error Then
        Return $_FileName[$_FileName[0]]
    Else
        Return 0
    EndIf
EndFunc ;==> _GetExtByFullPath ( )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Also if you want to check if a file is a .scr then use

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Here 2 other ways:

$path = "c:\users\testuser\desktop\my great screensaver.scr"
$ext1 = StringRegExpReplace($path, ".*(\..+)\Z", "$1")
$ext2 = StringRight($path, StringLen($path) - StringInStr($path, ".",  0, -1) + 1)
MsgBox(0, "Test", $ext1 & @LF & $ext2)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Here 2 other ways:

$path = "c:\users\testuser\desktop\my great screensaver.scr"
$ext1 = StringRegExpReplace($path, ".*(\..+)\Z", "$1")
$ext2 = StringRight($path, StringLen($path) - StringInStr($path, ".",  0, -1) + 1)
MsgBox(0, "Test", $ext1 & @LF & $ext2)

Br,

UEZ

Your example works only if there is an extension, else it return full path ...

Not mine ! Posted Image

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

This is how I did it in the end:

#include <Crypt.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <file.au3>

Global $drive
Global $dir
Global $filename
Global $ext
Global $alg = $CALG_AES_256

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 291, 119, 192, 124)
$Label1 = GUICtrlCreateLabel("File Encryption and Decryption", 24, 16, 249, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$encrypt = GUICtrlCreateButton("Encrypt a file", 24, 56, 75, 25, BitOR($WS_GROUP, $BS_DEFPUSHBUTTON))
$decrypt = GUICtrlCreateButton("Decrypt a file", 192, 56, 75, 25, $WS_GROUP)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
$AES = GUICtrlCreateRadio("AES (Win XP and above)", 24, 38, 140, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$RC4 = GUICtrlCreateRadio("RC4", 194, 38, 128, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $encrypt
            Encryptfile()

        Case $decrypt
            Decryptfile()

        Case $RC4
            ;MsgBox(0,'','RC4')
            $alg = $CALG_RC4

        Case $AES
            ;MsgBox(0,'','AES')
            $alg = $CALG_AES_256


    EndSwitch
WEnd





Func Decryptfile()
    $openfile = FileOpenDialog('Choose file to decrypt', @MyDocumentsDir, 'All Files (*.*)', 3)
    If $openfile <> '' Then
        $pathsplit = _PathSplit($openfile, $drive, $dir, $filename, $ext)
        $password = InputBox('Password', 'Enter the password', '', '*')
        $key = _Crypt_HashData($password, $CALG_SHA1)
        $savefile = FileSaveDialog('Save as', @MyDocumentsDir, 'All Files (*' & $pathsplit[4] & ')', 16)
        $pathsplitsave = _PathSplit($savefile, $drive, $dir, $filename, $ext)
        If $pathsplitsave[4] <> $pathsplit[4] Then $savefile = $savefile & $pathsplit[4]

        If $savefile <> '' Then
            If $openfile <> $savefile Then
                _Crypt_DecryptFile($openfile, $savefile, $key, $alg)
                If @error Then MsgBox(0, 'Error', 'There was an errror.')
            Else
                MsgBox(0, 'Error', 'You cannot overwrite the original file with the new file. Save as a different name.')
            EndIf
        EndIf


    EndIf



EndFunc   ;==>Decryptfile







Func Encryptfile()
    $openfile = FileOpenDialog('Choose file to encrypt', @MyDocumentsDir, 'All Files (*.*)', 3)
    If $openfile <> '' Then
        $pathsplit = _PathSplit($openfile, $drive, $dir, $filename, $ext)
        $password = InputBox('Password', 'Enter the password', '', '*')
        $key = _Crypt_HashData($password, $CALG_SHA1)
        $savefile = FileSaveDialog('Save as', @MyDocumentsDir, 'File (*' & $pathsplit[4] & ')', 16)
        $pathsplitsave = _PathSplit($savefile, $drive, $dir, $filename, $ext)
        If $pathsplitsave[4] <> $pathsplit[4] Then $savefile = $savefile & $pathsplit[4]




        If $savefile <> '' Then
            If $openfile <> $savefile Then
                _Crypt_EncryptFile($openfile, $savefile, $key, $alg)
                If @error Then MsgBox(0, 'Error', 'There was an errror.')
            Else
                MsgBox(0, 'Error', 'You cannot overwrite the original file with the new file. Save as a different name.')
            EndIf

        EndIf

    EndIf



EndFunc   ;==>Encryptfile

My method seems to get the whole extension. My first idea would not work with .'s in a file name like "My amazing v 0.5.32 file.cmd".

Also this one will add the extension if the file save as was not manually added with the extension.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...