Sign in to follow this  
Followers 0
scorp

Create TXT file from a folder of files

7 posts in this topic

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

Share this post


Link to post
Share on other sites



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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
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.

 

Share this post


Link to post
Share on other sites

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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
EndIf

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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

Share this post


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
Sign in to follow this  
Followers 0