Jump to content

How to add separator lines and "[", "]" to retrieved text?


Recommended Posts

I've progressed nicely on a script but I've run into snag. I'm getting everything I need by extracting contents of a list file into a blank notepad window. The only thing I feel I need to add to make it easier on the eyes is to add a few elements to the list, such as separator lines.

1) The original list text file is just a series of words or phrases on each line, represented here like so:

line 1
line 2
line 3
line 4
line 5

2) My script right now returns this, which is about halfway there:

Tue., Aug.19.2008; 19h26
-------------------------------------------
line 1
line 2
line 3
line 4
line 5

3) And I'd like to get this type of output:

Tue., Aug.19.2008; 19h26
-------------------------------------------
[  ]  line 1
-------------------------------------------
[  ]  line 2
-------------------------------------------
[  ]  line 3
-------------------------------------------
[  ]  line 4
-------------------------------------------
[  ]  line 5
-------------------------------------------

And here's the portion of the script that does #2 above:

;------ Print the list: ----------------------------------
        $PortableDrive = StringLeft(@ScriptDir, 2)
        $FILE = FileOpen($PortableDrive & "\APPS\ORGANIZING\list, SlimList (i.e., groceryList)\APP- SlimList\Lists\GroceryList.txt", 0)
        $READ = FileRead($FILE)
        FileClose($FILE)
        ClipPut($thisday & '., ' & $thismonth & '.' & @MDAY & '.' & @YEAR & '; ' & @HOUR & 'h' &  @MIN & _
                @CRLF & "-------------------------------------------" & @CRLF & _
                $READ)
        ShellExecute("notepad.exe", "", "", "Open", @SW_MAXIMIZE)
        WinWait("Notepad")
        Send("{CTRLDOWN}{END}{CTRLUP}")
        Sleep(250)
        Send ("^v")     ; retrieve text from the clipboard
What can I use, pls, to get the type of output in #3?

Thanks! ;)

Edited by Diana (Cda)
Link to comment
Share on other sites

Not a very efficient way to use copy-paste but it might work ;)

The best solution is to use _FileReadToArray then FileWriteLine (please read about these functions in the help file).

This code will do exactly what you want (but that doesn't prevent you to learn from this example ... isn't it?)

#Include <File.au3>
;------ Print the list: ----------------------------------
        Global $listArray[100]
        $PortableDrive = StringLeft(@ScriptDir, 2)
        $FILE = FileOpen($PortableDrive & "\APPS\ORGANIZING\list, SlimList (i.e., groceryList)\APP- SlimList\Lists\GroceryList.txt", 0)
        _FileReadToArray($FILE, $listArray) ;please READ _FileReadToArray from help file
        ReDim $listArray[$listArray[0]+2]   ;resize the array to not have many empty elements
        If @error Then
            MsgBox(16, "ERROR", "Error opening file")
            FileClose($FILE)
            Exit
        EndIf
        FileClose($FILE)
        $ShoppingList = FileOpen(@ScriptDir&"\ShoppingList.txt", 2) ;open/create the file (if file exists it will erase content)
        FileWriteLine($ShoppingList, $thisday & '., ' & $thismonth & '.' & @MDAY & '.' & @YEAR & '; ' & @HOUR & 'h' &  @MIN)
        FileWriteLine($ShoppingList, "-------------------------------------------")
        For $i=1 To $listArray[0]
            FileWriteLine($ShoppingList, "[ ] "&$listArray[$i])
            FileWriteLine($ShoppingList, "-------------------------------------------")
        Next
        FileClose($ShoppingList)
        ShellExecute(@ScriptDir&"\ShoppingList.txt")

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Not a very efficient way to use copy-paste but it might work :D

The best solution is to use _FileReadToArray then FileWriteLine (please read about these functions in the help file).

This code will do exactly what you want (but that doesn't prevent you to learn from this example ... isn't it?)

#Include <File.au3>
;------ Print the list: ----------------------------------
        Global $listArray[100]
        $PortableDrive = StringLeft(@ScriptDir, 2)
        $FILE = FileOpen($PortableDrive & "\APPS\ORGANIZING\list, SlimList (i.e., groceryList)\APP- SlimList\Lists\GroceryList.txt", 0)
        _FileReadToArray($FILE, $listArray);please READ _FileReadToArray from help file
        ReDim $listArray[$listArray[0]+2];resize the array to not have many empty elements
        If @error Then
            MsgBox(16, "ERROR", "Error opening file")
            FileClose($FILE)
            Exit
        EndIf
        FileClose($FILE)
        $ShoppingList = FileOpen(@ScriptDir&"\ShoppingList.txt", 2);open/create the file (if file exists it will erase content)
        FileWriteLine($ShoppingList, $thisday & '., ' & $thismonth & '.' & @MDAY & '.' & @YEAR & '; ' & @HOUR & 'h' &  @MIN)
        FileWriteLine($ShoppingList, "-------------------------------------------")
        For $i=1 To $listArray[0]
            FileWriteLine($ShoppingList, "[ ] "&$listArray[$i])
            FileWriteLine($ShoppingList, "-------------------------------------------")
        Next
        FileClose($ShoppingList)
        ShellExecute(@ScriptDir&"\ShoppingList.txt")
