Page 1 of 1

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

Posted: 10 Oct 2013 09:31
by ©opy[it]®ight
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

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

Posted: 12 Oct 2013 02:49
by ©opy[it]®ight
Bump :?

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

Posted: 12 Oct 2013 05:53
by aGerman
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

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

Posted: 19 Oct 2013 14:11
by ©opy[it]®ight
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

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

Posted: 24 Oct 2013 12:42
by ©opy[it]®ight
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 :)

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

Posted: 24 Oct 2013 14:42
by aGerman
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