Jump to content

Get file into text document


amakrkr
 Share

Recommended Posts

Hello all.

I require some help because i am kinda stuck with a script which is supposed to do the following:

I will provide path as parameter 1 and file name without an extension as parameter 2 via cmd.

Then i need to check if 2 same named files exist on the provided path (disregarding extension)

and also last modified time (day,mont,year:hour,minute) of the both files - times must match down to a minute.

If 2 of those files are found then .txt file will be formed and something will be written to it.

I hope i made it clear what am i trying to do here.

Below is a sample script ... so far i manage to find 2 files (names matching) but still lacking time (last modified) match ... i just dont know how to do that.

Any help would be apreciated!

Thank you!

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

$File = "c:temptest.dat"        
$file_open = FileOpen($File, 2)                                                         
$Path = $CmdLine[1]                                                                                               
$filename = $CmdLine[2]
$List = _FileListToArray($Path,"*",1)                                                   
$list_cut= _FileListToArray($Path,"*",1)
$counter = 0
for $i = 1 to ubound($list) -1                                                  
                $list_cut[$i] = StringTrimRight($list[$i],4)

                if  StringCompare($list_cut[$i],$filename) = 0 Then
        $counter = $counter +1
                               if $counter > 1 Then
                               FileWriteLine($file_open,$filename & @CRLF)
                               EndIf
                EndIf

next
FileClose($File_open)
Edited by amakrkr
Link to comment
Share on other sites

  • Moderators

amakrkr,

You need to use FileGetTime to check the timestamp of the files that match. :oops:

This does what you want when I test it:

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

$File = @ScriptDir & "test.dat"
$file_open = FileOpen($File, 2)

; Force another file to exist with the same timestamp
FileOpen(@ScriptDir & "test.dbl", 2)

$Path = $CmdLine[1]
; Force a trailing 
If StringRight($Path, 1) <> "" Then $Path &= ""
$filename = $CmdLine[2]

; get list of files on path
$List = _FileListToArray($Path, "*", 1)

; Create an array to hold any matching files
Global $aMatches[$List[0] + 1][2] = [[0]]

For $i = 1 To UBound($List) - 1

    ; Delete the extension - using SRER means we can ignore the size of the extension
    $sListCut = StringRegExpReplace($List[$i], "(.*)..*", "$1")
    ; Does it match the required string
    If StringCompare($sListCut, $filename) = 0 Then
        ; Increase the count
        $aMatches[0][0] += 1
        ; Store the file name
        $aMatches[$aMatches[0][0]][0] = $List[$i]
        ; Store the modified time - remove the SS at then end of the string
        $aMatches[$aMatches[0][0]][1] = StringTrimRight(FileGetTime($Path & $List[$i], 0, 1), 2)
    EndIf

Next

; Just for display
_ArrayDisplay($aMatches, "Final")

If $aMatches[0][0] > 1 Then
    ; Look at each turn
    For $i = 1 To $aMatches[0][0] - 1
        ; And try to match with all subsequent
        For $j = 2 To $aMatches[0][0]
            ; Do the timestamps match
            If $aMatches[$i][1] = $aMatches[$j][1] Then
                ; Write to the file
                FileWrite($file_open, $Path & $aMatches[$i][0] & @CRLF)
            EndIf
        Next
    Next
EndIf

; Empty the array
$aMatches = 0

FileClose($file_open)

Please ask if you have any questions. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Melba, amakrkr,

This may or may not be pertinent but, file names may be more than 2 nodes long, e.g. "my.new.exe", "my.new.exe.txt", etc. I do NOT know what the actual limits are.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

kylomas,

Which is why I used an SRER to get the name. :D

If you try it you will see that it removes only the final extension, no matter how long it is:

$sFileName_1 = "mynew.doc"
$sFileName_2 = "my.new.docx"

$sStripped_1 = StringRegExpReplace($sFileName_1, "(.*)..*", "$1")
$sStripped_2 = StringRegExpReplace($sFileName_2, "(.*)..*", "$1")

MsgBox(0, "Stripped", $sStripped_1 & @CRLF & $sStripped_2)

Convinced now? :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

  • Moderators

kylomas,

SRE = headache

Very true! :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

amakrkr,

Apologies for thread hijack but we may both learn something here.

@M23,

Do I interpret the SRER as:

(.*) - capture all groups

. - followed by a period

.* - followed by anything else

then replace with all captured groups????

I do NOT get how this winnows the last node from the filename.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

kylomas,

Pretty close. :D

Remember that SREs are "greedy" by default and so always look for the largest possible match (unless you tell them otherwise of course) So the SRER translates as:

(.*)    - Capture as many characters as you can until...
.      - you find a . (because of the "greediness" this will always be the final one)...
.*      - which is followed by any number of characters (so we can ignore the length of the extension)

$1      - Replace the whole lot with the captured group

All clear? :oops:

I always recommend this site as a good place to learn about SREs - and GEOSoft's PCRE Toolkit (look in his sig) as a good way to play with them to see exactly what they do. :rip:

M23

Edit: I see jchd looking in - stand by for incoming! :)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Nothing to add to what our Flying DutchMan GentleMan said. You know he's an SRE guru by now.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • Moderators

jchd,

Thanks for the compliments, but I would only class myself as an apprentice guru at best. :oops:

amakrkr,

Are you still out there? :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Woah .. so many replays, sorry for a late replay. :oops:

First of all thank you all for so much of a positive feedback.

Let me start about extensions ... Function StringRegExpReplace how did i miss that? Altho i know what kind of a file extensions there will be (max 4 chars long including the dot).

I now realize i have much to learn about coding ... i was off by a mile with my way of thinking. Using arrays like that ... I have learned quite a bit today so thanks again Melba23 for taking the time and solving this problem for me. My DBA will be really happy that she wont need to copy / paste files by hand anymore. :D

PS FileGetTime - i knew about the function ... but i didnt have any idea how to implement it in my script ... you should see the scripts i tried to make before i came here posting this. :rip:

Thanks again!

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...