Jump to content

text file generation


 Share

Recommended Posts

Alright guys/gals im pulling my hair out over this. My general goal is to have a program run automatically once every couple of days. This program will compile a list of everything on my server then filter out any files that were created more than say a 14 days ago. Once that list has been filtered I want to output a text file to a specific folder that lists all the newest files in my server with a directory and creation date.

example output:

19OCT2012 /folder/folder/file.avi

17OCT2012 /folder/folder/folder/music.mp3

The date obviously wont read that way but if i can find a way to make it in that format I would prefer it.

So I have very little created at this point as I'm stuck at the variable date generation part.......I have written and deleted about 50 or so lines of code and im staring at a file that says:

#include <file.au3>
#include <array.au3>
#include <GUIConstants.au3>
Global $spath = FileSelectFolder("Folder Select", "")
$checkpath = StringRegExp($spath,"^.+\\$",0)
If $checkpath = 0 Then $spath = $spath & "\"
$aArray = _FileListToArray($spath,"*.txt",1)
If Not IsArray($aArray) Then
    Exit
EndIf

I have written much more elaborate programs than this in the past that could prove very useful but I lost the source and have no way to decompile the program itself. Any help that anyone can provide will be greatly appreciated.

Link to comment
Share on other sites

alright I have added the UDF file to my include folder in autoit, now is there any way to get the automatic callouts when typing the function or is that not coded into it?

After looking through this new UDF I see that it can simplify the amount I have to code, maybe I need to take a break, but I'm feeling more lost now that I was before.....

Edited by Haagimus
Link to comment
Share on other sites

I'm feeling more lost now that I was before.....

You won't :

#include <Array.au3> ;just to display results
#include <Date.au3>
#include "RecFileListToArray.au3"

Global $iNowTime = _NowCalc(), $aTime

Global $sFolder = @DesktopDir

Global $aFiles = _RecFileListToArray($sFolder, "*", 1, 1)

Global $aFilteredFiles[$aFiles[0]][2]

For $iFile = 1 To $aFiles[0]
$aTime = FileGetTime($sFolder & "" & $aFiles[$iFile], 1)

If _DateDiff("D", $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5], $iNowTime) > 14 Then ;created more than 14 days ago
$aFilteredFiles[$aFilteredFiles[0][0] + 1][0] = $aTime[1] & StringUpper(_DateToMonth(@MON, 1)) & $aTime[0]
$aFilteredFiles[$aFilteredFiles[0][0] + 1][1] = $aFiles[$iFile]

$aFilteredFiles[0][0] += 1
EndIf
Next

ReDim $aFilteredFiles[$aFilteredFiles[0][0] + 1][2]

_ArrayDisplay($aFilteredFiles)

Br, FireFox.

Link to comment
Share on other sites

@Azjio why duplicate the work when it has already been done thank you sir very much I just need to tweak a few things here to fit my needs but this should work.

@FireFox Thank you for that, it makes more sense of things. While this does work it does not however call up the correct files in the array popup. I have a folder with a few files with modified/creation dates from 9/3/2012 to 10/20/2012, the dates that popup with the array table are not the same as what is actually on the file. would you have any idea why that is

Link to comment
Share on other sites

Got it all fixed up it was throwing me off because the formula was calculating greater than 14 days ago for creation, changed it to less than and everything works fine now. Thanks a million for the help, now all I have to do is _FileWriteFromArray a text and add in the array

EDIT: now I have another interesting problem, the file is created and the array reflected the correct files before i commented it out, but nothing gets written into the text file.

#include <Array.au3> ;just to display results
#include <Date.au3>
#include <file.au3>
#include "RecFileListToArray.au3"
Global $iNowTime = _NowCalc(), $aTime
Global $sFolder = FileSelectFolder("Select", "")
Global $aFiles = _RecFileListToArray($sFolder, "*", 1, 1)
Global $aFilteredFiles[$aFiles[0]][2]
Local $hfile = ($sFolder & "New Files.txt")
For $iFile = 1 To $aFiles[0]
$aTime = FileGetTime($sFolder & "" & $aFiles[$iFile], 0)
If _DateDiff("D", $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5], $iNowTime) < 14 Then ;created more than 14 days ago
  $aFilteredFiles[$aFilteredFiles[0][0] + 1][0] = $aTime[1] & StringUpper(_DateToMonth(@MON, 1)) & $aTime[0]
  $aFilteredFiles[$aFilteredFiles[0][0] + 1][1] = $aFiles[$iFile]
  $aFilteredFiles[0][0] += 1
EndIf
Next
ReDim $aFilteredFiles[$aFilteredFiles[0][0] + 1][2]
;_ArrayDisplay($aFilteredFiles)
FileOpen($hfile, 1)
If $hfile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileWrite($hfile, "Updated on " & $iNowTime)
_FileWriteFromArray($hfile, $aFilteredFiles, 1)
FileClose($hfile)
Edited by Haagimus
Link to comment
Share on other sites

