Using RegEx on a pair of lines at same time?
Posted: 09 Feb 2021 22:26
I have a .BAT that reports every found "a.txt" and directory names via something like
dir /a /od /s *.* | FINDSTR /I /R /C:"a.txt" /C:" Directory of "
(As you maybe guessed I'm using this construct to avoid "false positives" on short file names. It's actually more complex than this but this will suffice for my question. I actually have %1 et al logic instead of a.txt, and /R actually does get used.)
Unfortunately since I know no DIR form that shows paths I ask for both filespec matching lines and every DIR line.
Output is like
Can I exclude - or even just detect - consecutive lines that begin with { Directory of } ? Goal is to eliminate the first of each pair. So rid the C:\, \AL, \CAL lines but preserve the \BOB line.
Just to attempt to identify them (excluding is another matter!) I tried appending a pipe
| findstr /R /C:"^ Dir.*$^ Dir"
seeking to via RegEx report lines beginning with { Dir} that immediately after their EOL (the $) have a BOL (the ^) and { Dir}. As expected it fails even without the seemingly redundant ^.
More basically, are there tricks available with two lines, such that you can seek a line with something, then "peek forward" for something on the BOL of the next line? If I must concede that FINDSTR only works on one line at a time, any other approach? Thx. Win 10, Win 7, XP.
(If not, my grotesque fallback is to redirect to a file and run a trivial VBA macro in Excel who is always up. For this "project" my only programming avenues are batch files and VBA.)
dir /a /od /s *.* | FINDSTR /I /R /C:"a.txt" /C:" Directory of "
(As you maybe guessed I'm using this construct to avoid "false positives" on short file names. It's actually more complex than this but this will suffice for my question. I actually have %1 et al logic instead of a.txt, and /R actually does get used.)
Unfortunately since I know no DIR form that shows paths I ask for both filespec matching lines and every DIR line.
Output is like
Code: Select all
Directory of C:\
Directory of C:\AL
Directory of C:\BOB
06/30/2017 03:37 AM 730 a.txt
Directory of C:\CAL
Directory of C:\DOG
Just to attempt to identify them (excluding is another matter!) I tried appending a pipe
| findstr /R /C:"^ Dir.*$^ Dir"
seeking to via RegEx report lines beginning with { Dir} that immediately after their EOL (the $) have a BOL (the ^) and { Dir}. As expected it fails even without the seemingly redundant ^.
More basically, are there tricks available with two lines, such that you can seek a line with something, then "peek forward" for something on the BOL of the next line? If I must concede that FINDSTR only works on one line at a time, any other approach? Thx. Win 10, Win 7, XP.
(If not, my grotesque fallback is to redirect to a file and run a trivial VBA macro in Excel who is always up. For this "project" my only programming avenues are batch files and VBA.)