Jump to content

_FileGet_RealName ($File)


z0mgItsJohn
 Share

Recommended Posts

This Function Allows You To Get The "Real" Name Of The File Without The Extension For Example You Have "Test.Au3" And You Want To Know What The File Is Called Without The ".Au3" You Can Use This Function To Get That.. Enjoy! Also.. If You Have A File For Say "This..Is..A..Test.Au3" It Will Return "This..Is..A.Test".. :lmao:

; Source : _FileGet_RealName.Au3

#Include <String.Au3>

Func _FileGet_RealName ($File)
Local $String, $Return
If StringInStr ($File, '.') = '0' Then 
Return $File
ElseIf $File = '' Then 
Return $File
EndIf 
$String = StringSplit ($File, '')
For $Array = $String['0'] To '1' Step '-1'
If $String[$Array] <> '.' Then 
$Return = $Return & $String[$Array]
Else 
$Return = _StringReverse ($Return)
$Return = '.' & $Return
$Return = StringReplace ($File, $Return, '')
ExitLoop 
EndIf 
Next
Return $Return
EndFuncoÝ÷ Ù*.­ÇÅ©©à.Ý«­¢+Ø%¹±Õ±Ðí}¥±Ñ}9µ¹ÔÌÐì((ÀÌØí¥±|ÄôÌäíQ¡¥Ì¸¹%̸¹¸¹QÍйÔÌÌäì(ÀÌØíI±}9µ|Äô}¥±Ñ}9µ ÀÌØí¥±|Ĥ((ÀÌØí¥±|ÈôÌäíÕѽ%и¹%̸¹Q¹ÔÌÌäì(ÀÌØíI±}9µ|Èô}¥±Ñ}9µ ÀÌØí¥±|Ȥ()5Í ½à ÌäìÀÌäì°ÌäíIÍÕ±ÑÌÌäì°Ìäí¥±ÄèÌäìµÀìÀÌØí¥±|ĵÀì
I1µÀìÌäíI°9µèÌäìµÀìÀÌØíI±}9µ|ĵÀì
I1µÀìÌäí¥±ÈèÌäìµÀìÀÌØí¥±|ȵÀì
I1µÀìÌäíI°9µèÌäìµÀìÀÌØíI±}9µ|È°ÌäìÀÌäì¤

~ Enjoy ~

Please Leave Comments Etc.. Etc.. :)

- John

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

MsgBox(0, "Resuts", _FileGet_RealName("AutoIt..Is..FTW.Au3"))
MsgBox(0, "Resuts", _FileGet_RealName(@DesktopDir & "\Test.au3"))

Func _FileGet_RealName($sFile)
   $sFile = StringRegExpReplace($sFile, "(?i).*[[:punct:]](\w*?)\..*", "$1")
   If @Extended >= 1 Then Return $sFile
   Return ""
EndFunc

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

MsgBox(0, "Resuts", _FileGet_RealName("AutoIt..Is..FTW.Au3"))
MsgBox(0, "Resuts", _FileGet_RealName(@DesktopDir & "\Test.au3"))

Func _FileGet_RealName($sFile)
   $sFile = StringRegExpReplace($sFile, "(?i).*[[:punct:]](\w*?)\..*", "$1")
   Return $sFile
EndFunc
Umm.. That Dose Not Support... Stuff Like "AutoIt..Is..FTW.Au3"

Example..

#Region Yours...
$Var1 = _FileGet_RealName2 ('This..Is..A..Test.Au3')
; $Var = Test  
; Not "This..Is..A..Test"
ConsoleWrite ('$Var1 : ' & $Var1 & @CRLF)
$Var2 = _FileGet_RealName2 ('Test..Au3')
; Var2 Dose Not Show Anything..
ConsoleWrite ('$Var2 : ' & $Var2 & @CRLF)

Func _FileGet_RealName2($sFile)
   $sFile = StringRegExpReplace($sFile, "(?i).*[[:punct:]](\w*?)\..*", "$1")
   Return $sFile
EndFunc
#EndRegion

#Region Mine..

#Include <String.Au3>

$Var1 = _FileGet_RealName1 ('This..Is..A..Test.Au3')
; $Var = This..Is..A..Test
ConsoleWrite ('$Var1 : ' & $Var1 & @CRLF)
$Var2 = _FileGet_RealName1 ('Test..Au3')
; Var2 = Test.
ConsoleWrite ('$Var2 : ' & $Var2 & @CRLF)

Func _FileGet_RealName1 ($File)
Local $String, $Return
If StringInStr ($File, '.') = '0' Then
Return $File
ElseIf $File = '' Then
Return $File
EndIf
$String = StringSplit ($File, '')
For $Array = $String['0'] To '1' Step '-1'
If $String[$Array] <> '.' Then
$Return = $Return & $String[$Array]
Else
$Return = _StringReverse ($Return)
$Return = '.' & $Return
$Return = StringReplace ($File, $Return, '')
ExitLoop
EndIf
Next
Return $Return
EndFunc
#Region

Latest Projects :- New & Improved TCP Chat

Link to comment
Share on other sites

Well since I won't use console write I tested it in MBs and it returned properly using

"AutoIt..Is..FTW.Au3" But it did fail on "test..au3"

I'll look at that later. I might have to test twice for the regexp.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

why not just

Func _FileGetRealName($File)
    Return StringLeft($File, StringInStr($File, ".", True, -1) - 1)
EndFunc
Close Alek, but if the function is passed a file name without an extension, it wil return nothing.

I use one like this:

Func _FileGet_RealName($File)
    If StringInStr($File,".") then $File = StringLeft($File, StringInStr($File, ".", True, -1) - 1)
        Return $File
EndFunc
Link to comment
Share on other sites

Might be more efficient to confine it to a single StringInStr statement...

Func _FileGet_RealName($File)
    Local $x 
    $x = StringInStr($File, ".", 2, -1)
    If $x Then $File = StringLeft($File, $x - 1)
    Return $File
EndFunc
Edited by Spiff59
Link to comment
Share on other sites

Might be more efficient to confine it to a single StringInStr statement...

Func _FileGet_RealName($File)
    Local $x 
    $x = StringInStr($File, ".", 2, -1)
    If $x Then $File = StringLeft($File, $x - 1)
    Return $File
EndFunc
True.

While were at it:

Func _FileGet_RealName($File)
    Local $x = StringInStr($File, ".", 2, -1)
    If $x Then $File = StringLeft($File, $x - 1)
    Return $File
EndFunc
:)
Link to comment
Share on other sites

Link to comment
Share on other sites

@all

Why not ?! :)

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

Regards

ptrex

This will work but it's very slow!
Link to comment
Share on other sites

  • Moderators

I'm so confused on the significance of this or what it's really trying to attempt other than removing the extension.

StringRegExpReplace($s_file, "(.+?)(\.+(?:\w+\z|\z))", "$1")

Works just fine for that.

Edit:

BTW, if you run this as the file name

"This..Is..A..Test"
Then you get a decimal as the last character with everyone elses example, I don't "think" that's the desired output is it?

Having said that, this may be the desired output

StringRegExpReplace($s_file, "(.+?[^\.])(\.\w+\z)", "$1")
Edited by SmOke_N

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

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...