recursiv string concatenation in batch script

May 7, 2010 at 08:51:43
Specs: Windows XP
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: C

my output should be
ABC

can anyone help me with this


See More: recursiv string concatenation in batch script

Report •

#1
May 7, 2010 at 09:47:35
It may exceed the var length limit, depending on the file.

=============================
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set S=!S!%%a
)
>> newfile echo.!S!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
May 7, 2010 at 14:35:49
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


Report •

#3
May 8, 2010 at 12:19:18
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 felicity

M2


Report •

Related Solutions

#4
May 9, 2010 at 06:11:59
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.

Report •

#5
May 9, 2010 at 06:41:58
R2,

It's gonna take a few beers to get traction on this one.


=====================================
Helping others achieve escape felicity

M2


Report •

#6
May 9, 2010 at 07:03:38
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.


Report •

#7
May 9, 2010 at 19:14:47
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.



Report •

#8
May 9, 2010 at 22:03:19
Hi everyone,
Thanks for the replies,Can anyone provide some link where i will
be able to learn batch scripting

Report •

#9
May 10, 2010 at 00:48:53
Hi klint,

very smooth


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question