Batch: Remove file extension
You can use %%~nf
to get the filename only as described in the reference for for
:
@echo off
for /R "C:\Users\Admin\Ordner" %%f in (*.flv) do (
echo %%~nf
)
pause
The following options are available:
Variable with modifier Description %~I Expands %I which removes any surrounding quotation marks (""). %~fI Expands %I to a fully qualified path name. %~dI Expands %I to a drive letter only. %~pI Expands %I to a path only. %~nI Expands %I to a file name only. %~xI Expands %I to a file extension only. %~sI Expands path to contain short names only. %~aI Expands %I to the file attributes of file. %~tI Expands %I to the date and time of file. %~zI Expands %I to the size of file. %~$PATH:I Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string.
In case the file your variable holds doesn't actually exist the FOR
approach won't work. One trick you could use, if you know the length of the extension, is taking a substring:
%var:~0,-4%
the -4
means that the last 4 digits (presumably .ext) will be truncated.
Without looping
I am using this if I simply want to strip the extension from a filename or variable (without listing any directories or existing files):
for %%f in ("%filename%") do set filename=%%~nf
If you want to strip the extension from a full path, use %%dpnf
instead:
for %%f in ("%path%") do set path=%%~dpnf
Example:
(Use directly in the console)
@for %f in ("file name.dat") do @echo %~nf
@for %f in ("C:\Dir\file.dat") do @echo %~dpnf
OUTPUT:
file name
C:\Dir\file
I'm also a stranger to windows cmd, but try this:
echo %%~nf