|
Answer» Windows XP SP.2
Is it possible to wrap a string in a FOR loop command line so that the entire string is visible in Edit.com - my editor of choice. e.g.
Code: [Select]@echo off cls
set z=1 echo. & echo. & echo. & echo. & echo. & echo.
:rerun
FOR /F "tokens=%z%" %%D in ("This is a very very long string which will extend past the screen boundary.") do ( echo %%D )
set /a z=%z%+1 if %z% gtr 14 goto end goto rerun
:end
In the above code the string extends past the 80 column RH side of the screen, can it be wrapped somehow?
ThanksA caret can be used as line continuation marker as long as there aren't any quotes around the string
Thus
echo Mary had a little lamb: its fleece was white as snow, and everywhere that Mary went, the lamb was sure to go.
may be REPLACED by
echo Mary had a little lamb: its fleece ^ was white as snow, and ^ everywhere that Mary went, ^ the lamb was sure to go.
This can happen either at the prompt (I pasted the above 4 lines as a group into a command window; note the "more" prompt - this appears also if you type a single line with a caret at the end)
Code: [Select]C:\>echo Mary had a little lamb: its fleece ^ More? was white as snow, and ^ More? everywhere that Mary went, ^ More? the lamb was sure to go. Mary had a little lamb: its fleece was white as snow, and everywhere that Mary went, the lamb was sure to go. or in a batch file. The problem with a FOR loop lies in the need to quote the string, because the beginning quote will render the caret inoperative. You could always assign the string beforehand to a variable with a nice short name and use that in the FOR line. I actually do this sometimes to make my code more readable/editable. The following code includes examples of what I mentioned above.
Splitline.bat:
Code: [Select]@echo off
cls
echo Mary had a little lamb: its fleece ^ was white as snow, and ^ everywhere that Mary went, ^ the lamb was sure to go.
set z=1
REM you did know you don't actually need the spaces here? echo.&echo.&echo.&echo.&echo.&echo.
REM and you may be interested to know about doing the above REM this way. These 2 do the same thing & look nicer I think, REM and are less of a pain to modify.
REM for %%N in (1 2 3 4 5 6) do echo. REM for /L %%N in (1,1,6) do echo.
:rerun
REM this is one way to break a string REM Dunno if this will do exactly what you wanted REM but it seems to be functionally equivalent to REM what you posted
set string=This is a very very long ^ string which will extend ^ past the screen boundary.
FOR /F "tokens=%z%" %%D in ("%string%") do ( echo %%D )
set /a z=%z%+1 if %z% gtr 14 goto end goto rerun
:end Output:
Code: [Select] C:\>Splitline.bat Mary had a little lamb: its fleece was white as snow, and everywhere that Mary went, the lamb was sure to go.
This is a very very long string which will extend past the screen boundary.
As usual Dias you are awesome. I had forgot about the caret trick, and I did not know of the "echo.&echo.&echo" trick. Quote from: Dias de verano on April 05, 2008, 02:40:55 AM A caret can be used as line continuation marker as long as there aren't any quotes around the string. Code: [Select]REM you did know you don't actually need the spaces here? echo.&echo.&echo.&echo.&echo.&echo. I've already edited one of my batchfiles with the "echo.&echo.&echo...", greatly reducing the number of lines in the file. I will clean up some others soon.
I have another batch file with the long line problem that I am going to edit with your suggestion to set a variable to equal the long expression, then replace the expression with the short VAR name. AWESOME!!!
Thanks Again!!! the original poster used the echo. & echo. & echo. [etc] thing. I merely pointed out that the spaces were not needed, and further pointed out that this is even better
Code: [Select]for /L %%N in (1,1,6) do echo. Of course you can replace the number 6 with the number of BLANK lines you want to echo.
Sorry, I missed your second suggestion with the for command. QuoteCode: [Select]for /L %%N in (1,1,6) do echo.
That is great for anyone who is familiar with for, but for those of us who are still struggling, the "echo.&echo. ..." may be a bit easier. That doesn't mean I don't appreciate it. I saved the whole thing in a text file to explore more later.
Thanks.Of course you can save lines by joining any commands with & signs
cls&echo %date%&cd C:\&dir
Dias - thank you. I knew about the caret extension but not how to apply it in a bat script. Like others I am amazed, and in awe, at your depth of knowledge.
Thanks again for the lengthy explanation clarifying more than the question I asked..
Thank you for your kind words, which I do not deserve: most of what you guys think I "know" about batch scripting I picked up from Google searches and in particular from lurking in, and SEARCHING in the very valuable Usenet groups alt.msdos.batch.nt and and alt.msdos.batch
Useful searchable Google Groups archives of these groups:
http://groups.google.com/group/alt.msdos.batch.nt/topics?hl=en&lnk=gschg
http://groups.google.com/group/alt.msdos.batch/topics?hl=en&lnk=gschg
Just about every question you can think of has been asked and answered in them.
|