scorp Posted December 18, 2012 Share Posted December 18, 2012 I have been looking through the fourm and finding bits and pieces of code that I think might be helpful for doing this but not sure how to put it all together. What I need to do is select a folder and read all the files in that folder into an array, which this seems to work fine. #include <File.au3> #include <Array.au3> #include <GUIConstants.au3> Global $spath = FileSelectFolder("Folder Select", "") $checkpath = StringRegExp($spath,"^.+\\$",0) If $checkpath = 0 Then $spath = $spath & "\" ; Get the list Global $aList = _FileListToArray($spath) ; And this is what we get _ArrayDisplay($aList) Which the result could look like this.. 23423.jpg 1111.jpg test.jpg test.txt test.pdf 55555.jpg I would like to take this resulting array and create a file of all the numbered JPGs so that in a file named ids.txt, I would end up with this.. "23423","23423.jpg" "1111","1111.jpg" "55555","55555.jpg" So anything that was not a JPG or anything that didn't start with a number would get filtered out. Not sure how to start this one any help would be greatly appreciated.. Scorp Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 18, 2012 Moderators Share Posted December 18, 2012 scorp,Firstly, limit the _FileListToArray return to "*.jpg" files only - then all you have to worry about is the initial digit.Loop through the array and get the first character of each return with StringLeft - convert it to ASCII (use Asc) and you can then use a Select structure to see if it is in the correct range (48-57 decimal). If it is then use FileWriteLine to add the data to the file you want to create.All clear? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
bogQ Posted December 18, 2012 Share Posted December 18, 2012 (edited) if StringIsDigit can do additional help Func myfunc($string) If StringRight($string, 4) = '.jpg' Then $check = StringLeft($string,StringLen($string)-4) If StringIsDigit($check) Then $data = '"'&$check&'","'&$string&'"' MsgBox(0, "", $data) Return $data EndIf EndIf EndFunc Edited December 18, 2012 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost. Link to comment Share on other sites More sharing options...
UEZ Posted December 18, 2012 Share Posted December 18, 2012 Try this: #include <Array.au3> #include <Constants.au3> $sPath = "C:Temp" $sExt = "*.jpg" $iPID = Run(@ComSpec & " /c dir /s /b " & $sExt, $sPath, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) Global $sLines Do $sLines &= StdoutRead($iPID) If @error Then ExitLoop Until False $aFilter = StringRegExp($sLines, "(.*d+.*.jpg)", 3) Global $aResult[UBound($aFilter)][2] For $i = 0 To UBound($aFilter) - 1 $aResult[$i][1] = $aFilter[$i] $aResult[$i][0] = StringRegExpReplace($aFilter[$i], ".*(.*).jpg", "$1") Next _ArrayDisplay($aResult) Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
scorp Posted December 18, 2012 Author Share Posted December 18, 2012 Thanks for the great suggestions everyone.. I ended up with this and it worked great. #include <File.au3> #include <Array.au3> #include <GUIConstants.au3> $txtname = "IDLINK.TXT" Global $spath = FileSelectFolder("Select Folder", "",6) $checkpath = StringRegExp($spath,"^.+$",0) If $checkpath = 0 Then $spath = $spath & "" ; Get the JPG filtered Array Global $aList = _FileListToArray($spath, "*.jpg") Local $file = FileOpen($spath & $txtname, 2) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Loop through Array picking only Digits For $i = 1 To $aList[0] $check = StringLeft($aList[$i],StringLen($aList[$i])-4) If StringIsDigit($check) Then ;Write to file FileWriteLine($spath & $txtname,'"'&$check&'","'&$alist[$i]&'"') EndIf Next ;Close file FileClose($spath & $txtname) MsgBox (0, "Done", "File Created.") One thing I noticed was if I hit cancel on the fileselectfolder I get an error... anyone know how to handle that error? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 18, 2012 Moderators Share Posted December 18, 2012 scorp,Check of you have a valid return or if @error is set on return from the function:Global $spath = FileSelectFolder("Select Folder", "", 6) If $spath = "" Then ; Or you could use "If @error Then" instead ; Run some error code here Else ; Run your code here EndIfAll clear? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
scorp Posted December 18, 2012 Author Share Posted December 18, 2012 That worked great.. Thanks for the quick reply.. #include <File.au3> #include <Array.au3> #include <GUIConstants.au3> $txtname = "IDLINK.TXT" Global $spath = FileSelectFolder("Select Folder", "",6) if $spath = "" Then MsgBox (0, "Exit","Program Aborted.") Exit Else $checkpath = StringRegExp($spath,"^.+$",0) If $checkpath = 0 Then $spath = $spath & "" EndIf ; Get the JPG filtered Array Global $aList = _FileListToArray($spath, "*.jpg") Local $file = FileOpen($spath & $txtname, 2) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Loop through Array picking only Digits For $i = 1 To $aList[0] $check = StringLeft($aList[$i],StringLen($aList[$i])-4) If StringIsDigit($check) Then ;Write to file FileWriteLine($spath & $txtname,'"'&$check&'","'&$alist[$i]&'"') EndIf Next ;Close file FileClose($spath & $txtname) MsgBox (0, "Done", "File Created.") Exit 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