Sign in to follow this  
Followers 0
Juggernaut

All text files?

24 posts in this topic

I'm wondering if its possible to find and retrieve ALL text files from ONE specific directory, read the information in each of them, and then write all of it into 1 text document. Is this any way possible? if so, could someone push me in the right direction? I've google'd for days

Share this post


Link to post
Share on other sites



I'm wondering if its possible to find and retrieve ALL text files from ONE specific directory, read the information in each of them, and then write all of it into 1 text document. Is this any way possible? if so, could someone push me in the right direction? I've google'd for days

Yes it is possible and the Helpfile is a good start.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Well, yeah. I've checked the help file, but couldn't really find it. I found single files, but not ALL text files from that specific folder.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well, yeah. I've checked the help file, but couldn't really find it. I found single files, but not ALL text files from that specific folder.

Did you see FileFindFirstFile() and FileFindNextFile() ? Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

_filelisttoarray()

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ah nice. I just looked that up. It returns all the text files. Is there also a way to read EACH of those, and then write all of them to ONE single file? Maybe using fileopen and filewrite? Not sure, any suggestions?

Edit: I have this, it writes the file names to the text file, but not their contents. Why? Also, for some reason it creates these files on my desktop.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 0)

Global $msg, $file, $btnLoad, $fileWrite

Example1()

; example 1
Func Example1()

    GUICreate("Sniffer", 150, 150); will create a dialog box that when displayed is centered
    GUICtrlCreateLabel("clik", 0, 0, 150, 20); label will display file info
    $btnLoad = GUICtrlCreateButton("Open", 0, 30, 50, 30); load button
    GUISetState(@SW_SHOW); will display an empty dialog box

   ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
            if $msg = $btnLoad Then
                ; Shows the filenames of all files in the current directory.
                    $search = FileFindFirstFile("PATH")  

; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf

    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        MsgBox(4096, "File:", $file)
        FileOpen($file,1)
        FileRead($file)
        FileOpen("PATH", 1)
        FileWrite("PATH", $file & @CRLF)
        FileClose($file)
        FileClose("PATH")
        
    WEnd
; Close the search handle
    FileClose($search)
    EndIf

    WEnd
    GUIDelete(); end of gui ==>
EndFunc
Edited by Juggernaut

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You've read the file, but the data was lost because you didn't store it.

Try $fileData=fileRead($file) instead. Then when you go to write to your target file, write $fileData instead of $file (which just contains the file name). There's a lot of unnecessary lines in there as well, but let's get it working first :P

Edited by james3mg

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hmm..doesn't seem to be writing antyhing now.

Edit: Actually, sometimes it writes blank lines, and sometimes it doesn't write anything..:P

Edited by Juggernaut

Share this post


Link to post
Share on other sites

you could try a For - Next loop untill youve found all your filess......

FileGetLongName ( "file" [, flag] )

_FileListToArray($sPath[, $sFilter = "*"[, $iFlag = 0]])

FileFindFirstFile ( "filename" )

FileFindNextFile ( search )

FileRead ( filehandle or "filename" [, count] )

_FileReadToArray($sFilePath, ByRef $aArray)

these are just a few File Funcs STRAIGHT from the help file.... it seems each might be of use to you... please read the helpfile shes your best friend

Share this post


Link to post
Share on other sites

I have the search already, It just wont write into the new file

Share this post


Link to post
Share on other sites

is that the WHOLE code up there? if not please post it

Share this post


Link to post
Share on other sites

Take a look at this file..

#Include <File.Au3>
$List = _FileListToArray (@ScriptDir)
For $A = '1' To $List['0']
If StringRight ($List[$A], '4') == '.txt' And StringInStr ($List[$A], 'All.txt') == '0' Then 
$Path = @ScriptDir & '\' & $List[$A]
FileWriteLine ('All.txt','>> Start : ' & $List[$A] & ' <<')
$Count = _FileCountLines ($Path)
For $B = '1' To $Count
FileWriteLine ('All.txt', FileReadLine ($Path, $B))
Next
FileWriteLine ('All.txt','>> End : ' & $List[$A] & ' <<')
EndIf 
Next

What it dose :

