Jump to content

text file generation


 Share

Recommended Posts

  • Moderators

Haagimus,

Not just yet. :)

Try running this:

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)



Func _FileWriteFromArray($sFilePath, $aArray, $iBase = 0, $iUBound = 0, $sDelimeter = "|")
    ; Check if we have a valid array as input
    If Not IsArray($aArray) Then Return SetError(2, 0, 0)

    ; Check the number of dimensions
    Local $iDims = UBound($aArray, 0)
    If $iDims > 2 Then Return SetError(4, 0, 0)

    ; Determine last entry of the array
    Local $iLast = UBound($aArray) - 1
    If $iUBound < 1 Or $iUBound > $iLast Then $iUBound = $iLast
    If $iBase < 0 Or $iBase > $iLast Then $iBase = 0

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

    ; Write array data to file
    Local $iError = 0
    Switch $iDims
        Case 1
            For $i = $iBase To $iUBound
                If FileWrite($hFileOpen, $aArray[$i] & @CRLF) = 0 Then
                    $iError = 3
                    ExitLoop
                EndIf
            Next
        Case 2
            Local $sTemp
            Local $iCols = UBound($aArray, 2)
            For $i = $iBase To $iUBound
                $sTemp = $aArray[$i][0]
                For $j = 1 To $iCols - 1
                    $sTemp &= $sDelimeter & $aArray[$i][$j]
                Next
                If FileWrite($hFileOpen, $sTemp & @CRLF) = 0 Then
                    $iError = 3
                    ExitLoop
                EndIf
            Next
    EndSwitch

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

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

This is using the function from the beta File.au3 include. :)

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

This fixed it :graduated:

Now can I specify what the in line breaks are delineated by i would like to use something other than " | " to put a little more space in there, I know there is a way i can do that in the script im writing itself i just have no idea how to do it.

Can i get a hint to doing it without the actual code itself this one i would like to try to figure out myself

Edited by Haagimus
Link to comment
Share on other sites

  • Moderators

Haagimus,

Just run the script - do not play around with the #include file itself. :)

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

yeah that's what i mean is there any way in my script to change the space pipe space in line to say tab or whatever, I'm not going to mess with the includes otherwise ill break something

Never mind this i didn't RTFM i changed up the $s_delim and I'm all good now

Thanks a million for the help everyone this is good to go now you guys all rock

Edited by Haagimus
Link to comment
Share on other sites

The section of code I posted, as explained in the post, was a fix for a bug that is in the current _FileListFromArray function as distributed with the release version of AutoIt. It affects writing a 2D array to a file.

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

  • Moderators

Haagimus,

I suggest you first download the Beta version of AutoIt so that you have a working 2D function in the #include file. :)

Then, seeing you do not want actual code, I suggest you look at the Help file entry for the function and see if any of the parameters strike you as useful in changing the delimiter. ;)

You know where we are if you cannot find the answer yourself - but stand by for a bit of banter if you do have to ask! :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

Haagimus,

I suggest you first download the Beta version of AutoIt so that you have a working 2D function in the #include file. :)

Then, seeing you do not want actual code, I suggest you look at the Help file entry for the function and see if any of the parameters strike you as useful in changing the delimiter. ;)

You know where we are if you cannot find the answer yourself - but stand by for a bit of banter if you do have to ask! :D

M23

lol I know, banter is all good if I learn something in the end, I was in the military after all for 10 years I'm used to it. I did look at the call outs for the _FileWriteFromArray and failed to see the $s_Delim earlier. I got it all figured out now and I thank you very much for your help.

Link to comment
Share on other sites

  • Moderators

Haagimus,

Glad I could help. :)

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

M23 if your still around I have run into a strange occurrence with this script now. Everything works beautifully except that the date for every single file reads as 10OCT2012, that does not match up with the files in the folders. Not a big deal if I can't fix it, ill just remove it and have the directories only but the dates are nice to have for people accessing my server.

I'm assuming the problem lies somewhere in the section of code that lies within the "if _DateDiff" section but I'm uncertain where:

Global $NowTime = _NowCalc(), $Time ;Gets the current date and time
;Global $Folder = "B:ServerFolders"
Global $Folder = FileSelectFolder("Select", "") ;Testing only
Global $Files = _RecFileListToArray($Folder, "*", 1, 1, 0, 1, "*.db;*.pst;*.rwz;*.ini;*.jpg;*.dat;*.tmp;*.configdat;*.cc;*.txt") ;Adds the files from the folder selected to an array
Global $FilteredFiles[$Files[0]][2]

Local $File = (@ScriptDir & "00-READ ME FIRST - NEWEST FILES LISTED WITHIN.txt")

For $iFile = 1 To $Files[0]
$Time = FileGetTime($Folder & "" & $Files[$iFile], 0)

If _DateDiff("D", $Time[0] & "/" & $Time[1] & "/" & $Time[2] & " " & $Time[3] & ":" & $Time[4] & ":" & $Time[5], $NowTime) < 14 Then ;created more than 14 days ago
$FilteredFiles[$FilteredFiles[0][0] + 1][0] = $Time[1] & StringUpper(_DateToMonth(@MON, 1)) & $Time[0]
$FilteredFiles[$FilteredFiles[0][0] + 1][1] = $Files[$iFile]

$FilteredFiles[0][0] += 1
EndIf ;The above formula tells the program to find anything in the array that is older than 14 days and discard it
Next

ReDim $FilteredFiles[$FilteredFiles[0][0] + 1][2] ;Resize array

FileOpen($File, 2)
If $File = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf

FileWriteLine($File, "Updated on " & $NowTime & @CRLF & @CRLF) ;Add header to text file with time it was updated
FileWriteLine($File, "This file is automatically generated everyday at midnight and noon" & @CRLF & @CRLF)
_FileWriteFromArray($File, $FilteredFiles, 2, 0, " < Creation --- Location > ") ;Write array to the text file
FileClose($File)
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...