|
Answer» When I type the more>dailylog.txt command, I can begin to type some words. After finishing a line, I press "enter" KEY once.(this is the first time to use control key.) And I want to finish composing, I press combination of "Ctrl" and "Z". (the 2nd time to use control key.) At last, I have to press "enter" AGAIN! (this is the third time to use control key!)
I am lazy and I just want to press ONLY one "enter" key to finish composing!
I tried to use a BATCH file to solve this problem, but not work properly...... :-?
My implementation:
echo off date/t >> dailylog.txt echo.> crlf.dat time > dailylog.txt more < crlf.dat >> dailylog.txt
I want to type in " Edit some files.", but I CANNOT input anything!
How can I redirect a string to a text file with ONLY pressing one enter? If "more" cannot do this, are there any other commands can achieve this requirement ? Thanks heaps!Well, I can remove the need for the ^Z -- this will keep reading lines of text until the line entered is blank, when it will stop looping
Note I havent tested it, but it should work -- in principle
Graham
Code: [Select]echo off date/t >> dailylog.txt echo.> crlf.dat time <crlf.dat | find "curr" >> dailylog.txt
:GetLine SET /P Line= If "%Line%"=="" GoTo EndLoop >> dailylog.txt Echo %Line% GoTo GetLine :EndLoop
:: rest of batch
Code: [Select]echo off date/t >> dailylog.txt echo.> crlf.dat time <crlf.dat | find "curr" >> dailylog.txt
:GetLine Set /P Line= If "%Line%"=="" GoTo EndLoop >> dailylog.txt Echo %Line% GoTo GetLine :EndLoop
:: rest of batch
Thanks Graham. I have tested your code, but it seems the condition "%Line%"=="" never return true because I have to exit the loop by using Ctrl-C. Can you make any improvements for this code?Try this, slightly amended version, it works
Graham
Code: [Select]echo off date/t >> dailylog.txt echo.> crlf.dat time <crlf.dat | find "curr" >> dailylog.txt
:GetLine Set Line= Set /P Line= If "%Line%"=="" GoTo EndLoop >> dailylog.txt Echo %Line% GoTo GetLine :EndLoop
:: rest of batch
Thanks Graham, it does work now with only pressing one more enter key.
Keep it simple Dudes!
[edit]ECHO OFF SET Line=%date% %time% :GetLine ECHO.%Line%>>dailylog.txt SET /p Line=&&GOTO:GetLine [/edit]DosItHelp - not bad, except you omitted the initialising of the Line variable and the exit if the line is blank after the input Graham
Quote Keep it simple Dudes!
[edit] :GetLine ECHO.%Line%>>dailylog.txt SET /p Line=&&GOTO:GetLine [/edit]
Wooooo , so simple, but so DIFFICULT. I cannot understand this two statements. :-?
so many questions for me? What does ECHO.%Line% mean? What does && mean? Why when I give a initial assignment "set Line=", this code does not work properly? Why when I move GOTO:GetLine to the next line, this code does not work?
Can you give me some explanation on how it works? Thank you very much! Sure, let's go into detail:
ECHO OFF SET Line=%date% %time%
... the Line variable is now initialized with a string containing the current date and time.
:GetLine ECHO.%Line%>>dailylog.txt
... the first time coming here will append the date and time to the log file, looping back to here will append the new text entered by the user.
SET /p Line=&&GOTO:GetLine
... this will query the new Line to be added to the log file. The GOTO will only be executed when some text was entered. If no text was entered then the GOTO will not be executed, and the loop terminates.
Note that there is difference between single & and a double &&. Single & will concatenate commands whereas double && causes conditional execution so that the command following the double && will only execute when the command preceeding the double && succeeded. The set /a command only "succeeds" when text has been entered by the user. See also http://www.dostips.com/DtCodeSnippets.php#_Toc141112827
Hope this also eliminates jpl's concern.
QuoteThe GOTO will only be executed when some text was entered
You LIVE and learn; I never thought to try conditional execution .. thank you GrahamThank you sooo much for details, DosItHelp. I much appreciate your effort and time. Thanks again.
|