Jump to content

List all files from different folders and generate log.txt


Recommended Posts

Hi Experts,

Hello, how's everybody🙂. Sorry for this but I just need your help on my problem. I have this "FolderList.txt" that has the list of all folders (a hundred or more were listed) that I need to check individually and I need to get inside each folder and get the files filenames and FileWrite() them in my "Log.txt" output. See below.

1. Read my FolderList.txt (where all folder names are their)

2. List all files filename found from each folder (from my FolderList.text) in my Log.txt output.

 

Here's what I have so far (No success😞

#Include <File.au3>
#Include <Array.au3>

$FilePath = "D:\WorkDIR\Office\Server\in\" ; This is the main path where all these folders were found

$File = FileOpen(@ScriptDir & "\FolderList.txt") ; This is where all the folders that I need to check individually
$xFile = @ScriptDir & "\Log.txt" ; This is the log output
FileOpen($File, 0)
For $i = 1 to _FileCountLines($File)
    $line = FileReadLine($File, $i)
    ; msgbox(0,'','the line ' & $i & ' is ' & $line)
Local $MainFolder  = $FilePath & $line
Next

GetFile()

Func GetFile()
    ; List all the files and folders in the directory using the default parameters.
    Local $aFileList = _FileListToArray($MainFolder, "*", 1)
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ; Display the results returned by _FileListToArray.
    _ArrayDisplay($aFileList, $FilePath & $line)
    If Not @Error Then
     _FileWriteFromArray($xFile, $aFileList, Default, Default, @CRLF)
    EndIf
     FileClose($xFile)
     ShellExecute(@ScriptDir & "\Log.txt")
EndFunc   ;==>Example

Here's my sample FolderList.txt, I just added few folder name.

FolderList.txt

 

This is the expected output on my Log.txt file.

BTL105131
 - Filename here...
 - Filename here...
 - Filename here...
 - Filename here...
 
 SYM100758
  - Filename here...
  - Filename here...
  - Filename here...
  - Filename here...

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

A few things, just at a glance, haven't tested. 

Firstly, since the scope of your $MainFolder variable is local, when you execute the GetFile function, that variable doesn't exist. 

Secondly, you overwrite the $MainFolder variable each time your For Loop increments.

To address these, I would recommend you make your GetFile() function have a parameter that you feed the current $MainFolder (path) to.

I also recommend you move your FileClose and ShellExecute outside of  the GetFile function. 

 

Hope that helps.

Link to comment
Share on other sites

@spudw2k, Yup, thanks. I'm still figuring out the arrangement since that code is just my first attempt on my problem. This will be adjusted accordingly, but for now I just need to get the output and that would be great if you could share something or guide me on how to handle this scenario. Thanks a lot.

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@spudw2k I change the code. It is generating now all the filenames from a folder but still I need something that generate based on the scenario I've given.

$FilePath = "D:\WorkDIR\Office\Server\in\"

$File = FileOpen(@ScriptDir & "\FolderList.txt")
$xFile = FileOpen(@ScriptDir & "\Log.txt", 1)
FileOpen($File, 0)
For $i = 1 to _FileCountLines($File)
    $line = FileReadLine($File, $i)
;~     msgbox(0,'','the line ' & $i & ' is ' & $line)
    Global $MainFolder  = $FilePath & $line
    Local $aFileList = _FileListToArray($MainFolder, "*", 1)
        If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ; Display the results returned by _FileListToArray.
;~     _ArrayDisplay($aFileList, $line)
    FileWrite($xFile, $line & @CRLF) ; I added thhis to generate the parent folder before its files
    If Not @Error Then
     _FileWriteFromArray($xFile, $aFileList, 1, Default, @CRLF) ; this is the filenames
    EndIf
Next

FileClose($xFile)
ShellExecute(@ScriptDir & "\Log.txt")

 

I still need to make my output log as below.

BTL105131
 - Filename here...
 - Filename here...
 - Filename here...
 - Filename here...
 
 SYM100758
  - Filename here...
  - Filename here...
  - Filename here...
  - Filename here...

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@KickStarter15
This is one of many ways to do what you are looking for:

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>


If Test() Then MsgBox($MB_ICONINFORMATION, "", "File list exported correctly.", 10)

