Jump to content

Recommended Posts

Easy to do, but it seems to me that this should be a built-in function  -- unless I'm missing something, somewhere....

Updated: UDF _PathSplit returns extension with "." prepended. Why? Extension is a string, "." just happens to be to be the separator, and it forces StringTrimLeft($extension, 1) for no good reason (IMO).

 

#include <File.au3>

Global $sDr, $sPa, $sFn, $sEx
$sFileFullPath = "d:\path\blah blah blah.foo foo.bar bar.extension"
Exit MsgBox( 0, "", _PathSplit($sFileFullPath , $sDr, $sPa, $sFn, $sEx)[4] )

Original function (less expensive??):

Func _FileGetExt($sFn)
; Parse EXTension from [d:\path\]filename.EXTension
    Local $sExt = "", $aA
    If StringInStr($sFn, ".") Then
        If Not StringInStr(FileGetAttrib($sFn), "D") Then
            $aA = StringSplit($sFn, ".")
            $sExt = $aA[$aA[0]]
        EndIf
    EndIf
    Return $sExt
EndFunc  ;==>_FileGetExt

 

Edited by CarlD
Update (thanks to TheDCoder :-))
Link to post
Share on other sites
  • Replies 450
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

This functions lets you check if a window is responding or not. Equal to checking if a Window goes white after a while using your eye #include <WinAPISys.au3> ; #FUNCTION# ==

Those who post vague questions are not only wasting their own time but also the time of those replying. So please provide the following to help those who help you. #include <Misc.au3> ; Ver

-or- just add this in your properties file assuming you have the full version: save.session.advanced=1Jos

Posted Images

  • 1 month later...

Func _ConsoleEraseLine() is for compiled CUI scripts. It allows successive one-line console outputs to be printed to a single line on the screen (by overwriting the previous line). Output lines must NOT end in @LF.

Compile the code, then command EraseLineDemo.exe.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=EraseLineDemo.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;
; CLD rev.10/31/2021

Global $vLine1 = "The quick brown fox jumped over the lazy dog."
Global $vLine2 = 9999999
Global $vLine3 = 0000001
Global $vLine4 = "0000001"
Global $vLine5 = 3.1416
Global $vLine6 = -9999999
Global $vLine7 = -0000001
Global $vLine8 = "-0000001"
Global $vLine9 = -03.1416
Global $vLine10 = "-03.1416"
Global $vLine11 = True

ConsoleWrite($vLine1)
Sleep(2000)
_ConsoleEraseLine($vLine1)
ConsoleWrite($vLine2)
Sleep(2000)
_ConsoleEraseLine($vLine2)
ConsoleWrite($vLine3)
Sleep(2000)
_ConsoleEraseLine($vLine3)
ConsoleWrite($vLine4)
Sleep(2000)
_ConsoleEraseLine($vLine4)
ConsoleWrite($vLine5)
Sleep(2000)
_ConsoleEraseLine($vLine5)
ConsoleWrite($vLine6)
Sleep(2000)
_ConsoleEraseLine($vLine6)
ConsoleWrite($vLine7)
Sleep(2000)
_ConsoleEraseLine($vLine7)
ConsoleWrite($vLine8)
Sleep(2000)
_ConsoleEraseLine($vLine8)
ConsoleWrite($vLine9)
Sleep(2000)
_ConsoleEraseLine($vLine9)
ConsoleWrite($vLine10)
Sleep(2000)
_ConsoleEraseLine($vLine10)
ConsoleWrite($vLine11)
Sleep(2000)
_ConsoleEraseLine($vLine11)

Exit ConsoleWrite("Done!" & @LF)

Func _ConsoleEraseLine($vIn)
; Erase one line (only) from console output (DOS only, not SciTE)
; $vIn = Line_to_Erase
; Does NOT work if line ends with @LF !!
    If StringRight($vIn, 1) = @LF Then Return
    Local $sOut = ""
    For $i = 1 To StringLen($vIn)
        $sOut &= Chr(8) & " " & Chr(8)
    Next
    ConsoleWrite($sOut)
EndFunc  ;==>_ConsoleEraseLine

 

 

Edited by CarlD
Update
Link to post
Share on other sites
18 hours ago, JockoDundee said:

@CarlD, yes but what happens if the string to write and erase is also a valid integer, say 999999999 or 000001 ?

@JockoDundee, Yes, I don't think it makes a difference. Once I removed the erroneous IsInt() test, if If you feed an integer like 000001 to ConsoleWrite, it evaluates it (to "1") before writing it to the screen. If you want the output to actually be "000001" it has to be quoted as a string. _ConsoleEraseLine now behaves the same way, so it will (or should) erase whatever ConsoleWrite outputs. Try the updated code in the original snippet -- as modified to respond to your comment -- and see if it works for you.

Edited by CarlD
Eating crow
Link to post
Share on other sites
28 minutes ago, CarlD said:

Try the updated code in the original snippet and see if it works for you.

I think it would work now.

However, my objection was due to the IsInt() statement in your original code:

9DF65139-771D-4486-9451-D591DCEF2D37.thumb.png.e47401366ae5a604cc293cf68adcd429.png

a line which no longer exists in the refactored code.

What was that line supposed to do?