okay did some tweaking and I can get it to update the text file with the "Updated on " & $iNowTime but for some reason I cannot get it to add the array data to the text file, I have tried all the different array options but nothing is working. Any suggestions

Local $sfile = FileOpen($hfile, 2)
If $hfile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileWriteLine($sfile, "Updated on " & $iNowTime & @CRLF)
_FileWriteFromArray($sfile, $aFilteredFiles)
FileClose($sfile)
Link to comment
Share on other sites

  • Moderators

Haagimus,

I cannot get FileWriteLine and _FileWriteFromArray to play nicely together either. :(

But I can fix your problem with a little lateral thinking: ;)

#include <Array.au3> ;just to display results
#include <Date.au3>
#include <file.au3>
#include "RecFileListToArray.au3"

Global $iNowTime = _NowCalc(), $aTime
Global $sFolder = FileSelectFolder("Select", "")
Local $hfile = ($sFolder & "New Files.txt")

Global $aFiles = _RecFileListToArray($sFolder, "*", 1, 1)
; Create new array and set [0][0] element to "Updated on" text
Global $aFilteredFiles[$aFiles[0]][2] = [["Updated on " & $iNowTime]]

; Now use [0][1] as count
For $iFile = 1 To $aFiles[0]
    $aTime = FileGetTime($sFolder & "" & $aFiles[$iFile], 0)
    If _DateDiff("D", $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5], $iNowTime) < 14 Then ;created more than 14 days ago
        $aFilteredFiles[$aFilteredFiles[0][1] + 1][0] = $aTime[1] & StringUpper(_DateToMonth(@MON, 1)) & $aTime[0]
        $aFilteredFiles[$aFilteredFiles[0][1] + 1][1] = $aFiles[$iFile]
        $aFilteredFiles[0][1] += 1
    EndIf
Next
ReDim $aFilteredFiles[$aFilteredFiles[0][1] + 1][2]
; Remove count element [0][1]
$aFilteredFiles[0][1] = ""

FileOpen($hfile, 1)
If $hfile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Write array including [0][0] element
_FileWriteFromArray($hfile, $aFilteredFiles)
FileClose($hfile)

All clear? :)

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

Still not outputting anything into the text file, i'll mess with this a bit and see if I can't figure something out

EDIT: Okay messed with the original script posted and got it narrowed down to a single line of code now

_FileWriteFromArray($sfile, $aFilteredFiles)

This is the only thing that isn't working, everything else is functioning properly. Once I enable the line of code nothing populates into the text file, if it is commented out the date/time populates.

If I add the line

_ArrayDisplay($aFilteredFiles)

It brings up the array with all the correct files in it

After a little research it looks like the array is whats causing it to fail since _FileWriteFromArray cannot support multi dimensional arrays......now to figure out a workaround

And further research still appears to show that it is a limitation of the _RecFileListToArray.au3 not being able to do 2D arrays

Edited by Haagimus
Link to comment
Share on other sites

  • Moderators

Haagimus,

Do you have write permissions to the file? :huh:

What if you try writing directly to the file:

#include <Array.au3> ;just to display results
#include <Date.au3>
#include <file.au3>
#include "RecFileListToArray.au3"

Global $iNowTime = _NowCalc(), $aTime

ConsoleWrite($iNowTime & @CRLF)

Global $sFolder = "M:ProgramAu3 Scripts" ; FileSelectFolder("Select", "")

Global $aFiles = _RecFileListToArray($sFolder, "*", 1, 1)
Global $aFilteredFiles[$aFiles[0]][2] = [["Updated on " & $iNowTime]]

Local $sFile = ($sFolder & "New Files.txt")

For $iFile = 1 To $aFiles[0]
    $aTime = FileGetTime($sFolder & "" & $aFiles[$iFile], 0)
    If _DateDiff("D", $aTime[0] & "/" & $aTime[1] & "/" & $aTime[2] & " " & $aTime[3] & ":" & $aTime[4] & ":" & $aTime[5], $iNowTime) < 14 Then ;created more than 14 days ago
        $aFilteredFiles[$aFilteredFiles[0][1] + 1][0] = $aTime[1] & StringUpper(_DateToMonth(@MON, 1)) & $aTime[0]
        $aFilteredFiles[$aFilteredFiles[0][1] + 1][1] = $aFiles[$iFile]
        $aFilteredFiles[0][1] += 1
    EndIf
Next
ReDim $aFilteredFiles[$aFilteredFiles[0][1] + 1][2]
$aFilteredFiles[0][1] = ""

_FileWriteFromArray($sFile, $aFilteredFiles

Any luck? :)

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

