Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
ralfs_k
- Posts: 6
- Joined: 24 Feb 2012 04:17
#1
Post
by ralfs_k » 24 Feb 2012 04:23
hi I need to create batch file that searches in text file following string:
but this doesn't work!
Code: Select all
setlocal
set LF=^
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
findstr /r /i "[0-9]*!LF!;!LF!;!LF!;!LF!;" < %%a > nul
if not errorlevel 1 echo delete >> %%a
)
pause > nul
any ideas?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 24 Feb 2012 06:09
Try this. The upper section is file.txt used by the batch file in a simple test.
Code: Select all
a
b
c
012345;
;
;
;
d
e
f
012345;
;
;
;
012345;
;
;
;
012345;
;
;
;
Code: Select all
@echo off
set a=
setlocal EnableExtensions EnableDelayedExpansion
for /f "eol=! delims=" %%a in (file.txt) do (
echo %%a
if defined a (
if "%%a"==";" (set /a num=!num!+1) else (set a=)
if !num! EQU 3 (
echo found line sequence
set num=
set a=
)
)
if "%%a"=="012345;" set a=1
)
pause
-
dbenham
- Expert
- Posts: 2461
- Joined: 12 Feb 2011 21:02
- Location: United States (east coast)
#4
Post
by dbenham » 24 Feb 2012 06:24
@ralfs_k - It failed because you didn't account for the <CR> characters in the Windows style line terminator. A regex of "<CR>*<LF>" will match both unix and Windows style line terminators. Read the link that jeb posted for more info.
Dave Benham
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 24 Feb 2012 08:30
Your search parameter also has another fundamental flaw.
You are searching for any amount of numbers, then a LF and then a semi colon but your example is number semicolon and then there would be a LF if your file is unix based.