Mateocedillo Posted June 28, 2022 Share Posted June 28, 2022 I'm trying to create a function, which what it does is make an Array of file names so that it is later converted into a list and creates a file with the list that this Array has. The problem is that the syntax for this list or transcripts is wavs/num.wav|. So, I guess the "|" is misunderstood by the _array functions, which I call only for debugging. So I would need suggestions for this please - I'll share the code here and at the end a zip file with something to get this working. Thanks. expandcollapse popup#include <Array.au3> #include <File.au3> $bCreate = _Dataset_CreateTranscription(@scriptDir &"\mylist.txt", "FW") if @error then MsgBox(16, "Error", @error) if $bCreate then MsgBox(0, "Ready", "Your transcription has been done correctly.") Func _Dataset_CreateTranscription($sFileName, $iMaxItems, $sWavsPath = @ScriptDir &"\wavs") if FileExists($sFileName) then return SetError(1, 0, "") ;The transcript file already exists. if $iMaxItems = "FW" then $iMaxItems = 0 $hFiles = FileFindFirstFile($sWavsPath &"\*.wav") If $hFiles = -1 Then return SetError(2, 0, "") ;Files with the wav extension are not found. Local $aFileNames[], $aResult[], $iIndex = 0 While 1 $aFileNames[$iIndex] = "wavs/" &FileFindNextFile($hFiles) &"|" If @error Then ExitLoop Sleep(10) $iIndex = $iIndex +1 WEnd ;test: _ArrayDisplay($aFileNames) ;debug: if @error then MsgBox(0, "ArrayDisplay error", @error) _ArrayDelete($aFileNames, $iIndex) _ArraySort($aFileNames) _FileWriteFromArray($sFileName, $aFileNames) Else $hFile = FileOpen($sFileName, 1) If $hFile = -1 Then return SetError(3, 0, "") ;the transcription file could not be created. EndIf if not isInt($iMaxItems) then return SetError(4, 0, "") ;The maximum number of elements is not an integer. For $I = 1 to $iMaxItems FileWriteLine($hFile, "wavs/" &$I &"|") Next FileClose($hFileOpen) EndIf return true EndFunc Dataset text.zip Link to comment Share on other sites More sharing options...
Subz Posted June 28, 2022 Share Posted June 28, 2022 Works fine for me, although there were a number of other syntax errors, why not just use _FileListToArrayRec()? #include <Array.au3> #include <File.au3> Local $sFileDir = @ScriptDir & "\Dataset text" Local $sSavePath = $sFileDir & "\mylist.txt" Local $aFileList = _FileListToArrayRec($sFileDir & "\wavs", "*.wav", 1) If @error Then Exit MsgBox(4096, "Error", "No files found.") Local $hSavePath = FileOpen($sSavePath, 2) FileWrite($hSavePath, "wavs/" & _ArrayToString($aFileList, "|" & @CRLF & "wavs/", 1, -1, @CRLF) & "|" & @CRLF) FileClose($hSavePath) Link to comment Share on other sites More sharing options...
Mateocedillo Posted June 28, 2022 Author Share Posted June 28, 2022 9 hours ago, Subz said: Works fine for me, although there were a number of other syntax errors, why not just use _FileListToArrayRec()? #include <Array.au3> #include <File.au3> Local $sFileDir = @ScriptDir & "\Dataset text" Local $sSavePath = $sFileDir & "\mylist.txt" Local $aFileList = _FileListToArrayRec($sFileDir & "\wavs", "*.wav", 1) If @error Then Exit MsgBox(4096, "Error", "No files found.") Local $hSavePath = FileOpen($sSavePath, 2) FileWrite($hSavePath, "wavs/" & _ArrayToString($aFileList, "|" & @CRLF & "wavs/", 1, -1, @CRLF) & "|" & @CRLF) FileClose($hSavePath) Hello, Thanks for your answer. Yes, for a moment I had thought about the _FileListToArrayRec thing, but there is another problem here. Taking the example of your script, I want to order the elements of the Array that has the files, since with a dataset of 5 wavs like the one I passed it works fine, but in a large dataset like 500 wavs the txt of the list is untidy. For example, on one line there is wavs/1.wav| but in another there are wavs/10.wav| where wavs/2.wav| would have to go instead, that is, in an ordered way. I used _ArraySort but it gave no results. Link to comment Share on other sites More sharing options...
Solution Subz Posted June 28, 2022 Solution Share Posted June 28, 2022 As long as the file names are always numbers (or starting with a number) you could use something similar to the following: #include <Array.au3> #include <File.au3> Local $sFileDir = @ScriptDir & "\Dataset text" Local $sSavePath = $sFileDir & "\mylist.txt" Local $aFileList = _FileListToArrayRec($sFileDir & "\wavs", "*.wav", 1, 0, 2) If @error Then Exit MsgBox(4096, "Error", "No files found.") _SortList($aFileList) Local $hSavePath = FileOpen($sSavePath, 2) FileWrite($hSavePath, "wavs/" & _ArrayToString($aFileList, "|" & @CRLF & "wavs/", 1, -1, "|" & @CRLF & "wavs/", 0, 0) & "|" & @CRLF) FileClose($hSavePath) ShellExecute($sSavePath) Func _SortList(ByRef $_aSortList) _ArrayColInsert($_aSortList, 1) For $i = 1 To $_aSortList[0][0] $_aSortList[$i][1] = Number(StringReplace($_aSortList[$i][0], ".wav", "")) Next _ArraySort($_aSortList, 0, 1, 0, 1) EndFunc Link to comment Share on other sites More sharing options...
pixelsearch Posted June 29, 2022 Share Posted June 29, 2022 (edited) 19 hours ago, Mateocedillo said: I used _ArraySort but it gave no results. @Subz If we had a function that sorts naturally ("1","2","10") and not ("1","10","2") then things would often be easier. For example, a sort function based on what's found in ArrayDisplay, let's name it... _ArraySort1DLarsJ() expandcollapse popup#include <Array.au3> Local $aArray[] = ["wav7", "wav3", "wav10", "wav5", "wav4", "wav11", _ "wav6", "wav1", "wav8", "wav0", "wav2", "wav9"] ; _ArraySort($aArray) _ArraySort1DLarsJ($aArray, 2) ; 1 = numeric sort, 2 = natural sort _ArrayDisplay($aArray) ;============================================ Func _ArraySort1DLarsJ(ByRef $aArray, $iType) Local $iRows = UBound($aArray, $UBOUND_ROWS) Local $tIndex = DllStructCreate("uint[" & $iRows & "]") Local $pIndex = DllStructGetPtr($tIndex) Local $hDll = DllOpen("kernel32.dll") Local $hDllComp = DllOpen("shlwapi.dll") Local $lo, $hi, $mi, $r, $nVal1, $nVal2 For $i = 1 To $iRows - 1 $lo = 0 $hi = $i - 1 Do $mi = Int(($lo + $hi) / 2) If $iType = 1 Then ; Numeric sort $nVal1 = Number($aArray[$i]) $nVal2 = Number($aArray[DllStructGetData($tIndex, 1, $mi + 1)]) $r = $nVal1 < $nVal2 ? -1 : $nVal1 > $nVal2 ? 1 : 0 Else ; Natural sort $r = DllCall($hDllComp, 'int', 'StrCmpLogicalW', 'wstr', String($aArray[$i]), _ 'wstr', String($aArray[DllStructGetData($tIndex, 1, $mi + 1)]))[0] EndIf Switch $r Case -1 $hi = $mi - 1 Case 1 $lo = $mi + 1 Case 0 ExitLoop EndSwitch Until $lo > $hi DllCall($hDll, "none", "RtlMoveMemory", "struct*", $pIndex + ($mi + 1) * 4, _ "struct*", $pIndex + $mi * 4, "ulong_ptr", ($i - $mi) * 4) DllStructSetData($tIndex, 1, $i, $mi + 1 + ($lo = $mi + 1)) Next Local $aBackup = $aArray For $i = 0 To $iRows - 1 $aArray[$i] = $aBackup[DllStructGetData($tIndex, 1, $i + 1)] Next $tIndex = 0 DllClose($hDll) DllClose($hDllComp) EndFunc ;==>_ArraySort1DLarsJ Maybe it could help users when needed ? Edited June 29, 2022 by pixelsearch nothing special Subz, Werty and Musashi 3 Link to comment Share on other sites More sharing options...
Mateocedillo Posted June 29, 2022 Author Share Posted June 29, 2022 On 6/28/2022 at 12:08 PM, Subz said: As long as the file names are always numbers (or starting with a number) you could use something similar to the following: #include <Array.au3> #include <File.au3> Local $sFileDir = @ScriptDir & "\Dataset text" Local $sSavePath = $sFileDir & "\mylist.txt" Local $aFileList = _FileListToArrayRec($sFileDir & "\wavs", "*.wav", 1, 0, 2) If @error Then Exit MsgBox(4096, "Error", "No files found.") _SortList($aFileList) Local $hSavePath = FileOpen($sSavePath, 2) FileWrite($hSavePath, "wavs/" & _ArrayToString($aFileList, "|" & @CRLF & "wavs/", 1, -1, "|" & @CRLF & "wavs/", 0, 0) & "|" & @CRLF) FileClose($hSavePath) ShellExecute($sSavePath) Func _SortList(ByRef $_aSortList) _ArrayColInsert($_aSortList, 1) For $i = 1 To $_aSortList[0][0] $_aSortList[$i][1] = Number(StringReplace($_aSortList[$i][0], ".wav", "")) Next _ArraySort($_aSortList, 0, 1, 0, 1) EndFunc Thank you, the truth is that the two solutions have served me. The truth is that it seemed unpleasant to me that the list appeared out of order, I thought that was already a defect of the search engine or the system and it is a bit difficult to deal with something like making an order. Again, thanks for u help. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now