Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Cat
- Posts: 32
- Joined: 11 Nov 2011 12:04
#1
Post
by Cat » 14 Feb 2012 22:24
I'm writing a code that will combine lines from two text files. The first text file contains:
The second one contains:
I want the output to be:
Any ideas how to make this work? My best attempt results with:
And so on like that. This uses two seperate FOR commands with synchronized tokens.
-
Aacini
- Expert
- Posts: 1885
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#2
Post
by Aacini » 15 Feb 2012 01:52
Code: Select all
@echo off
setlocal EnableDelayedExpansion
< file2.txt ( for /F "delims=" %%a in (file1.txt) do (
set file2Line=
set /P file2Line=
echo %%a !file2Line!
)
)
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 15 Feb 2012 02:45
That's neat Aacini
This modification does away with the need for delayed expansion.
Code: Select all
@echo off
setlocal
< file2.txt ( for /F "delims=" %%a in (file1.txt) do (
set file2Line=
set /P file2Line=
for /f "tokens=1* delims==" %%b in ('set file2Line') do (
echo %%a %%c
)
)
)
pause
Poison characters and all.
-
jeb
- Expert
- Posts: 1042
- Joined: 30 Aug 2007 08:05
- Location: Germany, Bochum
#4
Post
by jeb » 15 Feb 2012 03:01
But now file contents with equal signs are removed
file2.txt wrote:===1
=2
===3
As the "delims==" option split at the delim "=" and removes all subsequential delims
jeb
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#5
Post
by foxidrive » 15 Feb 2012 03:44
jeb wrote:But now file contents with equal signs are removed
file2.txt wrote:===1
=2
===3
As the "delims==" option split at the delim "=" and removes all subsequential delims
jeb
Only leading equals signs.
Code: Select all
A 1 & a !b! = =
B 2 ^c ===
C 3 %d ==
D 4 |e1 =
-
jeb
- Expert
- Posts: 1042
- Joined: 30 Aug 2007 08:05
- Location: Germany, Bochum
#6
Post
by jeb » 15 Feb 2012 08:43
Or you could use the delayed toggling technic to be always safe
Code: Select all
@echo off
setlocal DisableDelayedExpansion
< file2.txt (
for /F "delims=" %%a in (file1.txt) do (
set file2Line=
set /P file2Line=
set "file1Line=%%a"
setlocal EnableDelayedExpansion
echo(!file1Line! !file2Line!
)
)
jeb
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#8
Post
by Ed Dyreen » 15 Feb 2012 11:12
jeb wrote:Code: Select all
@echo off
setlocal DisableDelayedExpansion
< file2.txt (
for /F "delims=" %%a in (file1.txt) do (
set file2Line=
set /P file2Line=
set "file1Line=%%a"
setlocal EnableDelayedExpansion
echo(!file1Line! !file2Line!
endlocal
)
)
Jeb, you are mean, you should endlocal
, besides you are eating memory AND slowing down performance
-
jeb
- Expert
- Posts: 1042
- Joined: 30 Aug 2007 08:05
- Location: Germany, Bochum
#9
Post
by jeb » 15 Feb 2012 14:16
You are right.
I forgot one of the key elements of the toggling technic.
Without the endlocal it doesn't toggle and it would fail
I am so lazy