Convert an arguments-script into a stand-alone one?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Convert an arguments-script into a stand-alone one?

#1 Post by ©opy[it]®ight » 10 Oct 2013 09:31

Greetings everyone,

I was looking for a script to do some RegEx string searching and found the following one at:

- https://groups.google.com/forum/#!msg/a ... jasiLNBGUJ

Code: Select all

@echo off
  set recurse=


  if "%~1%~2"=="%~1" goto :EOF
  if "%~3"=="" (set count=0 & %0 %1 %2 %2)
  for /f "delims=" %%f in ('findstr /m "%~2" "%1"') do (

    for /f %%a in ('type "%%f" ^| findstr "%~2"') do (

      set /a count +=1
    )

    set recurse=%0 %1 "%~2.*%~3" "%~3"
  )
  %recurse%
  echo %count%


but unfortunately the script only works using command-line arguments.

My aim here is to use the code in a stand-alone script where it doesn't rely on (external) command-line arguments, but simply reads a static value or from variables inside the FOR loop.
I've tried to do this myself by changing the FINDSTR values and related ones elsewhere in the script, but without luck (script didn't output anything anymore) =/

Hopefully someone here is willing to help me out.

Thanks a lot in advance! :)

Kind regards,
- ©opy[it]®ight


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Convert an arguments-script into a stand-alone one?

#3 Post by aGerman » 12 Oct 2013 05:53

You need the recursion since the pattern has to be extended until multiple occurrences of the search string can't be found anymore. That means you have to run a loop if you don't wanna call an external script multiple times.

Code: Select all

@echo off
call :count *.txt xyz
echo found xyz %errorlevel% times
pause
goto :eof

:count
if not defined __FILE__ if not defined __PATTERN__ (
  setlocal DisableDelayedExpansion
  set "__FILE__=%~1"
  set "__PATTERN__=%~2"
  set "count=0"
)
set /a "test=count"
for /f "delims=" %%f in ('findstr /m "%__PATTERN__%" "%__FILE__%"') do (
  for /f %%a in ('type "%%f" ^| findstr "%__PATTERN__%"') do set /a "count += 1"
)
if %test%==%count% endlocal &exit /b %count%
set "__PATTERN__=%__PATTERN__%.*%~2"
goto count

Regards
aGerman

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Convert an arguments-script into a stand-alone one?

#4 Post by ©opy[it]®ight » 19 Oct 2013 14:11

That did the job for me!
Thanks a lot aGerman! :) 8)

ps: i could've responded sooner, but i was playing around with the script/code and tried to modify it further to make it smaller, but i don't think that's possible =P
Doing the same (or more) with less code is cool but can be tricky (e'specially for batch-noob such as me).

I'm always into learning (new) things, but sometimes its too abstract/obfuscated for me to understand =^]

Again thanks for helping me out, i appreciate this a lot! ;)

Gruß,
- ©opy[it]®ight

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Convert an arguments-script into a stand-alone one?

#5 Post by ©opy[it]®ight » 24 Oct 2013 12:42

One more question: Is it (also) possible to search for strings that are enclosed with double quotes?
I didn't knew it wouldn't take these into account otherwise i would've mentioned it in the first place ;)

Thanks

edit: I've found a solution by doing "\"%__PATTERN__%\"" but i'm not sure whether this has to be repeated for all findstr occurences (the below part seems to suffice)

Code: Select all

for /f "delims=" %%f in ('findstr /m "%__PATTERN__%" "%__FILE__%"') do (
  for /f %%a in ('type "%%f" ^| findstr "\"%__PATTERN__%\""') do set /a "count += 1"


I first tried solving it by changing all %~n into %n (n = number) but that didn't work.

Hopefully the above snippet is correct :)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Convert an arguments-script into a stand-alone one?

#6 Post by aGerman » 24 Oct 2013 14:42

Basically you found the right way how to escape quotation marks in a FINDSTR pattern.
Try

Code: Select all

@echo off
call :count *.txt \"xyz\"
echo found xyz %errorlevel% times
pause
goto :eof

:count
if not defined __FILE__ if not defined __PATTERN__ (
  setlocal DisableDelayedExpansion
  set "__FILE__=%~1"
  set "__PATTERN__=%~2"
  set "count=0"
)
set /a "test=count"
for /f "delims=" %%f in ('findstr /mrc:"%__PATTERN__%"^ "%__FILE__%"') do (
  for /f %%a in ('type "%%f" ^| findstr /rc:"%__PATTERN__%"') do set /a "count += 1"
)
if %test%==%count% endlocal &exit /b %count%
set "__PATTERN__=%__PATTERN__%.*%~2"
goto count

Regards
aGerman

Post Reply