I can see how this would be better. I'm not at the stage yet where I can write something like this but will definitely study it to try to figure arrays out better.

I added this to my script but got one of those funny errors that target the UDF. I don't know what the error means or how to fix ...

---------------------------
AutoIt Error
---------------------------
Line 24  (File "F:\APPS\AutoIt\APP- AutoIt\Include\File.au3"):

Func _FileCountLines($sFilePath)

Error: "Func" statement has no matching "EndFunc".
---------------------------
OK   
---------------------------

Was wondering how to fix, pls? Thanks! ;)

Link to comment
Share on other sites

Diana (Cda)

Example:

$text = "line 1" & @CRLF & _
        "line 2" & @CRLF & _
        "line 3" & @CRLF & _
        "line 4" & @CRLF & _
        "line 5"

$result = StringRegExpReplace($text, "\r", @CR & "--------------------------------------------")

MsgBox(0, "", $result)
Hey, thanks! I look at this and I definitely understand this code better but something concerns me. Would this not stop at line 5? My example above should have had "... etc ..." underneath. The "grocery list" is a list of things to buy, yes, but it's turning out to be a To Do list now. Because of that, the list might have 5-35 lines or more.

Thanks! ;)

Edited by Diana (Cda)
Link to comment
Share on other sites

For your first problem: it looks like your File.au3 might be corrupted - try downloading it from somewhere.

All rasim did was to show you an example using a string ($text) providing you an example about how you should do it. It is an awesome example ... but there is need for a little work to adapt this to your script - and that's the best way for you to learn AutoIt.

Instead of giving you the code I'll give you a further hint:

You have no use for that variable ($text) - what you need to do is to put your text file content in that variable using FileRead

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

For your first problem: it looks like your File.au3 might be corrupted - try downloading it from somewhere.

Hmmm, okay. Guess I'll be hunting around for that.

All rasim did was to show you an example using a string ($text) providing you an example about how you should do it. It is an awesome example ... but there is need for a little work to adapt this to your script - and that's the best way for you to learn AutoIt.

Ah. I see. I can't always tell. I get discouraged sometimes, and sometimes I feel I have no business using AutoIt for the sheer learning curve ... <sigh> This has been, absolutely by far, the app that I could take the prize for re the length of time it's taken me to learn what I've learned. I have never had such a time frame as this ... But, then, I'm always glad that I manage to be able to keep coming back for more punishment! <g><sigh> I'm determined to beat this thing! <g>

Instead of giving you the code I'll give you a further hint:

You have no use for that variable ($text) - what you need to do is to put your text file content in that variable using FileRead

Oh, god! More cryptic stuff. <sigh> Thanks. I think I'll just live with this as is, for now, then. Attempted yet again and, so far, still nothing. I'll keep working on it, though. It might take a long time, but I now have hope. The trend lately is that if I keep coming back and looking over several weeks, eventually I'm now finding those pieces of working code that show me more clearly what to do, that are often either exactly what I need to do or close to. That's why all responses are so good because I learn new terminology to search for, too. I'm lucky enough that things aren't as critical as before because I have enough knowledge to build rudimentary working models of what I need that I then have been refining as I learn more. That wasn't the case in the not too distant past.

At any rate, I'm not giving up. I'm just beating a strategic retreat till I learn more in this regard to help. This is another project that is still beyond me as my attempts so far haven't worked but hopefully not for long.

Thanks. Much appreciated! ;)

Link to comment
Share on other sites

  • Moderators

Oh, god! More cryptic stuff. <sigh> Thanks. I think I'll just live with this as is, for now, then. Attempted yet again and, so far, still nothing. I'll keep working on it, though. It might take a long time, but I now have hope. The trend lately is that if I keep coming back and looking over several weeks, eventually I'm now finding those pieces of working code that show me more clearly what to do, that are often either exactly what I need to do or close to. That's why all responses are so good because I learn new terminology to search for, too. I'm lucky enough that things aren't as critical as before because I have enough knowledge to build rudimentary working models of what I need that I then have been refining as I learn more. That wasn't the case in the not too distant past.

At any rate, I'm not giving up. I'm just beating a strategic retreat till I learn more in this regard to help. This is another project that is still beyond me as my attempts so far haven't worked but hopefully not for long.

Thanks. Much appreciated! ;)

I'm not quite sure how that's "Cryptic".

Variable:

$text

Text file:

@ScriptDir&"\ShoppingList.txt"

Content:

FileRead(@ScriptDir&"\ShoppingList.txt")

Putting the content into a variable:

$text = FileRead(@ScriptDir&"\ShoppingList.txt")

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

if your going to have 5 - 35 entries try maybe a For...Step...Next, or a Do...until. I've got a handle on these now and they can shrink a bulky script real quick. (ie.

$n = 1
do 
$text = "line" & $n & @CRLF & _
if $text <> "" then
$n += 1
else
$n = $n - $n
endif
until $n = 0
)

Giggity

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