Jump to content

Return File Extension


RazerM
 Share

Recommended Posts

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.

A somewhat delayed answer to this as the post about _PathSplit answers the question. However, for my own benefit I followed through my suggestion and have used your pathname as an example. As I said earlier I am essentially a beginner when it comes to AutoIt so I'm sure this can be improved.

$filepath="\\server\sharename\path1\path2.ext\yyyymmdd.hhmm.log"

; BACKWARD OR FORWARD SLASHES?
If StringInStr($filepath,"\") > 0 then
    $d = "\"
elseif StringInStr($filepath,"/") > 0 then
    $d = "/"
else 
    $d=""
endif

;FIRST SPLIT THE FILE PATH INTO FOLDERS AND THE FULL FILENAME

$tree = Stringsplit($filepath,"/\",0)
$fullfilename = $tree[$tree[0]]
$namesplit = StringSplit($fullfilename,".",0)
$ext = $namesplit[$namesplit[0]]
$filename = StringLeft($fullfilename,StringLen($fullfilename) - StringLen($ext) - 1)

;NOW RECONSTRUCT $FILEPATH FROM THE $TREE ARRAY $FILENAME AND $EXT

$makefilepath = ""
For $i = 1 to $tree[0]-1
    If $tree[$i] = "" then 
        $makefilepath = $makefilepath & $d  
    elseif $tree[$i] <> "" then
        $makefilepath = $makefilepath & $tree[$i] & $d
    endif
next

MsgBox(1,"Filepath",$makefilepath &  $filename & "." & $ext)
Link to comment
Share on other sites

A somewhat delayed answer to this as the post about _PathSplit answers the question. However, for my own benefit I followed through my suggestion and have used your pathname as an example. As I said earlier I am essentially a beginner when it comes to AutoIt so I'm sure this can be improved.

$filepath="\\server\sharename\path1\path2.ext\yyyymmdd.hhmm.log"

; BACKWARD OR FORWARD SLASHES?
If StringInStr($filepath,"\") > 0 then
    $d = "\"
elseif StringInStr($filepath,"/") > 0 then
    $d = "/"
else 
    $d=""
endif

;FIRST SPLIT THE FILE PATH INTO FOLDERS AND THE FULL FILENAME

$tree = Stringsplit($filepath,"/\",0)
$fullfilename = $tree[$tree[0]]
$namesplit = StringSplit($fullfilename,".",0)
$ext = $namesplit[$namesplit[0]]
$filename = StringLeft($fullfilename,StringLen($fullfilename) - StringLen($ext) - 1)

;NOW RECONSTRUCT $FILEPATH FROM THE $TREE ARRAY $FILENAME AND $EXT

$makefilepath = ""
For $i = 1 to $tree[0]-1
    If $tree[$i] = "" then 
        $makefilepath = $makefilepath & $d  
    elseif $tree[$i] <> "" then
        $makefilepath = $makefilepath & $tree[$i] & $d
    endif
next

MsgBox(1,"Filepath",$makefilepath &  $filename & "." & $ext)
And what happens if there isn't a file extension in the path?
Link to comment
Share on other sites

Thanks alot guys i finally got my script working

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.
Link to comment
Share on other sites

  • 8 years later...

You do realize you replied to an year old topic right?

BTW, your snippet does zero error checking and will cause an error if there is no extension.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

You do realize you replied to an year old topic right?

BTW, your snippet does zero error checking and will cause an error if there is no extension.

The StringSplit function always returns an array with default settings.  So no error would exist, the array would be the entire string.

Global $ga_Split = StringSplit("noDots", ".")
Global $gsz_Data = $ga_Split[$ga_Split[0]]
ConsoleWrite($gsz_Data & @CRLF) 

 

 

Simplest way to find the true extention is:

$path = "C:\folder 1\folder2\autoit.0.0.1.exe"

$findext = StringSplit($path, ".")
$extention = $findext[$findext[0]]

MsgBox(0, "Results", "The extention is " & $extention)

Please be more mindful of the age of threads you respond to in the future.

In addition... Simplicity is really in the eye of the beholder I would think.  Your example is not the "simplest" IMHO.

Global $gsz_File = "C:\SomeFolder\SomeOtherFolder\Some.OtherFolder\SomeFile.SomeOddThing.IamTheExtension"
Global $gsz_DataAfterLastDecimalNotNecessarilyAnExtensionIfTheFileIsNotAFile = _
    StringRegExpReplace($gsz_File, "^(.+?)(\w+)\z", "$2")
ConsoleWrite($gsz_DataAfterLastDecimalNotNecessarilyAnExtensionIfTheFileIsNotAFile & @CRLF)

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.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

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