FINDSTR: Print the found string, not the line.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bodosko
Posts: 7
Joined: 17 Jun 2009 01:44

FINDSTR: Print the found string, not the line.

#1 Post by bodosko » 17 Jun 2009 01:56

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!

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 17 Jun 2009 10:23

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"

bodosko
Posts: 7
Joined: 17 Jun 2009 01:44

#3 Post by bodosko » 17 Jun 2009 10:59

Didn't work.
It returned the string after the first "." not after the first ".S"

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 18 Jun 2009 10:29

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%

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

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

#5 Post by ghostmachine4 » 18 Jun 2009 21:44

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


traveller
Posts: 2
Joined: 28 Jun 2009 04:52

#6 Post by traveller » 28 Jun 2009 05:03

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

Post Reply