Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#1
Post
by tinfanide » 13 May 2012 05:40
Code: Select all
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
< 6.txt (
FOR /F "delims=" %%A IN (5.txt) DO (
SET str=
SET /P str=
ECHO %%A - !str!
)
)
PAUSE
5.txt
a
b
c
6.txt
a
d
e
a - a
b -
c - d
Press any key to continue . . .
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 13 May 2012 05:47
Because that's the way FOR /F works
Try:
Code: Select all
...
FOR /F "tokens=1* delims=:" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
ECHO %%B - !str!
...
Regards
aGerman
EDIT:
To avoid faults with leading colons:
Code: Select all
...
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
ECHO %%A - !str:*:=!
...
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#4
Post
by tinfanide » 14 May 2012 06:15
aGerman wrote:Because that's the way FOR /F works
Try:
Code: Select all
...
FOR /F "tokens=1* delims=:" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
ECHO %%B - !str!
...
Regards
aGerman
EDIT:
To avoid faults with leading colons:
Code: Select all
...
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
...
ECHO %%A - !str:*:=!
...
Code: Select all
< 6.txt (
FOR /F "tokens=1* delims=:" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
SET str=
SET /P str=
ECHO %%B - !str!
)
)
Code: Select all
< 6.txt (
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
SET str=
SET /P str=
ECHO %%A - !str:*:=!
)
)
The first version works better than the second one because
the second one produces
first:
a - a
-
b - d
c - e
second
1:a - a
2: - *:=
3:b - d
4:c - e
It seems that
does not substitute the string "*:" for "".
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#5
Post
by aGerman » 14 May 2012 09:49
My fault.
Code: Select all
@ECHO OFF
< 6.txt (
FOR /F "delims=" %%A IN ('FINDSTR /N "^" "5.txt"') DO (
SET str=
SET /P str=
SET "str2=%%A"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !str2:*:=! - !str!
ENDLOCAL
)
)
PAUSE
Regards
aGerman