What version of AutoIt are you using? The reason I ask is because _FileWriteFromArray has worked with 2D arrays since version 3.3.7.x, previous versions it only worked with 1D arrays, also there's a bug in the _FileWriteFromArray 2D secton that might be an issue. See the code below for the fix for it.

Case 2
            Local $s_Temp
            For $x = $i_Base To $i_UBound
                $s_Temp = $a_Array[$x][0]
                For $y = 1 To $iDims - 1
                    $s_Temp &= $s_Delim & $a_Array[$x][$y]
                Next
                If FileWrite($hFile, $s_Temp & @CRLF) = 0 Then
                    $ErrorSav = 3
                    ExitLoop
                EndIf
            Next
    EndSwitch
; above is the original code
; this is the corrected code

          Case 2
               Local $sTemp
               Local $iCols = UBound($aArray, 2) ;<<<<<<<< added statement
               For $i = $iBase To $iUBound
                    $sTemp = $aArray[$i][0]
                    For $j = 1 To $iCols - 1 ; <<<<<<<<< Corrected section
                         $sTemp &= $sDelimeter & $aArray[$i][$j]
                    Next
                    If FileWrite($hFileOpen, $sTemp & @CRLF) = 0 Then
                         $iError = 3
                         ExitLoop
                    EndIf
               Next
     EndSwitch

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I just updated to the most current version of AIT yesterday and it is version 3.3.8.1

@Melba: I have write permissions to the file as the system owner, the program is creating and editing a file that didnt exist.

@BrewManNH: That code does not look like it is from my program where would I insert that to try to fix my problem

Edited by Haagimus
Link to comment
Share on other sites

  • Moderators

Haagimus,

Let us go back to basics - does this write the files correctly on your system: :huh:

#include <File.au3>

Global $aArray[4] = [1, 2, 3, 4]

_FileWriteFromArray(@ScriptDir & "Test_1.txt", $aArray)

Global $aArray[4][2] = [[1, 1], [2, 2], [3, 3], [4, 4]]

_FileWriteFromArray(@ScriptDir & "Test_2.txt", $aArray)

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

test_1.txt output

1

2

3

4

test_2.txt outputs nothing its blank

so it would appear it is a problem with the multi dimensional array, is there any way to split up the array and write them at separate times, maybe into an excel sheet?

Edited by Haagimus
Link to comment
Share on other sites

  • Moderators

Haagimus,

Interesting - it looks like you have a problem with the 2D version of the _FileWriteFromArray function. Well at least we now know where to look! :D

Please open the file Your_pathAutoIt3IncludeFile.au3 and look for the _FileWriteFromArray function - in my copy it is at lines #212-287. Please copy and paste your version and we will see what is different. ;)

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

Func _FileWriteFromArray($File, $a_Array, $i_Base = 0, $i_UBound = 0, $s_Delim = "|")
; Check if we have a valid array as input
If Not IsArray($a_Array) Then Return SetError(2, 0, 0)
Local $iDims = UBound($a_Array, 0)
If $iDims > 2 Then Return SetError(4, 0, 0)

; determine last entry
Local $last = UBound($a_Array) - 1
If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0

; Open output file for overwrite by default, or use input file handle if passed
Local $hFile
If IsString($File) Then
$hFile = FileOpen($File, $FO_OVERWRITE)
Else
$hFile = $File
EndIf
If $hFile = -1 Then Return SetError(1, 0, 0)

; Write array data to file
Local $ErrorSav = 0
Switch $iDims
Case 1
For $x = $i_Base To $i_UBound
If FileWrite($hFile, $a_Array[$x] & @CRLF) = 0 Then
$ErrorSav = 3
ExitLoop
EndIf
Next
Case 2
Local $s_Temp
For $x = $i_Base To $i_UBound
$s_Temp = $a_Array[$x][0]
For $y = 1 To $iDims
$s_Temp &= $s_Delim & $a_Array[$x][$y]
Next
If FileWrite($hFile, $s_Temp & @CRLF) = 0 Then
$ErrorSav = 3
ExitLoop
EndIf
Next
EndSwitch

; Close file only if specified by a string path
If IsString($File) Then FileClose($hFile)

; Return results
If $ErrorSav Then Return SetError($ErrorSav, 0, 0)
Return 1
EndFunc ;==>_FileWriteFromArray

Should I replace the code with what BreManNH posted up earlier? <---nevermind this I backup of the original File.au3 and replaced the code he specified and it broke the array function completely saying it called for undefined variables.

EDIT: dont know how i missed this before, this is the error generated by scite when running the script

G:Program Files (x86)AutoIt3Includefile.au3 (272) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$s_Temp &= $s_Delim & $a_Array[$x][$y]

$s_Temp &= $s_Delim & ^ ERROR

Edited by Haagimus
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...