paulpmeier Posted August 28, 2008 Share Posted August 28, 2008 (edited) A usefull feature of the FileOpen function:FileOpen("con", 4) opens console for binary input.#AutoIt3Wrapper_Change2CUI=y $input = "" ConsoleWrite(@CRLF & "Write some text and close with ENTER: ") $file = FileOpen("con", 4) While 1 $chr = FileRead($file, 1) If $chr = @LF Then ExitLoop $input = $input & BinaryToString($chr) WEnd FileClose($file) ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF)To read lines instead of single characters use this code:#AutoIt3Wrapper_Change2CUI=y $maxchr = 123 $input = "" ConsoleWrite(@CRLF & "Write some text and close with ENTER: ") $file = FileOpen("con", 4) $line = BinaryToString(FileRead($file, $maxchr)) FileClose($file) ConsoleWrite(@CRLF & "Your input was: " & $line & @CRLF)Compile as CUI and start the exe-file from console prompt.(Tested with AutoIt 3.2.12.1 and Windows XP SP2)PaulSee also: Reading from serial ports with FileRead Edited September 14, 2008 by paulpmeier Link to comment Share on other sites More sharing options...
ResNullius Posted August 28, 2008 Share Posted August 28, 2008 A usefull feature of the FileOpen function: FileOpen("con", 4) opens console for binary input. #AutoIt3Wrapper_Change2CUI=y $input = "" ConsoleWrite(@CRLF & "Write some text and close with ENTER: ") $file = FileOpen("con", 4) While 1 $chr = FileRead($file, 1) If $chr = @CR Then ExitLoop $input = $input & BinaryToString($chr) WEnd FileClose($file) ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF) Compile as CUI and start the exe-file from console prompt. (Tested with AutoIt 3.2.12.1 and Windows XP SP2) PaulExcellent find Paul! Out of curiosity, did you have this knowledge from another programming language, or did you just reason it out? Link to comment Share on other sites More sharing options...
paulpmeier Posted August 28, 2008 Author Share Posted August 28, 2008 Hello ResNullius I remembered a similar old Microsoft Basic statement. So I tried it with the AutoIt FileRead function. And it works. Paul Link to comment Share on other sites More sharing options...
Nahuel Posted August 28, 2008 Share Posted August 28, 2008 That's a great example. Thank you. Link to comment Share on other sites More sharing options...
Cyber Posted January 14, 2009 Share Posted January 14, 2009 FANTASTIC! THANKS!! Console Browse: Navigate on the WEB in a textual consoleMultiPing!: Show computer on the lan and/or show the local/remote task, ALL animated!KillaWin: Event executingCryptPage: Crypt your webpage and show only with key Link to comment Share on other sites More sharing options...
sniper120 Posted May 12, 2009 Share Posted May 12, 2009 #AutoIt3Wrapper_Change2CUI=y $input = "" ConsoleWrite(@CRLF & "Write some text and close with ENTER: ") $file = FileOpen("con", 4) While 1 $chr = FileRead($file, 1) If $chr = @LF Then ExitLoop $input = $input & BinaryToString($chr) WEnd FileClose($file) ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF) There is an error with the output! A Line Feed (@LF) is placed at the end of the string. This screwed my code up! Here is the fix: ConsoleWrite(@CRLF & "Messge: ") $file = FileOpen("con", 4) While 1 $chr = FileRead($file, 1) If $chr = @LF Then ExitLoop $pin = $pin & BinaryToString($chr) WEnd FileClose($file) $pin = StringStripCR($pin); <-- Fix Look at the last line of the code. Link to comment Share on other sites More sharing options...
ResNullius Posted May 12, 2009 Share Posted May 12, 2009 (edited) There is an error with the output! A Line Feed (@LF) is placed at the end of the string. This screwed my code up! Here is the fix:Funny, but I wouldn't always get the extra @LF. Nonetheless, I think this is probably the better fix: #AutoIt3Wrapper_Change2CUI=y $input = "" ConsoleWrite(@CRLF & "Write some text and close with ENTER: ") $file = FileOpen("con", 4) While 1 $chr = FileRead($file, 1) If $chr = @CR Or $chr = @LF Then ExitLoop; <------- Added check for @CR as well as @LF $input = $input & BinaryToString($chr) WEnd FileClose($file) ClipPut($input & "NewLine") ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF) Edited May 12, 2009 by ResNullius Link to comment Share on other sites More sharing options...
GaryFrost Posted May 12, 2009 Share Posted May 12, 2009 Funny, but I wouldn't always get the extra @LF. Nonetheless, I think this is probably the better fix: #AutoIt3Wrapper_Change2CUI=y $input = "" ConsoleWrite(@CRLF & "Write some text and close with ENTER: ") $file = FileOpen("con", 4) While 1 $chr = FileRead($file, 1) If $chr = @CR Or $chr = @LF Then ExitLoop; <------- Added check for @CR as well as @LF $input = $input & BinaryToString($chr) WEnd FileClose($file) ClipPut($input & "NewLine") ConsoleWrite(@CRLF & "Input was: " & $input & @CRLF) Don't think I would exit the loop, rather continueloop, might be a lf or cr in between other lines. SciTE for AutoItDirections for Submitting Standard UDFs  Don't argue with an idiot; people watching may not be able to tell the difference.  Link to comment Share on other sites More sharing options...
ResNullius Posted May 12, 2009 Share Posted May 12, 2009 Don't think I would exit the loop, rather continueloop, might be a lf or cr in between other lines.Well, the OP's original function appeared to be just to capture a single input line from the console.So looking for a lf/cr seems the most logical to indicate end of that single line, no?Agreed that multi-line input would call for a different approach to signal end of input, like the old CTRL-Z ("^Z") from "Copy CON" days. But again, for single line input isn't exiting the loop most appropriate? Link to comment Share on other sites More sharing options...
Kealper Posted January 4, 2010 Share Posted January 4, 2010 wow just found this topic, very very cool, thanks alot! but i've got a question...i've been fooling around with some things to no avail, any thoughts on how do clear all data from the console (like CLS for batch), and i've seen programs like AlacrityPC use different text colors for different lines of text in it's console (like an error or warning might be a yellow text, while normal is light gray, and debug info is dark gray)...any ideas how they did that? lol Link to comment Share on other sites More sharing options...
ResNullius Posted January 8, 2010 Share Posted January 8, 2010 wow just found this topic, very very cool, thanks alot!but i've got a question...i've been fooling around with some things to no avail, any thoughts on how do clear all data from the console (like CLS for batch), and i've seen programs like AlacrityPC use different text colors for different lines of text in it's console (like an error or warning might be a yellow text, while normal is light gray, and debug info is dark gray)...any ideas how they did that? lolCheck out this thread for some ideas on overwriting console output and a coloured text example in Post # 9 by The Kandie ManAlso, for clearing the entire console see rover's work here:http://www.autoitscript.com/forum/index.php?s=&showtopic=92049&view=findpost&p=662228or monoceres's here:http://www.autoitscript.com/forum/index.php?s=&showtopic=91557&view=findpost&p=659264 Link to comment Share on other sites More sharing options...
cherdeg Posted March 19, 2010 Share Posted March 19, 2010 BTW: none of the variants here does exitloop for me on pressing ENTER. Link to comment Share on other sites More sharing options...
ResNullius Posted March 24, 2010 Share Posted March 24, 2010 BTW: none of the variants here does exitloop for me on pressing ENTER.Since AutoIt has dropped support for opening files in RAW mode, the method for doing this now involves _WinApi functions.See #761205 for an updated version. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now