Func Test()

    Local $arrFolderList, _
          $strSourceFoldersPath, _
          $strFolder, _
          $arrFileList, _
          $arrFileListFormatted[0], _
          $strFileListFileName = @ScriptDir & "\FileListFromFolders.txt"

    ; Defining the folders path
    $strSourceFoldersPath = @ScriptDir & "\TestFolders"

    ; Read the list of the folders from which you need to retrieve the list of the files
    $arrFolderList = FileReadToArray(@ScriptDir & "\FolderList.txt")
    If @error Then Return ConsoleWrite("FileReadToArray ERR: " & @error & @CRLF)

    ; For each folder in the folders' array
    For $strFolder In $arrFolderList

        ; Getting the list of files (ONLY) in the folder
        $arrFileList = _FileListToArray($strSourceFoldersPath & "\" & $strFolder & "\", "*", $FLTA_FILES, False)

        ; In case of error
        If @error > 0 And @error < 4 Then
            Return ConsoleWrite("_FileListToArray ERR: " & @error & @CRLF)

        ; No files found (you could even remove this ElseIf)
        ElseIf @error = 4 Then
            _ArrayAdd($arrFileListFormatted, $strFolder & "|" & "No files found")
        Else
            ; Deleting the first element of the array (number of files found)
            _ArrayDelete($arrFileList, 0)

            ; Adding the folder which contains all the files found
            _ArrayAdd($arrFileListFormatted, $strFolder)

            ; Adding the list of files found
            _ArrayAdd($arrFileListFormatted, $arrFileList)
            If @error Then Return ConsoleWrite("_ArrayAdd ERR: " & @error & @CRLF)
        EndIf

    Next

    ; Display the content of the array
    _ArrayDisplay($arrFileListFormatted)

    ; (Over)Write the content of the array in the file
    _FileWriteFromArray($strFileListFileName, $arrFileListFormatted)
    If @error Then Return ConsoleWrite("_FileWriteFromArray ERR: " & @error & @CRLF)

    Return 1

EndFunc

Cheers ^_^

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Link to comment
Share on other sites

@FrancescoDiMuro thanks but it is not working as expected. There is no output generated. It will just prompt the message box "File list exported correctly."

@Nine Thanks as well but the still not the expected output that I need.

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Link to comment
Share on other sites

@Nine I tried replacing the code with the suggested of your but it will just add the total number of files being read and not like below as expected.

BTL105131
 - Filename here...
 - Filename here...
 - Filename here...
 - Filename here...
 
 SYM100758
  - Filename here...
  - Filename here...
  - Filename here...
  - Filename here...

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@KickStarter15

Did you check the @ScriptDir?

Because the MsgBox only appears if the _FileWriteFromArray() function returns 1, and so, it has written correctly the file to the specified path.

EDIT: Did you see the array of files path and files list for each path?

Any error in the console?

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

@FrancescoDiMuro, I got it now working. Thanks. I still have another problem below. I need to show these process in my EditBox GUICtrlCreateEdit() or in _GUICtrlRichEdit_Create() using GUICtrlSetData() or _GUICtrlRichEdit_InsertText() but both are not working.

BTL105131
 - Filename here...
 - Filename here...
 - Filename here...
 - Filename here...
 
 SYM100758
  - Filename here...
  - Filename here...
  - Filename here...
  - Filename here...

 

Here's the code I tried:

$File = FileOpen(@ScriptDir & "\FolderList.txt")
Global $xFile = FileOpen(@ScriptDir & "\Log.txt", 1)
FileOpen($File, 0)
For $i = 1 to _FileCountLines($File)
$line = FileReadLine($File, $i)
Global $MainFolder  = $sFileSelectFolder & $line
Local $aFileList = _FileListToArray($MainFolder, "*", 1)

If Not @Error Then
    _FileWriteFromArray($xFile, $aFileList, 1, Default, @CRLF) ; I need to insert the progress of $aFileList in editbox
    _GUICtrlRichEdit_InsertText($Edit1, $aFileList) ; Here's the attempt but not working
    If $aFileList = 0 Then
    _GUICtrlRichEdit_InsertText($Edit1, "..................... Not Found here: " & $sFileSelectFolder & $line)
    EndIf
EndIf

 

I need it to be in my editbox same as below.

image.png.e98a3d98f25c4ebf6616bf0c3ec16e52.png

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

1 hour ago, KickStarter15 said:

If $aFileList = 0 Then

I'm quite sad because in the example I provided to you I used the @error flag to see if the array has been filled or not, so, this lines doesn't make any sense, since $aFileList is an array, so you need to see if the array is empty with UBound(), or if the array is an array with IsArray(), or, as I did, check the @error flag to be equal 4 when no files are found.
Good luck with your script.

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...