Espequair
Espequair

Reputation: 33

How do I use the Searchmask for FORFILES in batch?

So I am trying to use forfiles /M DX* /P ..\fsb /C "cmd /c cd ..\extracted && fsb2wav @path" to extract .wav files from .fsb files.
But I need to extract only the files that are formatted like DX*.fsb and no DX_FR.fsb or DX_DE.fsb where * any be any number of alphanumeric characters.
Is there a way to do that? I have been messing around with the /M option but it does not seem to like regular expressions.

Upvotes: 1

Views: 1266

Answers (1)

aschipfl
aschipfl

Reputation: 34919

The wildcard * stands for any number of characters valid in file/path names (? stands for a single valid character), so you need to do the filtering yourself.

For your task at hand I would not use forfiles here, because it is quite slow as it instances a new cmd instance for every iteration and it is more complicated to apply an additional filter. The way I would go for is a for /F loop that parses the output of a dir /B command piped into findstr:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define constants here:
set "WORKDIR=..\extracted"
set "SEEKDIR=..\fsb"
set "DIRFILT=DX*.fsb"
set "REGEXPR=^DX[0-9A-Z]*.fsb$"

pushd "%SEEKDIR%" && (
    for /F "eol=| delims=" %%F in ('
        dir /B "%DIRFILT%" ^| ^
            findstr /I /R "%REGEXPR%"
    ') do (
        pushd "%WORKDIR%" && (
            ECHO fsb2wav "%%~fF"
            popd
        )
    )
    popd
)

endlocal
exit /B

After having tested the script, remove the upper-case ECHO to actually execute the fsb2wav tool.

Upvotes: 2

Related Questions