Lists all the files in the drive its in, if the file ends with '.txt' (text file) it will write that files context to a file called 'all.txt' (yes it will filter out 'all.txt')


Latest Projects :- New & Improved TCP Chat

Share this post


Link to post
Share on other sites

Global $TotalFilesFound = 0
_RecursiveSearch('C:\')

MsgBox(0x40, 'Title', 'Total files found: ' & $TotalFilesFound)

Func _RecursiveSearch($sWorkingDirectory = '')
    Local $hFile, $hFolder
    Local $sFullPathName, $FileName, $FolderName
    
    $hFile = FileFindFirstFile($sWorkingDirectory & '\*.txt')
    
    If $hFile <> -1 Then
        While 1
            $FileName = FileFindNextFile($hFile)
            If @error Then ExitLoop
        
            ConsoleWrite($sWorkingDirectory & '\' & $FileName & @LF)
            $TotalFilesFound += 1
        WEnd
        FileClose($hFile)
    EndIf

    $hFolder = FileFindFirstFile($sWorkingDirectory & '\*.')
    
    If $hFolder <> -1 Then 
        While 1
            $FolderName = FileFindNextFile($hFolder)
            If @error Then ExitLoop
            _RecursiveSearch($sWorkingDirectory & '\' & $FolderName)
        WEnd
        FileClose($hFolder)
    EndIf
    
EndFunc

Give it a try, put all your needs like storing path+file names or reading the file into one big text file where you see the "ConsoleWrite($sWorkingDirectory & '\' & $FileName & @LF)" line. It might seems fast but it's because it actually do nothing meaning after all except for outputting file and path names...

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Local $sPath = "C:\Somepath\Somefolder\", $sReturn = ""
$sSearch = FileFindFirstFile($sPath & "*.txt")
If NOT @Error Then
   While 1
      $sFile = FileFindNextFile($sSearch)
      If @Error Then ExitLoop
      $sReturn &= FileRead($sPath & $sFile) & @CRLF
   Wend
EndIf
FileClose($sSearch)

If $sReturn Then
   $hFile = FileOpen(@DesktopDir & "\TestResults.txt", 2)
   FileWrite($hFile, StringStripWS($sReturn, 2))
   FileClose($hFile)
Else
   MsgBox(0, "Oooooops", "Houston, we have a problem!")
EndIf

Edit: At Jos' insistence I closed out the last If statement with EndIf instead of Next. Don't ask me why it has to be that way but he says it does so now it is.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Edit: At Jos' insistence I closed out the last If statement with EndIf instead of Next. Don't ask me why it has to be that way but he says it does so now it is.

LOL

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

LOL

If you think that was bad you should have seen it before the first edit. I forgot to close out the first If at all and left out the FileClose() for the search handle. I do that with my own code just to see how many hissy fits Tidy will throw before it gives up in utter disgust.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Also, I have another quick question. How can I make it so that instead of specifying a directory in the code itself, it extracts the data from whichever directory the file is in?

Like, if i put it in Program Files, or My Music, it would do the same thing?

I tried @ScriptDir, but doesn't seem to work..is there something im missing?

Share this post


Link to post
Share on other sites

Yes, it's @ScriptDir but the first call has to be it, look at my code and replace the $sWorkingDirectory parameter on the first iteration to @ScriptDir.

Share this post


Link to post
Share on other sites

Well, I'm using this. I've tried a few variations, and none of them seem to work.

Local $sPath = @ScriptDir, $sReturn = ""
$sSearch = FileFindFirstFile($sPath & "*.txt")
If NOT @Error Then
   While 1
      $sFile = FileFindNextFile($sSearch)
      If @Error Then ExitLoop
      $sReturn &= FileRead($sPath & $sFile) & @CRLF
   Wend
EndIf
FileClose($sSearch)

If $sReturn Then
   $hFile = FileOpen(@DesktopDir & "\TestResults.txt", 2)
   FileWrite($hFile, StringStripWS($sReturn, 2))
   FileClose($hFile)
Else
   MsgBox(0, "Oooooops", "Houston, we have a problem!")
EndIf

Share this post


Link to post
Share on other sites

@ScriptDir isn't appended with the backslash so you need to append it:

$sSearch = FileFindFirstFile($sPath & "\*.txt")

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