My immediate need is to ensure that each and every line, including empty blank lines, is extracted by SET /P
For initial testing I simply write each line to the screen for instant debug.
So far I have found 25 ways of using FIND or FINDSTR,
but only 3 of them are valid - the other 22 are defective and fail when confronted by blank lines
I would appreciate suggestions of any other possibilities that merit testing.
After which I will evaluate which is most suitable for my needs.
I started with from code on this site
viewtopic.php?f=3&t=2128&hilit=multiple+lines+in+variable&start=0
That topic included a statement by "dbenham" :-
"This was much more complicated than I thought it would be. Results are NOT as expected."
My understanding is so limited that no result would surprise me
It was such an obvious assumption that ('find /n "3" %2') was responsible for both search/selection of text and glitches at blank lines,
that I chose not to assume but to test with TWO text files invoked with %1 and %2,
each with five non-blank and unique lines and one blank line but at different positions.
Code: Select all
<%1 (
for /f %%i in ('find /n "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
File3.txt has the gap between lines 1 and 2,
and File4.txt has the gap between lines 4 and 6.
The output to screen should be 6 lines, and some may be blank,
BUT the file text extraction has failed if the 6th line does not announce
"???? line 6 of 6 ????"
Three ways to make it work :-
Code: Select all
:_D
<%1 (
for /f %%i in ('findstr /n "^" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
:_M
<%1 (
for /f %%i in ('type %2 ^| find /n /v ""') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
:_S
<%1 (
for /f %%i in ('find /n /v "" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
The first of the three above methods, :_D, produced this
E:\T\CCleaner\New># D
--- starting --- --- starting --- --- starting ---
:*********** 30/11/2011 16:16:19.06 *************
MODE = _D. Files :- OUTER = File3.TXT ; INNER = File3.TXT.
:========== findstr /n "^" File3.TXT =======================
file 3 line 1 of 6 but missing line 2 ####
file 3 line 3 of 6 but missing line 2 ####
file 3 line 4 of 6 but missing line 2 ####
file 3 line 5 of 6 but missing line 2 ####
file 3 line 6 of 6 but missing line 2 ####
SUCCESS ---MODE = _D : File3.TXT File3.TXT PROCESSED 6 LINES
:*********** 30/11/2011 16:16:19.10 *************
MODE = _D. Files :- OUTER = File3.TXT ; INNER = File4.TXT.
:========== findstr /n "^" File4.TXT =======================
file 3 line 1 of 6 but missing line 2 ####
file 3 line 3 of 6 but missing line 2 ####
file 3 line 4 of 6 but missing line 2 ####
file 3 line 5 of 6 but missing line 2 ####
file 3 line 6 of 6 but missing line 2 ####
SUCCESS ---MODE = _D : File3.TXT File4.TXT PROCESSED 6 LINES
:*********** 30/11/2011 16:16:19.15 *************
MODE = _D. Files :- OUTER = File4.TXT ; INNER = File3.TXT.
:========== findstr /n "^" File3.TXT =======================
file 4 line 1 of 6 but missing line 4 @ @@@@@
file 4 line 2 of 6 but missing line 4 @ @@@@@
file 4 line 3 of 6 but missing line 4 @ @@@@@
file 4 line 5 of 6 but missing line 4 @ @@@@@
file 4 line 6 of 6 but missing line 4 @ @@@@@
SUCCESS ---MODE = _D : File4.TXT File3.TXT PROCESSED 6 LINES
:*********** 30/11/2011 16:16:19.20 *************
MODE = _D. Files :- OUTER = File4.TXT ; INNER = File4.TXT.
:========== findstr /n "^" File4.TXT =======================
file 4 line 1 of 6 but missing line 4 @ @@@@@
file 4 line 2 of 6 but missing line 4 @ @@@@@
file 4 line 3 of 6 but missing line 4 @ @@@@@
file 4 line 5 of 6 but missing line 4 @ @@@@@
file 4 line 6 of 6 but missing line 4 @ @@@@@
SUCCESS ---MODE = _D : File4.TXT File4.TXT PROCESSED 6 LINES
ALL TESTS DONE
It is of interest that the above Mode :_D extracts text from "OUTER" file "%<1",
and NOT from INNER" file %2 which is the apparent target of the FINDSTR search string.
The other two methods produced similar results.
A sorted list of 25 code portions - Good and Bad :-
for /f %%i in ('find "3" %2') do ( Defective
for /f %%i in ('find "3" ^< %2') do ( Defective
for /f %%i in ('find /n "3" %2') do ( Defective
for /f %%i in ('find /n "3" ^< %2') do ( Defective
for /f %%i in ('find /n /V "" %2') do ( Defective
for /f %%i in ('find /n /v "" ^< %2') do ( SUCCESSFULL ****
for /f %%i in ('find /n /V "3" %2') do ( Defective
for /f %%i in ('find /n /v "3" ^< %2') do ( Defective
for /f %%i in ('find /v /N "3" %2') do ( Defective
for /f %%i in ('findstr "3" %2') do ( Defective
for /f %%i in ('findstr "3" ^< %2') do ( Defective
for /f %%i in ('findstr /n "^" %2') do ( SUCCESSFULL ****
for /f %%i in ('findstr /n "3" %2') do ( Defective
for /f %%i in ('findstr /n "3" ^< %2') do ( Defective
for /f %%i in ('findstr /n /v "^" ^< %2') do ( Defective
for /f %%i in ('findstr /n /v "3" ^< %2') do ( Defective
for /f %%i in ('findstr /V "3" %2') do ( Defective
for /f %%i in ('type %2 ^| find "3" ') do ( Defective
for /f %%i in ('type %2 ^| find /n "3"') do ( Defective
for /f %%i in ('type %2 ^| find /n /v ""') do ( SUCCESSFULL ****
for /f %%i in ('type %2 ^| find /n /v "3"') do ( Defective
for /f %%i in ('type %2 ^| findstr "3" ') do ( Defective
for /f %%i in ('type %2 ^| findstr /n "3"') do ( Defective
for /f %%i in ('type %2 ^| findstr /n /v "^"') do ( Defective
for /f %%i in ('type %2 ^| findstr /n /v "3"') do ( Defective
N.B. The Good portions are SUCCESSFUL in producing an output of 6 lines.
The text files are
File3.txt
Code: Select all
file 3 line 1 of 6 but missing line 2 ####
file 3 line 3 of 6 but missing line 2 ####
file 3 line 4 of 6 but missing line 2 ####
file 3 line 5 of 6 but missing line 2 ####
file 3 line 6 of 6 but missing line 2 ####
File4.txt
Code: Select all
file 4 line 1 of 6 but missing line 4 @ @@@@@
file 4 line 2 of 6 but missing line 4 @ @@@@@
file 4 line 3 of 6 but missing line 4 @ @@@@@
file 4 line 5 of 6 but missing line 4 @ @@@@@
file 4 line 6 of 6 but missing line 4 @ @@@@@
The Principle script is named #.bat,
'#' chosen for launch convenient proximity to the Return Key.
Regardless of any arguments,
this will run four times any chosen find/findstr/etc
using each permutation of Text3.txt and/or text4.txt
#.bat With no arguments compares how "type" and "findstr" give different results.
Arguments A or B or C show other comparisons of FIND, FINDSTR, etc.
The above "sorted list of 25 code portions - Good and Bad" are taken from
Arguments D through to Z plus Z1 plus Z2
An executive script is ##.BAT
##.bat With no arguments will run #.bat for all modes D through to Z2
"##.BAT ALL" runs #.bat for all modes A through to Z2
"##.BAT VALID" runs #.bat for the "good" Modes D, M, and S, which deliver 6 lines of output.
Please note that I launched CMD.EXE and adjusted "properties from my normal screen Buffer size height of 1000 lines to 1100,
After which I could run "##.BAT ALL" and then scroll back and the start was still visible, and the whole buffer could be copy / pasted.
Supervisory script ##.Bat
Code: Select all
@echo off & setlocal EnableDelayedExpansion
CLS
ECHO( > MSG.TXT
IF /I "%1"=="ALL" GOTO ALL
IF /I "%1"=="VALID" GOTO VALID
FOR %%Z IN (D E F G H I J K L M N O P Q R S T U V W X Y Z Z1 Z2) DO CALL # %%Z
GOTO DONE
:VALID
FOR %%Z IN (D M S) DO CALL # %%Z
GOTO DONE
:ALL
FOR %%Z IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Z1 Z2) DO CALL # %%Z
:DONE
TYPE MSG.TXT
ECHO( HAVE A NICE DAY - HIT ANY KEY TO CLOSE
PAUSE
Principle Script #.Bat
Code: Select all
@echo off & setlocal EnableDelayedExpansion & ECHO(
echo --- starting --- --- starting --- --- starting --- & ECHO(
CALL :TESTX File3.TXT File3.TXT _%1
CALL :TESTX File3.TXT File4.TXT _%1
CALL :TESTX File4.TXT File3.TXT _%1
CALL :TESTX File4.TXT File4.TXT _%1
ECHO ALL TESTS DONE
:PAUSE
GOTO :EOF
:TESTX
echo :*********** %DATE% %TIME% *************
ECHO MODE = %3. Files :- OUTER = %1 ; INNER = %2.
SET file=%1
FOR %%Z IN (A B C D E F G H I J K L M O P Q R S T U V W X Y) DO IF /I "%3"=="_%%Z" (
CALL :_%%Z %1 %2 %3
FOR /F "tokens=4" %%A IN ("!ln!") DO IF "%%A"=="6" (
SET MSG=SUCCESS ---MODE = %3 : %1 %2 PROCESSED %%A LINES
ECHO !MSG! >> MSG.TXT & ECHO !MSG!
)
GOTO :EOF
)
echo :====A======= type ==================================
<%1 (
for /f %%i in ('type %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
echo :=====B==== findstr /n "^" %2 =======================
<%1 (
for /f %%i in ('findstr /n "^" %2') do (
set "ln=" & set /p "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_A
echo :============ FIND "3" ==============================
<%1 (
for /f %%i in ('find /N "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
:_Z1
echo :============ FIND /v "3" ===========================
<%1 (
for /f %%i in ('find /v /N "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_B
echo :========== findstr "3" %2 =======================
<%1 (
for /f %%i in ('findstr "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
echo :========== findstr /n "3" %2 =======================
<%1 (
for /f %%i in ('findstr /n "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_C
echo :============ findstr "3" ============================
<%1 (
for /f %%i in ('findstr "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
:_Z2
echo :============= find "3" =============================
<%1 (
for /f %%i in ('find "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_D
echo :========== findstr /n "^" %2 =======================
<%1 (
for /f %%i in ('findstr /n "^" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_E
echo :========== findstr /n "3" %2 =======================
<%1 (
for /f %%i in ('findstr /n "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_F
echo :============ findstr "3" %2 ========================
<%1 (
for /f %%i in ('findstr "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_G
echo :========== find /n /V "" %2 =======================
<%1 (
for /f %%i in ('find /n /V "" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_H
echo :========== find /n /V "3" %2 =======================
<%1 (
for /f %%i in ('find /n /V "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_I
echo :============ find /n "3" %2 =======================
<%1 (
for /f %%i in ('find /n "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_J
echo :==========="('type %2 ^| findstr /n /v "^"')"==========
<%1 (
for /f %%i in ('type %2 ^| findstr /n /v "^"') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_K
echo :==========="('type %2 ^| findstr /n /v "3"')"==========
<%1 (
for /f %%i in ('type %2 ^| findstr /n /v "3"') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_L
echo :==========="('type %2 ^| findstr /n "3"')"==========
<%1 (
for /f %%i in ('type %2 ^| findstr /n "3"') do (
set "ln=" & set /p "ln=" & set "LINE=result = !ln!" & echo(!ln!
)
)
GOTO :EOF
:_M
echo :==========="('type %2 ^| find /n /v ""')"==========
<%1 (
for /f %%i in ('type %2 ^| find /n /v ""') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_N
echo :==========="('type %2 ^| find /n /v "3"')"==========
<%1 (
for /f %%i in ('type %2 ^| find /n /v "3"') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_o
echo :==========="('type %2 ^| find /n "3"')"==========
<%1 (
for /f %%i in ('type %2 ^| find /n "3"') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_P
echo :==========="('findstr /n /v "^" ^< %2')"==========
<%1 (
for /f %%i in ('findstr /n /v "^" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_Q
echo :==========="('findstr /n /v "3" ^< %2')"==========
<%1 (
for /f %%i in ('findstr /n /v "3" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_R
echo :==========="('findstr /n "3" ^< %2')"==========
<%1 (
for /f %%i in ('findstr /n "3" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_S
echo :==========="('find /n /v "" ^< %2')"==========
<%1 (
for /f %%i in ('find /n /v "" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_T
echo :==========="('find /n /v "3" ^< %2')"==========
<%1 (
for /f %%i in ('find /n /v "3" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_U
echo :==========="('find /n "3" ^< %2')"==========
<%1 (
for /f %%i in ('find /n "3" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_V
<%1 (
for /f %%i in ('find "3" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_W
<%1 (
for /f %%i in ('findstr "3" ^< %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_X
echo :=====B====== findstr /V "3" Inner File ==============
<%1 (
for /f %%i in ('findstr /V "3" %2') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
echo :=====================================================
GOTO :EOF
:_Y
<%1 (
for /f %%i in ('type %2 ^| find "3" ') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
:_Z
<%1 (
for /f %%i in ('type %2 ^| findstr "3" ') do (
set "ln=" & set /p "ln=" & echo(!ln!
)
)
GOTO :EOF
Regards
Alan