Page 1 of 1

FINDSTR: Print the found string, not the line.

Posted: 17 Jun 2009 01:56
by bodosko
Hello, I need some help here.

If I use this command:
for /F %%a in ('dir /b *.avi ^| findstr /i "S[0-9]*E[0-9]*"') do (echo %%a)
It will print the full filename.

But I just want it to print the found string, for example:
If one of the files is "Lost.S02E11.HDTV.avi" it would print the full filename.
But I want it just to print "S02E11".

I thought using "tokens=2 and delims=." But it can't be used, since some filenames have more or less "." before the "SnnEnn" part.

So how can I store only the string found(not the entire line) in a variable to use it later?
Or if there is a better solution for my problem please tell me.
Thanks!

Posted: 17 Jun 2009 10:23
by RElliott63
If the value you're looking for is ".Sxx" where xx could be numbers, then try this:

Code: Select all

For /F "delims=.S tokens=2" %%F in ('Dir /B *.avi') do (
   Echo Filename : %%F
)


This will give you the first token as the value of the first ".S" (if it always starts with ".S"

Posted: 17 Jun 2009 10:59
by bodosko
Didn't work.
It returned the string after the first "." not after the first ".S"

Posted: 18 Jun 2009 10:29
by avery_larry
I think you'd have to do a big loop by basically getting the result of the findstr command and then stripping the the leading characters one at a time until the findstr command fails (and then obviously adding that character back on) and then stripping the trailing characters one at a time until the findstr fails. Something like this (not tested):

Code: Select all

for /F %%a in ('dir /b *.avi ^| findstr /i "S[0-9]*E[0-9]*"') do (set "superstring=%%~a")

:leadloop
echo.%superstring:~1%|findstr /i "S[0-9]*E[0-9]*"
if not errorlevel 1 (
   set "superstring=%superstring:~1%"
   goto leadloop
)

:trailloop
echo.%superstring:~0,-1%|findstr /i "S[0-9]*E[0-9]*"
if not errorlevel 1 (
   set "superstring=%superstring:~0,-1%"
   goto trailloop
)
echo %superstring%

Re: FINDSTR: Print the found string, not the line.

Posted: 18 Jun 2009 21:44
by ghostmachine4
bodosko wrote:Hello, I need some help here.

If I use this command:
for /F %%a in ('dir /b *.avi ^| findstr /i "S[0-9]*E[0-9]*"') do (echo %%a)
It will print the full filename.

But I just want it to print the found string, for example:
If one of the files is "Lost.S02E11.HDTV.avi" it would print the full filename.
But I want it just to print "S02E11".


if you have GNU tools and Gawk for windows
eg

Code: Select all

@echo off
for /f %%a in ('dir /b *.avi ^| gawk -F"." "{print $2}"') do echo %%a

output

Code: Select all

C:\test>test.bat
S02E11


Posted: 28 Jun 2009 05:03
by traveller
avery_larry wrote:I think you'd have to do a big loop by basically getting the result of the findstr command and then stripping the the leading characters one at a time until the findstr command fails (and then obviously adding that character back on) and then stripping the trailing characters one at a time until the findstr fails. Something like this (not tested):

Code: Select all

for /F %%a in ('dir /b *.avi ^| findstr /i "S[0-9]*E[0-9]*"') do (set "superstring=%%~a")

:leadloop
echo.%superstring:~1%|findstr /i "S[0-9]*E[0-9]*"
if not errorlevel 1 (
   set "superstring=%superstring:~1%"
   goto leadloop
)

:trailloop
echo.%superstring:~0,-1%|findstr /i "S[0-9]*E[0-9]*"
if not errorlevel 1 (
   set "superstring=%superstring:~0,-1%"
   goto trailloop
)
echo %superstring%


With a few modifications, this should work:

@echo off
for /F %%a in ('dir /b *.avi ^| findstr /i "S[0-9]*E[0-9]*"') do (set "superstring=%%~a" & call :FINDSTR )
PAUSE
exit


:FINDSTR
:leadloop
set errorlevel=
echo.%superstring:~1%|findstr /i "S[0-9]*E[0-9]*" >nul
if not errorlevel 1 (
set "superstring=%superstring:~1%"
goto leadloop
)
for /f "tokens=1 delims=." %%i in ("%superstring%") do set "superstring=%%i"
echo %superstring%
goto :EOF