telmob

Find lower date in multiple files

5 posts in this topic

#1 ·  Posted (edited)

Hi,

I have multiple text files with multiple dates inside on every line 6 of each file, with format 30/02/2017. If i use the code below, i can check for the dates in each file:

#Include <File.au3>
$path = FileOpenDialog("SELECT",@ScriptDir,"All (*.txt)",1)
Dim $LINE
_FileReadToArray($path,$LINE)
Local $ShowDate = StringLeft($LINE[6],10)
MsgBox(0,"LINE 6",$ShowDate)

I can also display which files have specific dates:

#include <Array.au3>
#include <Constants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstants.au3>
#include <File.au3>
#include <StringConstants.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>

GUICreate("listview items", 646, 430, 100, 200, -1)
GUISetState(@SW_SHOW)

MakeList()

Func MakeList()
    Local $aArray = _FindInFile('20/02/2017', @ScriptDir, '*.txt')
    $listviewD = GUICtrlCreateListView("     Data Confere", 488, 5, 156, 390)
    For $INDEX = 1 To $aArray[0]
        $sFileName = StringRegExpReplace($aArray[$INDEX], "^.*\\|\.*$", "")
        $sFileNameTrim = StringTrimRight($sFileName,4)
        GUICtrlCreateListViewItem($sFileNameTrim, $listviewD)
    Next
    $Label2 = GUICtrlCreateLabel(_GUICtrlListView_GetItemCount($listviewD), 488, 400, 36, 17)
EndFunc   ;==>MakeList

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func _FindInFile($sSearch, $sFilePath, $sMask = '*', $fRecursive = True, $fLiteral = Default, $fCaseSensitive = Default, $fDetail = Default)
    Local $sCaseSensitive = $fCaseSensitive ? '' : '/i', $sDetail = $fDetail ? '/n' : '/m', $sRecursive = ($fRecursive Or $fRecursive = Default) ? '/s' : ''
    If $fLiteral Then
        $sSearch = ' /c:' & $sSearch
    EndIf
    If $sMask = Default Then
        $sMask = '*'
    EndIf

    $sFilePath = StringRegExpReplace($sFilePath, '[\\/]+$', '') & '\'
    Local Const $aMask = StringSplit($sMask, ';')
    Local $iPID = 0, $sOutput = ''
    For $i = 1 To $aMask[0]
        $iPID = Run(@ComSpec & ' /c ' & 'findstr ' & $sCaseSensitive & ' ' & $sDetail & ' ' & $sRecursive & ' "' & $sSearch & '" "' & $sFilePath & $aMask[$i] & '"', @SystemDir, @SW_HIDE, $STDOUT_CHILD)
        ProcessWaitClose($iPID)
        $sOutput &= StdoutRead($iPID)
    Next
    Return StringSplit(StringStripWS(StringStripCR($sOutput), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)), @LF)
EndFunc   ;==>_FindInFile

But i can't display files that have dates, in line 6, different (lower) from the date i chose.

So if i search for a specific date, i can find it and display it, but i need to get the filenames of all the files that have dates different from the date i select and display it in a listview, and i can't find a way to do that. Something like 'Not 20/02/2017' in the function below would be way to easy...: :)

Func MakeList()
    Local $aArray = _FindInFile(Not '20/02/2017', @ScriptDir, '*.txt')
    $listviewD = GUICtrlCreateListView("     Data Confere", 488, 5, 156, 390)
    For $INDEX = 1 To $aArray[0]
        $sFileName = StringRegExpReplace($aArray[$INDEX], "^.*\\|\.*$", "")
        $sFileNameTrim = StringTrimRight($sFileName,4)
        GUICtrlCreateListViewItem($sFileNameTrim, $listviewD)
    Next
    $Label2 = GUICtrlCreateLabel(_GUICtrlListView_GetItemCount($listviewD), 488, 400, 36, 17)
EndFunc   ;==>MakeList

Any ideas anyone? I admit, i'm completely lost.

Edited by telmob

Share this post


Link to post
Share on other sites



telmob,

I would do something like this:

#include <GUIConstantsEx.au3>
#include <File.au3>
#include <GuiListView.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.txt", $FLTA_FILES, True)

$hGUI = GUICreate("Test", 500, 500)

$cLV = GUICtrlCreateListView("", 10, 10, 400, 200)
_GUICtrlListView_AddColumn($cLV, "Filename", 190)
_GUICtrlListView_AddColumn($cLV, "Date", 190)

; Loop through each file and get date
For $i = 1 To $aFiles[0]
    $aLines = FileReadToArray($aFiles[$i])
    $sDate = StringLeft($aLines[5], 10) ; Note element 5 = line 6 because array is 0-based
    ; Add data to ListView
    GUICtrlCreateListViewItem($aFiles[$i] & "|" & $sDate, $cLV)
Next

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

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)

Thanks for the quick reply Melba23. I took a while to understand the code, but i got it.

But i still need to show only the files with dates lower than a specific date, for example lower than 20/02/2017.

The script is currently showing all the files and dates.

I think an easy way to solve this would be to compare the two columns and display only the items that are lower from a specific date.

I have two text files with the date 20/02/2017 and one file with a different date, for example.

 

Edited by telmob

Share this post


Link to post
Share on other sites

telmob,

Once you have the date from within each file, use _DateDiff to check it against the specific date and only display the file/date if it is earlier. You will have to get the dates into the correct format to be compared, but that is trivial.

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

Nice!

This time i got it working.

Thank you again for your time and help.

 

For $i = 1 To $aFiles[0]
    $aLines = FileReadToArray($aFiles[$i])
    $sDate = StringLeft($aLines[5], 10) ; Note element 5 = line 6 because array is 0-based
 $original = $sDate
 $new = StringRegExpReplace($original, "\A(\d*)/(\d*)/(\d*)","$3/$2/$1")
$iDateCalc = _DateDiff('D', $new, "2017/02/10")
$sLastDate = $iDateCalc <> "0"
    $sFileNameTrim1 = StringRegExpReplace($aFiles[$i], "^.*\\|\.txt", "")
    ; Add data to ListView
    If $iDateCalc <> "0" Then GUICtrlCreateListViewItem($sFileNameTrim1, $cLV)
Next

 

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