Jump to content

Custom String, Map, File, and Array functions


Recommended Posts

Hi All,

I intend on keeping custom functions/UDFs (works in progress) here; if anyone wants to use any code, feel free.

String functions:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include-once

; #FUNCTION# ====================================================================================================================
; Name ..........:  _DateTimeGet
; Description ...:  Returns the date and time formatted for use in sortable filenames, logs, listviews, etc.
; Syntax ........:  _DateTimeGet(iType = 1[, $bHumanFormat = False])
; Parameters ....:  $iType              - [optional] an integer value. Default is 1.
;                                       1 - Date and time in file-friendly format; 20190115_113756
;                                       2 - Date in file-friendly format; 20190115
;                                       3 - Time in file friendly format; 113756
;                   $bHumanFormat       - [optional] a boolean value. Default is False.
;                                       True - Includes slashes in the date and colons in the time with a space inbetween
;                                       False - No slashes or colons included with an underscore inbetween
; Return values .:  Success             - String
;                   Failure             - Sets @error to non-zero and returns an empty string
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _DateTimeGet($iType = 1, $bHumanFormat = False)

    If $iType < 1 Or $iType > 3 Then Return (SetError(-1, 0, ""))

    ;; Param1:
    ;; 1 = Date and time in file friendly format:   20190115_113756
    ;; 2 = Date in file friendly format:            20190115
    ;; 3 = Time in file friendly format:            113756

    ;; Param2:
    ;; True = Use human-readable format:            15/01/2019 11:37:56

    Local $sTime = @HOUR & ":" & @MIN & ":" & @SEC
    Local $sDate = @MDAY & "/" & @MON & "/" & @YEAR

    If $iType = 1 Then
        If $bHumanFormat = False Then
            $sTime = StringReplace($sTime, ":", "")
            $sDate = StringReplace($sDate, "/", "")
            $sDate = StringTrimLeft($sDate, 4) & StringMid($sDate, 3, 2) & StringLeft($sDate, 2)
            Return ($sDate & "_" & $sTime)
        Else
            Return ($sDate & " " & $sTime)
        EndIf
    ElseIf $iType = 2 Then
        If $bHumanFormat = False Then
            $sDate = StringReplace($sDate, "/", "")
            $sDate = StringTrimLeft($sDate, 4) & StringMid($sDate, 3, 2) & StringLeft($sDate, 2)
        EndIf
        Return ($sDate)
    ElseIf $iType = 3 Then
        If $bHumanFormat = False Then
            $sTime = StringReplace($sTime, "/", "")
        EndIf
        Return ($sTime)
    EndIf

EndFunc   ;==>_DateTimeGet

; #FUNCTION# ====================================================================================================================
; Name ..........:  _FileToFileExtension
; Description ...:  Returns a file extension from a filename/FQPN (Fully Qualified Path Name)
; Syntax ........:  _FileToFileExtension($sPath)
; Parameters ....:  $sPath              - a string value.
; Return values .:  Success             - String
;                   Failure             - Empty string as returned from StringTrimLeft()
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _FileToFileExtension($sPath)

    Return (StringTrimLeft($sPath, StringInStr($sPath, ".", 0, -1)))

EndFunc   ;==>_FileToFileExtension

; #FUNCTION# ====================================================================================================================
; Name ..........:  _FileToFileName
; Description ...:  Returns a filename from a FQPN (Fully Qualified Path Name)
; Syntax ........:  _FileToFileName($sPath[, $bIncludeExtension = True])
; Parameters ....:  $sPath              - a string value.
;                   $bIncludeExtension  - [optional] a boolean value. Default is True.
; Return values .:  Success             - String
;                   Failure             - Empty string as returned from StringLeft()
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _FileToFileName($sPath, $bIncludeExtension = True)

    Local $sReturn = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1))
    If $bIncludeExtension = False Then $sReturn = StringLeft($sReturn, StringInStr($sReturn, ".", 0, -1) - 1)
    Return ($sReturn)

EndFunc   ;==>_FileToFileName