Code hard, but don’t hard code...

Link to post
Share on other sites
4 hours ago, JockoDundee said:

I think it would work now.

However, my objection was due to the IsInt() statement in your original code:

9DF65139-771D-4486-9451-D591DCEF2D37.thumb.png.e47401366ae5a604cc293cf68adcd429.png

a line which no longer exists in the refactored code.

What was that line supposed to do?

It was supposed to allow inputting either the string itself or an integer representing the length of the string to overwrite -- but as you pointed out, what if the "string" was itself an integer? So I was wrong to say in my last message that "it doesn't make a difference". I'd conveniently forgotten that I modified my code to respond to your comment, and then pretended that there was nothing to your comment in the first place. 😏 Thank you for setting me straight. 😁

Link to post
Share on other sites
  • 5 months later...

_StringCapitalize() - version 1.0

I like _StringProper, but it has some unwanted effects, like capitalizing the letter s after an apostrophe (ex: cat's = Cat'S) or letters after other symbols. Also, _StringProper changes all letters after the first to lowercase. With this code, it only capitalizes the first letter and leaves the rest alone. If you use the exceptions option ($text4 example), it makes email addresses all lowercase and deals with names that begin with mc (like McDonald). Lastly, you have the ability to override the default symbols that the script will capitalize afterwards (space, period, comma, brace, bracket, etc.).

When you run the example, you will see the difference between _StringProper and _StringCapitalize (standard and with exceptions). It's not perfect (yet), but it's closer to what I want than _StringProper.

Usage:

_StringCapitalize ( string,  [, exceptions= 0  [, symbols="[.| |,|/(|/[|-]"  ] ] )

Parameters:

string

string passed to function

exceptions       

[optional] Default = 0
0 = do not use exceptions
1 = use exceptions

symbols [optional] define symbols for function to use

 

#include <String.au3>
#include <Array.au3>

$text1 = "mcdonald's AshtonK Ke$ha #trending @realcelebrity (etc) TEST@MSN.NET"
$text2 = _StringProper($text1)
$text3 = _StringCapitalize($text1)
$text4 = _StringCapitalize($text1, 1) ; 1 = use email and "mc" exceptions
$text5 = _StringCapitalize($text1, 1, "[.| |,|/[|-|#|@|*]") ; user defined symbols - script will capitalize next letter after these symbols.

MsgBox(0, "", "Original Text:" & @CRLF & $text1 & @CRLF & @CRLF & "_StringProper:" & @CRLF & $text2 & @CRLF & @CRLF & "_StringCapitalize:" & @CRLF & $text3 & @CRLF & @CRLF & "_StringCapitalize with exceptions:" & @CRLF & $text4 & @CRLF & @CRLF & "_StringCapitalize with exceptions and user specified symbols:" & @CRLF & $text5)


Func _StringCapitalize($__sText, $__iExceptions = 0, $__sSymbols = "[.| |,|/(|/[|-]")   ; version 1.0
    $__aText = StringSplit($__sText, "")
    For $i = 1 To $__aText[0]
        If $i = 1 OR StringRegExp($__aText[$i-1], $__sSymbols) = 1 Then
            $__aText[$i] = StringUpper($__aText[$i])
        EndIf
    Next
    $__textOut = _ArrayToString($__aText, "", 1)
    If $__iExceptions = 1 Then
        ; Lowercase all email addresses
        $__aExpression = StringRegExp($__textOut, "[\w]+@[\w]+\.+[\w]+", 3)
        For $i = 1 To UBound($__aExpression)
            $__textOut = StringReplace($__textOut, $__aExpression[$i - 1], StringLower($__aExpression[$i - 1]))
        Next
        $__aExpression = "" ; reset variable
        ; Format words beginning with mc. Ex: mcdonald = McDonald
        $__aExpression = StringRegExp($__textOut, "([Mm][Cc][A-Za-z])", 3)
        For $i = 1 To UBound($__aExpression)
            $__textOut = StringReplace($__textOut, $__aExpression[$i - 1], "Mc" & StringUpper(StringRight($__aExpression[$i - 1], 1)))
        Next
    EndIf
    Return $__textOut
EndFunc

 

Link to post
Share on other sites
  • 3 months later...

_ArraySafeDelim() - Returns a usable delimiter that is not in the array. Could probably be expanded to use Unicode but I CBA to filter symbols from letters for the entire unicode range. You could technically use letters as a delim but eh, maybe as a flag.
 

Func _ArraySafeDelim($aArray)
    
    $sChars = _ArrayToString($aArray)
    
    Select
        
        Case Not StringInStr($sChars, "|")
            Return "|"
        Case Else
            For $iLoop = 33 To 191 Step 1
                Switch $iLoop
                    Case 48 to 57 ; Skip Numbers
                        ContinueLoop
                    Case 65 to 90 ; Skip Upper Case
                        ContinueLoop
                    Case 97 to 122 ; Skip Lower Case
                        ContinueLoop
                    Case Else
                        If Not StringInStr($sChars, Chr($iLoop)) Then Return Chr($iLoop)
                EndSwitch
            Next
            
    EndSelect

    ; If No Char Found, Error
    Return SetError(1, 0, False)

EndFunc
Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...