Search code examples
batch-filecmdwindows-explorer

Using explorer.exe with wildcards in batch file


My problem is starting explorer.exe with select parameters in batch file. I use this:

if exist "!SATIR!\!SATIR:~20,-23!.mp4" (explorer.exe /select, "!SATIR!\!SATIR:~20,-23!.mp4") else (explorer.exe /select, "!SATIR!\!SATIR:~20,-23!.mkv")

It works but I don't want to depend on specific file extensions. I want to use something like wildcard for all extensions (avi, wmv, etc.).

Using something like:if exist "!SATIR!\!SATIR:~20,-23!.*" explorer.exe /select, "!SATIR!\!SATIR:~20,-23!.*" doesn't work. Fails at explorer.exe part. So far I got nothing. Is it possible to do?

PS. !SATIR! variable contains local address of those mp4 and mkv files. It is something like: F:\Movies\000y.001y\The.Lord.of.the.Rings.The.Return.of.the.King.(2003){0167260}[00087]

PS. !SATIR:~20,-23! EQU The.Lord.of.the.Rings.The.Return.of.the.King


Solution

  • Windows Explorer does not support wildcards for option /select as this option requires the path of a directory, or the name of a file (with or without path depending on usage of root, or the name of an object. See the command line option pages for Windows Explorer for Windows 95/98/Me/NT4, Windows 2000/NT4, Windows XP (and later Windows versions). Selecting multiple files is not possible with this command line option as far as I know.

    But the batch file can nevertheless work with a wildcard for the file extension on testing for existence and then start Windows Explorer with the full name of the file matching first the file specification.

    Here is an example batch file:

    @echo off
    set SATIR=F:\Movies\000y.001y\The.Lord.of.the.Rings.The.Return.of.the.King.(2003){0167260}[00087]
    set FILESPEC=%SATIR:~20,-23%.*
    if exist "%SATIR%\%FILESPEC%" (
        for %%f in ("%SATIR%\%FILESPEC%") do (
            %windir%\explorer.exe /select,"%%f"
            goto :LoopExit
        )
    )
    :LoopExit
    set FILESPEC=
    set SATIR=
    

    Well, the if condition is more or less useless here. The behavior is the same on the batch file below.

    @echo off
    set SATIR=F:\Movies\000y.001y\The.Lord.of.the.Rings.The.Return.of.the.King.(2003){0167260}[00087]
    set FILESPEC=%SATIR:~20,-23%.*
    for %%f in ("%SATIR%\%FILESPEC%") do (
        %windir%\explorer.exe /select,"%%f"
        goto :LoopExit
    )
    :LoopExit
    set FILESPEC=
    set SATIR=