i need to read a file line by line using batch
scripting
and concate each line
and save the output.in a file if i have
Line1: A
Line2: B
Line3: Cmy output should be
ABCcan anyone help me with this
It may exceed the var length limit, depending on the file. =============================
@echo off > newfile & setLocal enableDELAYedeXpansionfor /f "tokens=* delims= " %%a in (myfile) do (
set S=!S!%%a
)
>> newfile echo.!S!
=====================================
Helping others achieve escape felicityM2
And if variable length limit might be a problem, here's a simpler version that doesn't use environment variables: @echo off
(for /f "delims=" %%a in (myfile) do set/p=%%a) <nul >newfile
Hi klint, You really got me now. [As the song says.]
Clearly, it does work, but I have no idea how.
=====================================
Helping others achieve escape felicityM2
Clearly, it does work, but I have no idea how.
He's using the behavior of SET /P to echo what you want, without adding a '\n' to the end. He's also piping in NUL to keep things moving.
R2, It's gonna take a few beers to get traction on this one.
=====================================
Helping others achieve escape felicityM2
Hi M2, Here's a little practical exercise to demonstrate the trick for outputting a string without adding a newline at the end.
Step 1: lubricate with a couple of beers. (They don't actually help, but they make you think they help. That boosts confidence ;-)
Step 2: See how the normal way of outputting a string also outputs a newline after it, which is not what we want:
echo hello
Step 3: The SET /P command outputs a string as a prompt. It stays on the same line.
SET /P var=Enter something:
Step 4. We don't want to pause for the user to enter anything, so we get our input from NUL:
SET /P var=Enter something:<NUL
Step 5: An undocumented feature is that the SET /P command doesn't need a variable name! So you can do this:
SET /P =Some string<NUL
and it just outputs a string without a newline.
Now, putting it together, we have a loop that reads each line of the file and outputs it without a newline. By surrounding the whole loop in parentheses, thus:
(FOR ... DO (...) ) <in.txt >out.txt
we can redirect input and output just once for the whole loop. You can also redirect inside the loop, but that opens and closes the file each time through the loop, which is inefficient. By surrounding the whole loop in parentheses and redirecting outside the loop, we open the file just once.
It is a great trick but with just about everything in batch it has a shortcoming, it won't accept a string that starts with "=" (on xp at very least...) C:\>set /p ==string<nul The syntax of the command is incorrect. C:\>set /p =^=string<nul The syntax of the command is incorrect. C:\>for %a in ("=string") do set /p =%~a<nul C:\>set /p ==string 0<nul The syntax of the command is incorrect. C:\>cmd /v:on /c "set test==string& set /p =!test!<nul" The syntax of the command is incorrect.
Hi everyone,
Thanks for the replies,Can anyone provide some link where i will
be able to learn batch scripting
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |