batch find file extension

Vhaerun picture Vhaerun · Sep 26, 2008 · Viewed 49.7k times · Source

If I am iterating over each file using :

@echo off

FOR %%f IN (*\*.\**) DO (
    echo %%f
)

how could I print the extension of each file? I tried assigning %%f to a temporary variable, and then using the code : echo "%t:~-3%" to print but with no success.

Answer

Sam Holloway picture Sam Holloway · Sep 26, 2008

The FOR command has several built-in switches that allow you to modify file names. Try the following:

@echo off
for %%i in (*.*) do echo "%%~xi"

For further details, use help for to get a complete list of the modifiers - there are quite a few!