I have this code
for /R "C:\myfiles" %%i in (*super*.ts) do (
echo %%i
)
so I want to iterate over all files in the folder C:\myfiles which have in the path super and end with .ts. e.g.
"c:\myfiles\foo\bar\super\123.ts"
but that substring search doesn't work.
CodePudding user response:
What about this:
dir /S /B C:\myfiles\*.ts | findstr /I "Super"
Explanation:
dir /S : search in subdirectories
dir /B : show bare format, like C:\myfile\subdir\filename.ts
...
findstr /I : filter is case insensitive
CodePudding user response:
Your for /R loop searches for files whose names contain super, but that is not what you want.
If one of the following situations apply, you could use below code snippets:
If the word
supermay occur anywhere in the path, even in the file name:for /F "delims=" %%F in (' dir /S /B /A:-D-H-S "C:\myfiles\*.ts" ^| findstr /I /C:"super" ') do ( echo(%%F )The
/A:-D-H-Soption ofdirexcludes directories as well as hidden and system files.If the word
superis the name of any directory in the hierarchy:for /F "delims=" %%F in (' dir /S /B /A:-D-H-S "C:\myfiles\*.ts" ^| findstr /I /C:"\\super\\" ') do ( echo(%%F )The
\\in thefindstrsearch expression constitutes an escaped\.If the word
superis the name of the immediate parent directory:for /F "delims=" %%F in (' dir /S /B /A:-D-H-S "C:\myfiles\*.ts" ^| findstr /I /R /C:"\\super\\[^\\][^\\]*\.ts$" ') do ( echo(%%F )The string
\\super\\[^\\][^\\]*\.ts$is afindstrregular expression, that anchors to the right ($) a string that consists of the literal string\super\plus a string of at least a character other than\and the literal string.ts.And here is an alternative approach without
findstrbut anifstatement instead, together with a standardforloop to resolve the name of the parent directory:for /R "C:\myfiles" %%F in (*.ts) do ( for %%E in ("%%~F\..") do ( if /I "%%~nxE"=="super" echo(%%~F ) )This may be faster, because there is no
for /Fand no pipe (|) involved, both of which instantiate newcmd.exeinstances.
