Leaderboard
Popular Content
Showing content with the highest reputation on 04/13/2023 in all areas
-
Introduction JSON (Javascript Object Notation) is a popular data-interchange format and supported by a lot of script languages. On AutoIt, there is already a >JSON UDF written by Gabriel Boehme. It is good but too slow, and not supports unicode and control characters very well. So I write a new one (and of course, fast one as usual). I use a machine code version of JSON parser called "jsmn". jsmn not only supports standard JSON, but also accepts some non-strict JSON string. See below for example. Important Update!! I rename the library from jsmn.au3 to json.au3. All function names are changed, too. Decoding Function Json_Decode($Json) $Json can be a standard or non-standard JSON string. For example, it accepts: { server: example.com port: 80 message: "this looks like a config file" } The most JSON data type will be decoded into corresponding AutoIt variable, including 1D array, string, number, true, false, and null. JSON object will be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). AutoIt build-in functions like IsArray, IsBool, etc. can be used to check the returned data type. But for Object and Null, Json_IsObject() and Json_IsNull() should be used. If the input JSON string is invalid, @Error will be set to $JSMN_ERROR_INVAL. And if the input JSON string is not finish (maybe read from stream?), @Error will be set to $JSMN_ERROR_PART. Encoding Function Json_Encode($Data, $Option = 0, $Indent = "\t", $ArraySep = ",\r\n", $ObjectSep = ",\r\n", $ColonSep = ": ") $Data can be a string, number, bool, keyword(default or null), 1D arrry, or "Scripting.Dictionary" COM object. Ptr will be converted to number, Binary will be converted to string in UTF8 encoding. Other unsupported types like 2D array, dllstruct or object will be encoded into null. $Option is bitmask consisting following constant: $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) $JSON_UNESCAPED_UNICODE ; Encode multibyte Unicode characters literally $JSON_UNESCAPED_SLASHES ; Don't escape / $JSON_HEX_TAG ; All < and > are converted to \u003C and \u003E $JSON_HEX_AMP ; All &amp;amp;amp;s are converted to \u0026 $JSON_HEX_APOS ; All ' are converted to \u0027 $JSON_HEX_QUOT ; All " are converted to \u0022 $JSON_PRETTY_PRINT ; Use whitespace in returned data to format it $JSON_STRICT_PRINT ; Make sure returned JSON string is RFC4627 compliant $JSON_UNQUOTED_STRING ; Output unquoted string if possible (conflicting with $JSMN_STRICT_PRINT) Most encoding option have the same means like PHP's json_enocde() function. When $JSON_PRETTY_PRINT is set, output format can be change by other 4 parameters ($Indent, $ArraySep, $ObjectSep, and $ColonSep). Because these 4 output format parameters will be checked inside Jsmn_Encode() function, returned string will be always accepted by Jsmn_Decode(). $JSON_UNQUOTED_STRING can be used to output unquoted string that also accetped by Jsmn_Decode(). $JSON_STRICT_PRINT is used to check output format setting and avoid non-standard JSON output. So this option is conflicting with $JSON_UNQUOTED_STRING. Get and Put Functions Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False) Json_Get(ByRef $Var, $Notation) These functions helps user to access object or array more easily. Both dot notation and square bracket notation can be supported. Json_Put() by default will create non-exists objects and arrays. For example: Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test" Object Help Functions Json_ObjCreate() Json_ObjPut(ByRef $Object, $Key, $Value) Json_ObjGet(ByRef $Object, $Key) Json_ObjDelete(ByRef $Object, $Key) Json_ObjExists(ByRef $Object, $Key) Json_ObjGetCount(ByRef $Object) Json_ObjGetKeys(ByRef $Object) Json_ObjClear(ByRef $Object) These functions are just warps of "Scripting.Dictionary" COM object. You can use these functions if you are not already familiar with it. == Update 2013/05/19 == * Add Jsmn_Encode() option "$JSMN_UNESCAPED_ASCII". Now the default output of Json_Encode() is exactly the same as PHP's json_encode() function (for example, chr(1) will be encoded into u0001). $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) == Update 2015/01/08 == * Rename the library from jsmn.au3 to json.au3. All function names are changed, too. * Add Json_Put() and Json_Get() * Add Null support * Using BinaryCall.au3 to loading the machine code. == Update 2018/01/13== (Jos) * Add JsonDump() to list all Json Keys and their values to easily figure out what they are. == Update 2018/10/01== (Jos) * Fixed JsonDump() some fields and values were not showing as discussed here - tnx @TheXman . == Update 2018/10/01b== (Jos) * Added Json_ObjGetItems, Tidied source and fixed au3check warnings - tnx @TheXman . == Update 2018/10/28== (Jos) * Added declaration for $value to avoid au3check warning - tnx @DerPensionist == Update 2018/12/16== (Jos) * Added another declaration for $value to avoid au3check warning and updated the version at the top - tnx @maniootek == Update 2018/12/29== (Jos) * Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string. == Update 2019/01/17== (Jos) * Added support for DOT notation in JSON functions. == Update 2019/07/15== (Jos) * Added support for reading keys with a dot inside when using a dot as separator (updated) == Update 2021/11/18== (TheXman) * Update details in below post: == Update 2021/11/20== (TheXman) * Minor RegEx update, no change to the functionality or result._Json(2021.11.20).zip1 point
-
Verify Azure membership
rsn reacted to mistersquirrle for a topic
You only need it in a while loop if the process is running and you want/need to get data while it's running. Or if you wanted to do something else in the loop while it's running. In @argumentums example and mine, ProcessWaitClose is used. So it waits until the output is done, then gets it all at once. If it was a process that ran for 10 minutes and you wanted status updates or get the output while it's running, then yes, use StdoutRead in a loop to get data as it's printed. These are the same: And: #AutoIt3Wrapper_UseX64=n ; running x86 #include <WinAPIFiles.au3> _WinAPI_Wow64EnableWow64FsRedirection(False) ; this does it =) ; ------ your code from the first post -------- #include <Constants.au3> Local $foo = Run(@WindowsDir & "\system32\dsregcmd.exe /status", @TempDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) Local $sOutput While ProcessExists($foo) Sleep(10) $sOutput &= StdoutRead($foo) If @error Then ExitLoop EndIf WEnd $sOutput &= StdoutRead($foo) MsgBox(0,"",$sOutput) Obviously argumentum's is shorter and bit cleaner, however if it wasn't a process that ends in under a second, you'd probably want to go with the loop method so you can get status updates and whatnot.1 point -
Verify Azure membership
rsn reacted to argumentum for a topic
#AutoIt3Wrapper_UseX64=n ; running x86 #include <WinAPIFiles.au3> _WinAPI_Wow64EnableWow64FsRedirection(False) ; this does it =) ; ------ your code from the first post -------- #include <Constants.au3> Local $foo = Run(@WindowsDir & "\system32\dsregcmd.exe /status", @TempDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) ProcessWaitClose($foo) Local $sOutput = StdoutRead($foo) MsgBox(0,"",$sOutput)1 point -
I agree that it is profile related. I suspect that this is the cause -- I wouldn't expect a temporary directory to be created under the Program Files directory. 🤔1 point
-
Thanks will have a look later today to see if that one does replicate the issue for me.1 point
-
Always searching for the "final" solution to my zipping/unzipping needs, I started years ago using WinRar with AutoIT (don't ask me why...) and for the last 10 years I worked well with the _zip.UDF , a good solution using the embedded windows zipfldr.dll. But often I work with a lot of data (both multi gigabytes and/or 100K+ files) and I noticed the performance of the windows zip DLL are not so good, the problem is maybe worsened by the mono thread operation using AutoIT + zipfldr.dll. SO my choice is 7zip (7ZA.exe) also for licence (freeware also for business) reasons, and I wrote a small and simple UDF: ; #INDEX# ======================================================================================================================= ; Title .........: _7za ; AutoIt Version : 3.3.16.0 ; Language ......: English ; Description ...: Functions for using 7za.exe archive manipulation app ; Author(s) .....: NSC ; Version .......: 1.2 ; Date ..........: 2022/06/28 ; =============================================================================================================================== ; ------------------------------------------------------------------------------ ; This software is provided 'as-is', without any express or ; implied warranty. In no event will the authors be held liable for any ; damages arising from the use of this software. ; #INCLUDES# =================================================================================================================== ; #include-once #include <AutoItConstants.au3> ; =============================================================================================================================== ; #VARIABLES# =================================================================================================================== ; Global Global $7za_exe = @ScriptDir & "\" & "7za.exe" ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ; _EXEC7za ;_UNcompress_7za ;_COMpress_7za_7z ;_COMpress_7za_zip ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _EXEC7za ; Description ...: launch 7Za.exe with params and returns exit codes ; Syntax ........: EXEC7za($7zCommands, $archive, $folder[, $show]) ; Parameters ....: $7zCommands - 7zip command line params ; $archive - complete path to the archive ; $folder - the source/destination folder ; $show - optional set the state of 7za console visibility, default @SW_HIDE, ; other values as ShellExecuteWait() ; Return values .: 1 - Success ; 0 - and set @error = 1 ; and ; @extended = 1 (Warning (Non fatal error(s)) ; @extended = 2 (Fatal error) ; @extended = 7 (Command line error) ; @extended = 8 (Not enough memory for operation) ; @extended = 255 (User stopped the process) ; @extended values set by 7za.exe exit codes ; Author ........: NSC ; Modified ......: 2022/05/13 ; Remarks .......: requires 7za.exe in @scriptdir, 7za.exe (7-Zip Extra: standalone console version) ; Thanks to 7-zip.org ; Related .......: ; Link ..........: ; Examples .......: compress a folder recursive with subfolders ; EXEC7za("u -mx4 -bt", c:\folder1\archive.7z", c:\folder1\folderTOcompress\ ) ; uncompress the same folder recursive ; EXEC7za("x -aoa -bt -r", "c:\folder1\archive.7z", "-oc:\folder2\") ; =============================================================================================================================== Func _EXEC_7za($7zCommands, $archive, $folder, $show = @SW_HIDE) Local $return7za = ShellExecuteWait($7za_exe, $7zCommands & ' "' & $archive & '" "' & $folder & '"', '', $SHEX_OPEN, $show) Select Case $return7za = 0 Return 1 Case Else Return SetError(1, $return7za, 0) EndSelect EndFunc ;==>_EXEC_7za ; #FUNCTION# ==================================================================================================================== ; Name ..........: _UNcompress_7za ; Description ...: launch 7Za.exe with preset params to uncompress an archive (.7z or .zip recursively) and returns exit codes ; Syntax ........: _UNcompress_7za($archive, $folder[, $show]) ; Parameters ....: $archive - complete path to the archive ; $folder - the source/destination folder ; $show - optional set the state of 7za console visibility, default @SW_HIDE, ; other values as ShellExecuteWait() ; Return values .: 1 - Success ; 0 - and set @error = 1 ; and ; @extended = 1 (Warning (Non fatal error(s)) ; @extended = 2 (Fatal error) ; @extended = 7 (Command line error) ; @extended = 8 (Not enough memory for operation) ; @extended = 255 (User stopped the process) ; @extended values set by 7za.exe exit codes ; Author ........: NSC ; Modified ......: 2022/05/19 ; Remarks .......: requires 7za.exe in @scriptdir, 7za.exe (7-Zip Extra: standalone console version) ; Thanks to 7-zip.org ; Related .......: ; Link ..........: ; =============================================================================================================================== Func _UNcompress_7za($archive, $folder, $show = @SW_HIDE) Local $return7za = ShellExecuteWait($7za_exe, "x -aoa -bt -r" & ' "' & $archive & '" -o"' & $folder & '"', '', $SHEX_OPEN, $show) Select Case $return7za = 0 Return 1 Case Else Return SetError(1, $return7za, 0) EndSelect EndFunc ;==>_UNcompress_Folder_7za ; #FUNCTION# ==================================================================================================================== ; Name ..........: _COMpress_7za_7z ; Description ...: launch 7Za.exe with precompiled params to compress in .7z format ;a single file, a filtered (*.pdf) bunch of files or a folder (recursively) and returns exit codes ; Syntax ........: _COMpress_7za_7z($archive, $folder[, $show [, $compLvl]] ) ; Parameters ....: $archive - complete path to the archive ; $folder - the source file(s) / folder ; $show - optional set the state of 7za console visibility, default @SW_HIDE, ; other values as ShellExecuteWait() ; $CompLvl - optional compression level (1-9) default 4 ; Return values .: 1 - Success ; 0 - and set @error = 1 ; and ; @extended = 1 (Warning (Non fatal error(s)) ; @extended = 2 (Fatal error) ; @extended = 7 (Command line error) ; @extended = 8 (Not enough memory for operation) ; @extended = 255 (User stopped the process) ; @extended values set by 7za.exe exit codes ; Author ........: NSC ; Modified ......: 2022/06/22 ; Remarks .......: requires 7za.exe in @scriptdir, 7za.exe (7-Zip Extra: standalone console version) ; avoids re-compress of popular archives. ; Thanks to 7-zip.org ; Related .......: ; Link ..........: ; =============================================================================================================================== Func _COMpress_7za_7z($archive, $folder, $show = @SW_HIDE, $CompLvl = 4) If StringRight($folder, 4) = ".zip" Or StringRight($folder, 3) = ".7z" Or StringRight($folder, 4) = ".rar" Or StringRight($folder, 4) = ".lha" Or StringRight($folder, 3) = ".gz" Or StringRight($folder, 7) = ".tar.gz" Or StringRight($folder, 4) = ".iso" Then $CompLvl = 0 EndIf Local $return7za = ShellExecuteWait($7za_exe, 'u -mx' & $CompLvl & ' -mmt -bt' & ' "' & $archive & '" "' & $folder & '"', '', $SHEX_OPEN, $show) Select Case $return7za = 0 Return 1 Case Else Return SetError(1, $return7za, 0) EndSelect EndFunc ;==>_COMpress_7za_7z ; #FUNCTION# ==================================================================================================================== ; Name ..........: _COMpress_7za_zip ; Description ...: launch 7Za.exe with precompiled params to compress in zip format ; a single file, a filtered (*.pdf) bunch of files or a folder (recursively) and returns exit codes ; Syntax ........: _COMpress_7za_zip($archive, $folder[, $show [, $compLvl]] ) ; Parameters ....: $archive - complete path to the archive ; $folder - the source file(s) / folder ; $show - optional set the state of 7za console visibility, default @SW_HIDE, ; other values as ShellExecuteWait() ; $CompLvl - optional compression level (1-9) default 4 ; Return values .: 1 - Success ; 0 - and set @error = 1 ; and ; @extended = 1 (Warning (Non fatal error(s)) ; @extended = 2 (Fatal error) ; @extended = 7 (Command line error) ; @extended = 8 (Not enough memory for operation) ; @extended = 255 (User stopped the process) ; @extended values set by 7za.exe exit codes ; Author ........: NSC ; Modified ......: 2022/06/22 ; Remarks .......: requires 7za.exe in @scriptdir, 7za.exe (7-Zip Extra: standalone console version), ; avoids re-compress of popular archives. ; Thanks to 7-zip.org ; Related .......: ; Link ..........: ; =============================================================================================================================== Func _COMpress_7za_zip($archive, $folder, $show = @SW_HIDE, $CompLvl = 9) If StringRight($folder, 4) = ".zip" Or StringRight($folder, 3) = ".7z" Or StringRight($folder, 4) = ".rar" Or StringRight($folder, 4) = ".lha" Or StringRight($folder, 3) = ".gz" Or StringRight($folder, 7) = ".tar.gz" Or StringRight($folder, 4) = ".iso" Then $CompLvl = 0 EndIf Local $return7za = ShellExecuteWait($7za_exe, 'u -tzip -mx' & $CompLvl & ' -mmt -bt' & ' "' & $archive & '" "' & $folder & '"', '', $SHEX_OPEN, $show) Select Case $return7za = 0 Return 1 Case Else Return SetError(1, $return7za, 0) EndSelect EndFunc ;==>_COMpress_7za_zip You have to provide 7za.exe, in scriptdir in some way, maybe with a fileinstall or embedding in some way. Daily I use most of the time: _UNcompress_7za _COMpress_7za_7z so I'am almost done with these funcs.... Also I made a quick and dirty benchmark on some real world data (for me at least) , comparing the windows DLL, the zip ULTRA by 7zip and the various 7zip levels. My choice is level 4 (time/size) but your mileage may vary... Also, extracting many thousands of little files from a 7z archive with 7zip is waaaay fast in respect to other solutions.1 point
-
Here is partially fixed code but it still doesn't have much sense. Post example of desired output INI file ... #include <Misc.au3> #include <File.au3> #include <Array.au3> #include <GUIConstantsEx.au3> _Singleton(@ScriptName, 0) Global $aFile,$bFile _FileReadToArray("Datalar.txt", $aFile, $FRTA_NOCOUNT) _FileReadToArray("Users.txt", $bFile, $FRTA_NOCOUNT, "=") Global $iLines = UBound($aFile) GUICreate("_ank_ File Splitting", 430, 500, -1, -1) $lv = GUICtrlCreateListView("KART ID |DATE |TIME |NAME AND SURNAME ", 10, 10, 410, 480) $sDate = '' For $i = 0 To $iLines - 1 $aTemp = StringSplit($aFile[$i], ", ", $STR_NOCOUNT) $user = GetUser($aTemp[1]) $sLine = $aTemp[1] &"|"& $aTemp[3] &"|"& $aTemp[4] &"|"& $user GUICtrlCreateListViewItem($sLine, $lv) If $aTemp[4] > "08:10:00" And $aTemp[4] < "17:00:00" Then GUICtrlSetBkColor(-1, 0xC0FFFF) If $sDate <> $aTemp[3] Then $sDate = $aTemp[3] ;~ $sFilePath = @ScriptDir &'\'& $sDate &'.txt' IniWrite(@ScriptDir &"\" &$sDate& ".ini",$user,"Value01",$aTemp[1]) IniWrite(@ScriptDir &"\" &$sDate& ".ini",$user,"Value02",$aTemp[3]) IniWrite(@ScriptDir &"\" &$sDate& ".ini",$user,"Value03",$aTemp[4]) Next GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete() Exit EndSwitch WEnd Func GetUser($ID) Local $iIndex = _ArraySearch($bFile, $ID) If $iIndex <> -1 Then Return $bFile[$iIndex][1] Else Return "Undefined ID" EndIf EndFunc1 point
-
This function has the same functionality as the one in the above post, The only difference is that the one above counts the days in sequence until it reaches the required number of days (in a kind of brute force), while the latter calculates the target week and then only counts the last few days left. This way it is much faster especially for dates far from the initial one. (I also changed the function name from _WorkDay() to _DateAddWorkDay) I copied the calculation from <this post> by @Nine (thanks) if you find any bugs please report. #include <date.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateAddWorkDay ; Description ...: Calculates a new date by adding/subtracting a specified number of Days from an initial date, excluding from the count ; those days corresponding to specified weekdays. (By defaul Sundays and Saturdays are excluded from the count). ; You can freely choose the days of the week to be considered as non-working days. See the third parameter for this. ; Syntax ........: _DateAddWorkDay([$StartDate = _NowCalcDate([, $iDays = 0[, $sHolidays = "17"]]]) ; Parameters ....: $StartDate - Initial date in the format YYYY/MM/DD ; (no check is made on the correctness of the date provided) ; ; $iDays - Number of business days to add/subtract (use negative number to count back) ; a zero value indicates that the StartDate itself will be returned. ; (Indicating 0 the StartDate is returned even if this corresponds to a non-working day, ; but in this case the @error flag is set to 1. This can be useful if you need to check ; if a specified date is a working or not working day) ; ; $sHolidays - a string of digits indicating not working days of week. Default is "17". ; Indicate the days of the week to be considered as non-working days. ; (not to be considered in the count). $sHolidays represents a string with any combination ; of digits from 1 to 7 (the comma between the digits is optional). Digit correspond as follows: ; 1=Sunday, 2 =Monday , 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday. ; if you want to consider working all days of the week pass an empty string. ; (if, by contradiction, all seven days of the week are indicated as holidays, then the day ; pointed to by $Days is returned anyway, but the @error flag is also set to 1) ; ; Return values .: Target date in the "YYYY/MM/DD" format ; Author ........: Gianni ; Modified ......: ; ; Remarks .......: It is possible to force the function to return a date even if it belongs to a non-working day. ; This is possible by passing 0 as the second parameter and setting the start date to point to a non working day of the week. ; In this case, even if a holiday, that date is returned but the @error flag is set to 1. ; This could be used to check in one shot if a date belongs or not to a day of the week included in the third parameter. ; (also, by setting all seven days as holidays in the third parameter you will be rturned with the date pointed to by the ; second parameter, even if it's holiday, but also in this case the @error flag is set to 1) ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateAddWorkDay($StartDate = _NowCalcDate(), $iDays = 0, $sHolidays = "17") Local $iDirection = 1 * ($iDays < 0 ? -1 : $iDays > 0) Local $sWorkDays = StringRegExpReplace("1234567", "[" & StringRegExpReplace($sHolidays, "[^1234567]", "") & "]", "") Local $iWorkDays = StringLen($sWorkDays) If $iWorkDays = 7 Then Return SetError(1, 0, _DateAdd('D', $iDays, $StartDate)) Local $aYMD = StringSplit($StartDate, "/", 2) If Not $iDirection Then Return SetError(False = StringInStr($sWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2), 0, $StartDate) Local $iNumWeek = Int(($iDays - $iDirection) / $iWorkDays) Local $iDaysRemainder = Mod($iDays - $iDirection, $iWorkDays) + $iDirection $iDays -= $iDaysRemainder $StartDate = _DateAdd("w", $iNumWeek, $StartDate) $aYMD = StringSplit($StartDate, "/", 2) Local $nJulianDate = _DateToDayValue($aYMD[0], $aYMD[1], $aYMD[2]) While $iDaysRemainder $nJulianDate += $iDirection _DayValueToDate($nJulianDate, $aYMD[0], $aYMD[1], $aYMD[2]) $iDaysRemainder -= (StringInStr($sWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2) ? 1 : 0) * $iDirection WEnd Return SetError(0, 0, $aYMD[0] & '/' & $aYMD[1] & '/' & $aYMD[2]) EndFunc ;==>_DateAddWorkDay1 point
-
1 point