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!
FINDSTR: Print the found string, not the line.
Moderator: DosItHelp
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
If the value you're looking for is ".Sxx" where xx could be numbers, then try this:
This will give you the first token as the value of the first ".S" (if it always starts with ".S"
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"
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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%
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: FINDSTR: Print the found string, not the line.
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
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