; #FUNCTION# ====================================================================================================================
; Name ..........:  _FileToFilePath
; Description ...:  Returns a folder path from a FQPN (Fully Qualified Path Name)
; Syntax ........:  _FileToFilePath($sPath)
; Parameters ....:  $sPath              - a string value.
; Return values .:  Success             - String
;                   Failure             - Empty string as returned from StringLeft()
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _FileToFilePath($sPath)

    Return (StringLeft($sPath, StringInStr($sPath, "\", 0, -1) - 1))

EndFunc   ;==>_FileToFilePath

; #FUNCTION# ====================================================================================================================
; Name ..........:  _StringLeft
; Description ...:  Searches for a string inside a string, then removes everything on the right of that string
; Syntax ........:  _StringLeft($sString, $sRemove[, $iCaseSense = 0, $iOccurrence = 1])
; Parameters ....:  $sString            - a string value. The string to search inside.
;                   $sRemove            - a string value. The string to search for.
;                   $iCaseSense         - an integer value. Flag to indicate if the operations should be case sensitive.
;                   $iOccurrence        - an integer value. Which occurrence of the substring to find in the string. Use a
;                                         negative occurrence to search from the right side.
; Return values .:  Success             - String
;                   Failure             - Empty string as returned from StringLeft()
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _StringLeft($sString, $sRemove, $iCaseSense = 0, $iOccurrence = 1)

    Return (StringLeft($sString, StringInStr($sString, $sRemove, $iCaseSense, $iOccurrence) - 1))

EndFunc   ;==>_StringLeft

; #FUNCTION# ====================================================================================================================
; Name ..........:  _StringRandom
; Description ...:  Returns a string of random characters
; Syntax ........:  _StringRandom($iAmount[, $iType = 1])
; Parameters ....:  $iAmount            - an integer value. Length of returned string
;                   $iType              - [optional] an integer value. Default is 1.
;                                       1 - Return digits (0-9)
;                                       2 - Return hexadecimal (0-9, A - F)
;                                       3 - Return Alphanumeric upper (0-9, A - Z)
;                                       4 - Return Alphanumeric (0-9, A - Z, a - z)
;                                       5 - Return Alpha upper (A - Z)
;                                       6 - Return Alpha (A - Z, a - z)
; Return values .:  Success             - String
;                   Failure             - Empty string and @error flag as follows:
;                   @error :            1 - $iAmount is not a positive integer
;                                       2 - $iType is out of bounds
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _StringRandom($iAmount, $iType = 1)

    If $iAmount < 1 Or IsInt($iAmount) = 0 Then Return (SetError(-1, 0, ""))

    Local $sString = ""
    Local $iRandomLow = 1, $iRandomHigh = 62

    #Tidy_Off
    Local Static $aCharId[63] = [0, Chr(48), Chr(49), Chr(50), Chr(51), Chr(52), Chr(53), Chr(54), Chr(55), Chr(56), Chr(57), Chr(65), Chr(66), Chr(67), _
                                    Chr(68), Chr(69), Chr(70), Chr(71), Chr(72), Chr(73), Chr(74), Chr(75), Chr(76), Chr(77), Chr(78), Chr(79), Chr(80), _
                                    Chr(81), Chr(82), Chr(83), Chr(84), Chr(85), Chr(86), Chr(87), Chr(88), Chr(89), Chr(90), Chr(97), Chr(98), Chr(99), _
                                    Chr(100), Chr(101), Chr(102), Chr(103), Chr(104), Chr(105), Chr(106), Chr(107), Chr(108), Chr(109), Chr(110), Chr(111), _
                                    Chr(112), Chr(113), Chr(114), Chr(115), Chr(116), Chr(117), Chr(118), Chr(119), Chr(120), Chr(121), Chr(122)]
     #Tidy_On

    If $iType = 1 Then ;; digits: 1 - 10
        $iRandomHigh = 10
    ElseIf $iType = 2 Then ;; hexadecimal: 1 - 16
        $iRandomHigh = 16
    ElseIf $iType = 3 Then ;; alnumupper: 1 - 36
        $iRandomHigh = 36
    ElseIf $iType = 4 Then ;; alnum: 1 - 62
        $iRandomHigh = 62
    ElseIf $iType = 5 Then ;; alphaupper: 11 - 36
        $iRandomLow = 11
        $iRandomHigh = 36
    ElseIf $iType = 6 Then ;; alpha: 11 = 62
        $iRandomLow = 11
        $iRandomHigh = 62
    Else
        Return (SetError(-2, 0, ""))
    EndIf

    For $i = 1 To $iAmount
        $sString &= $aCharId[Random($iRandomLow, $iRandomHigh, 1)] ;; append string with corresponding random character from ascii array
    Next

    Return ($sString)

EndFunc   ;==>_StringRandom

; #FUNCTION# ====================================================================================================================
; Name ..........:  _StringTrimLeft
; Description ...:  Searches for a string inside a string, then removes everything on the left of that string
; Syntax ........:  _StringTrimLeft($sString, $sRemove[, $iCaseSense = 0, $iOccurrence = 1])
; Parameters ....:  $sString            - a string value. The string to search inside.
;                   $sRemove            - a string value. The string to search for.
;                   $iCaseSense         - an integer value. Flag to indicate if the operations should be case sensitive.
;                   $iOccurrence        - an integer value. Which occurrence of the substring to find in the string. Use a
;                                         negative occurrence to search from the right side.
; Return values .:  Success             - String
;                   Failure             - Empty string as returned from StringTrimLeft()
; Author ........:  Sam Coates
; ===============================================================================================================================
Func _StringTrimLeft($sString, $sRemove, $iCaseSense = 0, $iOccurrence = 1)

    Return (StringTrimLeft($sString, StringInStr($sString, $sRemove, $iCaseSense, $iOccurrence) + StringLen($sRemove) - 1))

EndFunc   ;==>_StringTrimLeft

Examples:

ConsoleWrite(_StringRandom(100, 6) & @CRLF)

ConsoleWrite(_StringTrimLeft("C:\Windows\System32\cmd.exe", "C:\Windows\System32\") & @CRLF)

ConsoleWrite(_StringLeft("C:\Windows\System32\cmd.exe", "cmd.exe") & @CRLF)

ConsoleWrite(_FileToFileName("C:\Windows\System32\cmd.exe") & @CRLF)

ConsoleWrite(_FileToFilePath("C:\Windows\System32\cmd.exe") & @CRLF)

ConsoleWrite(_FileToFileExtension("C:\Windows\System32\cmd.exe") & @CRLF)

ConsoleWrite(_StringRandom(6, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(6, 4)&  @CRLF)

 

Edited by Inpho
Link to post
Share on other sites

Scripting Dictionary (Map) functions (_MapOfMaps* stuff for reading all ini sections into a map of maps):

#AutoIt3Wrapper_AU3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include-once
#include <Array.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapAddKeyValuePair
; Description ...:
; Syntax ........: _MapAddKeyValuePair($mMap, $sKey, $sValue)
; Parameters ....: $mMap                - a map. The map you want to add the key/value pair to.
;                  $sKey                - a string value.
;                  $sValue              - a string value.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapAddKeyValuePair($mMap, $sKey, $sValue)

    If $mMap.Exists($sKey) Then
        Return (SetError(-1, 0, '"' & $sKey & '"' & ' already exists with a value of ' & '"' & $mMap.Item($sKey) & '"'))
    Else
        $mMap.Add($sKey, $sValue)
    EndIf

EndFunc   ;==>_MapAddKeyValuePair

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapAppendToKey
; Description ...:
; Syntax ........: _MapAppendToKey($mMap, $sKey, $sValue)
; Parameters ....: $mMap                - a map. The map you want to modify the key/value pair in.
;                  $sKey                - a string value. The key you want to append the value to.
;                  $sValue              - a string value. The value you want to append to the key.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapAppendToKey($mMap, $sKey, $sValue)

    Local $sCurrent

    If $mMap.Exists($sKey) Then
        $sCurrent = $mMap.Item($sKey)
        $mMap.Remove($sKey)
        $mMap.Add($sKey, $sCurrent & ";" & $sValue)
    Else
        $mMap.Add($sKey, $sValue)
    EndIf

EndFunc   ;==>_MapAppendToKey

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapFrom2DArray
; Description ...:
; Syntax ........: _MapFrom2DArray($aArray)
; Parameters ....: $aArray              - an array of unknowns.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapFrom2DArray($aArray)

    Local $mMap = ObjCreate("Scripting.Dictionary")

    If Not IsObj($mMap) Then
        Return (SetError(-1, 0, "Object not created"))
    EndIf

    For $i = 0 To UBound($aArray) - 1
        _MapAddKeyValuePair($mMap, $aArray[$i][0], $aArray[$i][1])
    Next

    Return ($mMap)

EndFunc   ;==>_MapFrom2DArray

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapFromIniSection
; Description ...:
; Syntax ........: _MapFromIniSection($sFile, $sSection)
; Parameters ....: $sFile               - a string value.
;                  $sSection            - a string value.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapFromIniSection($sFile, $sSection)

    Local $mMap = _MapInit()

    Local $aArray = IniReadSection($sFile, $sSection)

    For $i = 1 To UBound($aArray) - 1
        $mMap.Add($aArray[$i][0], $aArray[$i][1])
    Next

    Return ($mMap)

EndFunc   ;==>_MapFromIniSection

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapGetValue
; Description ...:
; Syntax ........: _MapGetValue($mMap, $sKey)
; Parameters ....: $mMap                - a map. The map you want to get the value from.
;                  $sKey                - a string value.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapGetValue($mMap, $sKey)

    Return ($mMap.Item($sKey))

EndFunc   ;==>_MapGetValue

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapInit
; Description ...:
; Syntax ........: _MapInit()
; Parameters ....:
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapInit()

    Local $mMap = ObjCreate("Scripting.Dictionary")

    If Not IsObj($mMap) Then
        Return (SetError(-1, 0, "Object not created"))
    EndIf

    Return ($mMap)

EndFunc   ;==>_MapInit

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapOfMapsFromIni
; Description ...:
; Syntax ........: _MapOfMapsFromIni($sFile)
; Parameters ....: $sFile               - a string value.
; Return values .: None
; Author ........: Sam Coates
; ===============================================================================================================================
Func _MapOfMapsFromIni($sFile)

    Local $mMap = _MapInit()
    Local $mMapScratch
    Local $sError

    Local $aSections = IniReadSectionNames($sFile)

    For $i = 1 To UBound($aSections) - 1
        $mMapScratch = _MapFromIniSection($sFile, $aSections[$i])
        $sError = _MapAddKeyValuePair($mMap, $aSections[$i], $mMapScratch)
        If @error Then
            ConsoleWrite($sError & @CRLF)
            ContinueLoop
        EndIf
    Next

    Return ($mMap)

EndFunc   ;==>_MapOfMapsFromIni

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapOfMapsGetMap
; Description ...:
; Syntax ........: _MapOfMapsGetMap($mMap, $sMapChildName)
; Parameters ....: $mMap                - a map.  The map you want to extract the map from.
;                  $sMapChildName       - a string value. The name of the map you want to extract.
; Return values .: None
; Author ........: Sam Coates
; ===============================================================================================================================
Func _MapOfMapsGetMap($mMap, $sMapChildName)

    Local $mReturn = _MapGetValue($mMap, $sMapChildName)

    Return ($mReturn)

EndFunc   ;==>_MapOfMapsGetMap

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapOfMapsGetValue
; Description ...:
; Syntax ........: _MapOfMapsGetValue($mMap, $sMapChildName, $sKey)
; Parameters ....: $mMap                - a map. The map which holds the maps.
;                  $sMapChildName       - a string value. The name of the map which holds the key/value pairs.
;                  $sKey                - a string value. The name of the key which you want to retrieve the value from.
; Return values .: None
; Author ........: Sam Coates
; ===============================================================================================================================
Func _MapOfMapsGetValue($mMap, $sMapChildName, $sKey)

    Local $sReturn = _MapGetValue(_MapGetValue($mMap, $sMapChildName), $sKey)

    Return ($sReturn)

EndFunc   ;==>_MapOfMapsGetValue

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapOfMapsToString
; Description ...:
; Syntax ........: _MapOfMapsToString($mMap)
; Parameters ....: $mMap                - a map. The map which holds the maps.
; Return values .: None
; Author ........: Sam Coates
; ===============================================================================================================================
Func _MapOfMapsToString($mMap)

    Local $sReturn = ""

    For $sKey In $mMap.Keys
        $sReturn &= "[" & $sKey & "]" & @CRLF & _MapToString(_MapGetValue($mMap, $sKey), True) & @CRLF & @CRLF
    Next

    Return ($sReturn)

EndFunc   ;==>_MapOfMapsToString

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapReassignKey
; Description ...:
; Syntax ........: _MapReassignKey($mMap, $sKey, $sValue)
; Parameters ....: $mMap                - a map. The map which you want to modify the key/value pair in.
;                  $sKey                - a string value.
;                  $sValue              - a string value.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapReassignKey($mMap, $sKey, $sValue)

    If $mMap.Exists($sKey) Then
        $mMap.Remove($sKey)
        $mMap.Add($sKey, $sValue)
    Else
        $mMap.Add($sKey, $sValue)
    EndIf

EndFunc   ;==>_MapReassignKey

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapRemoveKey
; Description ...:
; Syntax ........: _MapRemoveKey($mMap, $sKey)
; Parameters ....: $mMap                - a map. The map which you want to remove the key/value pair from.
;                  $sKey                - a string value.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapRemoveKey($mMap, $sKey)

    $mMap.Remove($sKey)

EndFunc   ;==>_MapRemoveKey

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapTo2dArray
; Description ...:
; Syntax ........: _MapTo2dArray($mMap)
; Parameters ....: $mMap                - a map. The map you want to export to a 2D array.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapTo2dArray($mMap, $iStartIndex = 1)

    If $iStartIndex < 0 Or $iStartIndex > 1 Then Return(SetError(-1, 0, -1))
    Local $aArray[$mMap.Count + $iStartIndex][2]

    If $iStartIndex = 1 Then $aArray[0][0] = $mMap.Count

    For $sKey In $mMap.Keys
        $aArray[$iStartIndex][0] = $sKey
        $aArray[$iStartIndex][1] = $mMap.Item($sKey)
        $iStartIndex += 1
    Next

    Return ($aArray)

EndFunc   ;==>_MapTo2dArray

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapToArray
; Description ...:
; Syntax ........: _MapToArray($mMap)
; Parameters ....: $mMap                - a map. The map you want to export to an array.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapToArray($mMap)

    Local $aArray[$mMap.Count]

    Local $i = 0
    For $sKey In $mMap.Keys
        $aArray[$i] = $sKey & " = " & $mMap.Item($sKey)
        $i += 1
    Next

    Return ($aArray)

EndFunc   ;==>_MapToArray

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapToIniSection
; Description ...:
; Syntax ........: _MapToIniSection($mMap, $sSection, $sFile[, $fSort = 0])
; Parameters ....: $mMap                - a map. The map you want to export to an ini section.
;                  $sSection            - a string value.
;                  $sFile               - a string value.
;                  $fSort               - [optional] a floating point value. Default is 0.
; Return values .: None
; Author ........: iamtheky
; ===============================================================================================================================
Func _MapToIniSection($mMap, $sSection, $sFile, $fSort = 0)

    Local $aMap = _MapTo2dArray($mMap)

    _ArrayInsert($aMap, 0, UBound($aMap) - 1)
    If $fSort = 1 Then _ArraySort($aMap)

    IniWriteSection($sFile, $sSection, $aMap)
    If @error Then Return (SetError(-1, 0, ""))

    Return (1)

EndFunc   ;==>_MapToIniSection

; #FUNCTION# ====================================================================================================================
; Name ..........: _MapToString
; Description ...:
; Syntax ........: _MapToString($mMap[, $bPadding = False])
; Parameters ....: $mMap                - a map. The map you want to export to a string.
;                  $bPadding            - [optional] a boolean value. Whether to pad the values with a tab. Default is False.
; Return values .: None
; Author ........: iamtheky
; Modified ......: Sam Coates
; ===============================================================================================================================
Func _MapToString($mMap, $bPadding = False)

    Local $sPadding
    If $bPadding = True Then $sPadding = @TAB
    Local $sReturn = ""

    For $sKey In $mMap.Keys
        $sReturn &= $sPadding & $sKey & " = " & $mMap.Item($sKey) & @LF
    Next

    Return (StringReplace($sReturn, @LF, "", -1))

EndFunc   ;==>_MapToString

Examples:

$sFile = "" ;; .ini

$mConfig = _MapOfMapsFromIni($sFile)

$sValue = _MapOfMapsGetValue($mConfig, "Last", "1")
ConsoleWrite($sValue & @CRLF)
$sValue = _MapOfMapsGetValue($mConfig, "Last", "2")
ConsoleWrite($sValue & @CRLF)
$sValue = _MapOfMapsGetValue($mConfig, "Last", "3")
ConsoleWrite($sValue & @CRLF)

msgbox(0, 'Map String - No Sort' , _MapToString($mConfig))
msgbox(0, 'Map String - No Sort' , _MapToString(_MapOfMapsGetMap($mConfig, "Last")))

_ArrayDisplay(_MapTo2dArray($mConfig, 0))
_ArrayDisplay(_MapTo2dArray(_MapOfMapsGetMap($mConfig, "Last")))


$sMaps = _MapOfMapsToString($mConfig)
ConsoleWrite($sMaps & @CRLF)

Sample .ini:

#region Last - Last 10 models updated using Muninn
[Last]
1=SM-N976B|EVR|3.13
2=SM-T585|BTU|3.13
3=SM-T295|BTU|3.13
4=SM-G973F|EVR|3.13
5=SM-A600FN|EVR|3.13
6=SM-A202F|EVR|3.13
7=SM-J415FN|BTU|3.13
8=SM-G975F|EVR|3.13
9=SM-G970F|EVR|3.13
10=SM-A510F|EVR|3.13
#endregion

#region FlashCountTargets - flash counts that will trigger an email
[FlashCountTargets]
Amounts=140000|150000|175000|200000|250000|300000|350000|400000|450000|500000|600000|700000|800000|900000|1000000
#endregion

#region FlashCountEmails - email addresses used when triggered by FlashCountTargets
[FlashCountEmails]
Users=melba23|junkew|jchd|water|iamtheky
#endregion

#region ListType - Top10 for the top 10 used models by hits. Last10 for the Last 10 models assigned by .init
[ListType]
ListType=Top10
#endregion

#region Variants - Key - RadioButton text ;; Value - 2-letter code from .init. Add |Locked to disable the radio from Flash Gui
[Variants]
Deep Black = DB|Locked
La Fleur =LF|Locked
Hugo Boss =HB|Locked
#endregion

 

Edited by Inpho
Link to post
Share on other sites

Array stuff:

; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayFindEmptyRows
; Description ...: Returns an array of indexes (similar to _ArrayFindAll) ready to feed straight into _ArrayDelete.
; Syntax ........: _ArrayFindEmptyRows(Const Byref $aArray[, $iStartRow = 1])
; Parameters ....: $aArray              - [in/out and const] The array to search for empty rows
;                  $iStartRow           - [optional] an integer value. The index to start searching from
; Return values .: Success:             - An array of indexes
;                  Failure              - Empty string and @error flag as follows:
;                                       1 - $aArray is not an array
;                                       2 - $aArray contains one row
;                                       3 - $iStartRow is out of bounds
;                                       4 - No results (unable to find any blank rows)
; Author ........: Sam Coates
; ===============================================================================================================================
Func _ArrayFindEmptyRows(ByRef Const $aArray, $iStartRow = 1)

    If Not IsArray($aArray) Then Return(SetError(-1, 0, "")) ;; Array isn't an array
    If UBound($aArray) < 2 Then Return(SetError(-2, 0, "")) ;; Array only contains one row

    Local $i, $ii
    Local $sResults = ""
    Local $aReturn
    Local $iArrayRows = UBound($aArray) - 1 ;; hold the number of rows
    If $iStartRow > $iArrayRows Then Return(SetError(-3, 0, "")) ;; Check if StartRow isn't out of bounds

    Local $iArrayColumns = UBound($aArray, 2) ;; hold the number of columns
    If @error = 2 Then $iArrayColumns = 1 ;; if error, then 1d array

    If $iArrayColumns = 1 Then ;; if 1d array

        For $i = $iStartRow To $iArrayRows ;; loop through rows
            If $aArray[$i] = "" Then $sResults &= $i & ";" ;; if its blank, save the index
        Next

    ElseIf $iArrayColumns > 1 Then ;; if 2d array

        For $i = $iStartRow To $iArrayRows ;; loop through rows
            For $ii = 0 To $iArrayColumns - 1 ;; loop through columns
                If $aArray[$i][$ii] <> "" Then ExitLoop ;; if a non-blank is found in any cell on a row, skip to next row
                If $ii = $iArrayColumns - 1 Then $sResults &= $i & ";" ;; if we reach the end of the columns and still havent found a non-blank, save the index
            Next
        Next

    EndIf

    If $sResults <> "" Then ;; if we made changes
        $sResults = StringTrimRight($sResults, 1) ;; strip the final semi-colon
    Else ;; if we made no changes
        Return(SetError(-4, 0, "")) ;; No results
    EndIf

    $aReturn = StringSplit($sResults, ";") ;; split the final string

    Return($aReturn) ;; return it

EndFunc

Example:

$aArray = _GenRandomArray()
$aArrayRemoveBlanks = _ArrayFindEmptyRows($aArray)
_ArrayDelete($aArray, $aArrayRemoveBlanks)

Func _GenRandomArray()
    Local $sFile = @TempDir & "\rnd.txt"
    Local $aArrayReturn[100000];[10]
    If FileExists($sFile) = 0 Then
        For $i = 1 To UBound($aArrayReturn) - 1
            ;(Mod($i, 10) = 0) ? ($aArray[$i][0] = (_StringRandom(20, 4)) : ($aArray[$i][0] = "") ;; why won't this work lel
            If Mod($i, 10) <> 0 Then $aArrayReturn[$i] = _StringRandom(20, 4)
        Next
        _FileWriteFromArray($sFile, $aArrayReturn)
    Else
        _FileReadToArray($sFile, $aArrayReturn, 1, "|")

    EndIf

    Return ($aArrayReturn)

EndFunc   ;==>_GenRandomArray

 

Edited by Inpho
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.

  • Similar Content

    • By lIlIIlIllIIIIlI
      $input = $CmdLine[1] $bytes = 1000000 ; 1000000 for 1 MB, 1000 for 1 KB $size = FileGetSize($input) $file = fileopen($input, 16) $max = ceiling($size / $bytes) for $i = 1 to $max $data = fileread($file, $bytes) $output = $input & '_' & $i & 'of' & $max filewrite($output, $data) next ^file split
      file path for a 20MB "video.mp4" is input, it will be output as "video.mp4_1of20", "video.mp4_2of20", etc. change $bytes to affect the split files size, this example made them 1MB (10^6 bytes)
       
      $input = $CmdLine[1] $name = stringtrimright($input, 1 + stringlen($input) - stringinstr($input, '_', 0, -1)) $split = stringsplit($input, 'of', 3) $max = $split[ubound($split) - 1] for $i = 1 to $max $in = $name & '_' & $i & 'of' & $max $file = fileopen($in, 16) $data = fileread($file) fileclose($file) filedelete($in) filewrite($name, $data) next ^ file join
      file path for any of the "video.mp4_Xof20" segments is input. the "video.mp4_Xof20" segments are read and written to "video.mp4" and then deleted, leaving the newly joined "video.mp4" in the end
       
      i made this today because i had a big file that i couldn't really open. i split it into 1000 chunks to make processing and stuff easier. i've also used it to split files to just under 25MB segments so i can attach and mail big files over gmail which is silly but it worked
    • By TheSaint
      I like and have been using TeraCopy, a third party program, for many years. Mostly it is a great program, but it does have some issues. On Windows XP for instance, Thumbs.db files could often hold up a copy or move process until the user manually responded to the error prompt. Some other issues I discuss below.
      PLEASE NOTE - I am not related to or affiliated in any way with the 3rd party TeraCopy program developers.
      ALSO NOTE - I myself have only tested TeraCopy Cure at this point, on Windows 7 (32 bit), and only with the free version of TeraCopy 2.27.
      This program, TeraCopy Cure, is related to another one of my TeraCopy assistant programs, TeraCopy Timer, but aims at being simpler and quicker to use ... if lacking its more advanced features.
      TeraCopy Cure is a frontend for TeraCopy and sets out to make up for its flaws and limitations.
      One of those flaws is queuing order, and the limitation relates to a same destination issue.
      You would think that queue order would be the same as add order, but that is not the case, and if you are doing a mix of COPY and MOVE then the COPY process could easily fail. It could fail with some source items, even if the  COPY process has already started before the same source MOVE process begins ... especially if the destination folder is on the source drive ... only the currently copying file is locked to that process.
      If you drag and drop another source for a same destination as an existing or impending COPY or MOVE process, then usually TeraCopy nicely adds it for you to that existing job. However, you might wish to avoid that, or it might occur during the testing phase of that active process, and then not be properly processed etc. But you are not given a choice and it just gets added.
       
           
      HOW TO USE
      See the right-click menu of the 'Batch List' (lowest field) and the right-click menu of the Tree field, for some useful options.
      (1) If desired, enable 'Auto Start'. NOTE - Even if enabled, this can be bypassed.
      (2) Set the destination folder, either by browsing on the tree or by dragging a folder to the Destination input field or label. Right-clicking on a folder in Explorer will also work, if enabled. The destination folder path will also show in the Tree if that right-click option is enabled.
      (3) Then drag & drop source file or folder onto one of three five areas - Folder (Drive) Tree field, or COPY or MOVE buttons ... this now also includes the source input and label.
      NOTE - If the Folder (Drive) Tree field is used, then you will additionally need to click either the COPY or MOVE button, to have that job added to the Batch List ... but this avoids 'Auto Start' if it is enabled. Drag & dropping to the buttons instead, saves on clicking, but starts the first job etc immediately if 'Auto Start' is enabled.
      (4) If needed and ready, click the START button to run the first job and those that follow.
      More information is included in program and in the NOTES etc sections below.
         
      WARNING - Depending on the amount of content on your destination drive, and folder level depth, and the speed of your PC, display of the full path in the Folder (Drive) Tree can take a while to show ... if you have that (right-click) option enabled ... it isn't by default. I found this feature quite tricky to get right, and I'm still not 100% sure it is now full-proof.
      TeraCopy Cure v1.5.zip  (source is included)
      TeraCopy Cure v1.6.zip  (source is included)
      NOTES
      UPDATES INFORMATION
      OLDER DOWNLOADS
       
    • By Zedna
      #include <WinAPI.au3> $text = FileReadLastChars("C:\Program Files\AutoIt3\Include\Array.au3", 1024) MsgBox(0, 'FileReadLastChars', $text) Func FileReadLastChars($sFile, $nChars)     Local $nBytes     $tBuffer = DLLStructCreate("char["&$nChars&"]")     $hFile = _WinAPI_CreateFile($sFile, 2, 2) ; open for read     _WinAPI_SetFilePointer($hFile, -1 * $nChars, 2) ; from end     _WinAPI_ReadFile($hFile, DLLStructGetPtr($tBuffer), $nChars, $nBytes)     _WinAPI_CloseHandle($hFile)     Return DLLStructGetData($tBuffer, 1) EndFunc ; included as standard UDF since AutoIt 3.2.13.6 version Func _WinAPI_SetFilePointer($hFile, $iPos, $iMethod = 0)     $aResult = DllCall( "kernel32.dll", "long", "SetFilePointer", "hwnd", $hFile, "long", $iPos, "long_ptr", 0, "long", $iMethod)     If @error Then Return SetError(1, 0, -1)     If $aResult[0] = -1 Then Return SetError(2, 0, -1) ; $INVALID_SET_FILE_POINTER = -1     Return $aResult[0] EndFunc ;==>_WinAPI_SetFilePointer Here is my topic about _WinAPI_SetFilePointer()
       
      EDIT: simpler version compatible with latest AutoIt
      $text = FileReadLastChars("C:\Program Files\AutoIt3\Include\Array.au3", 1024) MsgBox(0, 'FileReadLastChars', $text) Func FileReadLastChars($sFile , $nChars) $hFile = FileOpen($sFile, 0) ; open for read FileSetPos($hFile, -1 * $nChars, 2) ; from end $sRet = FileRead($hFile) FileClose($hFile) Return $sRet EndFunc  
    • By Jahar
      Hi All,
      Please help me on how to fetch date/time of last windows 10 system restore point using autoit? Basically its about querying the last restore point.
    • By TheDcoder
      Hi everyone,
      I am bit stumped as to why I am not able to set the time in the Date control
      #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() GUICreate("My GUI get date", 200, 200, 800, 200) Local $idDate = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20, $DTS_TIMEFORMAT) ; to select a specific default format Local $sStyle = "HH:mm:ss" GUICtrlSendMsg($idDate, $DTM_SETFORMATW, 0, $sStyle) ; Set time Local $iRet = GUICtrlSetData($idDate, '13:33:37') ConsoleWrite('GUICtrlSetData returned ' & ($iRet = 1 ? 'success' : 'failure') & @CRLF) GUISetState(@SW_SHOW) ; Loop until the user exits. While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd MsgBox($MB_SYSTEMMODAL, "Time", GUICtrlRead($idDate)) EndFunc ;==>Example The documentation for GUICtrlSetData clearly mentions that it uses the same format as GUICtrlRead:
      But I get failure
      What gives?
      Thanks for the help in advance!
×
×
  • Create New...