How to get filename only without path in windows command line?
230,362
Solution 1
Use %~nxf
for <filename>.<extension>
.
And yes, you can do:
for /r %%f in (*) do (
echo "blah blah blah '%%~nxf'" >> blahblah_%%~nxf.txt
)
See for /?
:
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~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 - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to 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, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
Solution 2
I'm not yet able to comment, but I believe the following should be noted to avoid confusion.
While f
can be used as a FOR variable name, it's best to use another letter to avoid confusing it with format letter f
(which, as stated in Bob's answer, returns full path name), or at least use a capital F
.
Related videos on Youtube
Comments
-
Jay over 1 year
for /r %f in (*) do echo %f
Gives the output filename along with the entire path
\path\to\dir\<filename>
How do i get just the
<filename>
without the path included? I need to use that 'filename' string.Also, is it possible to do the following, once the filename is acquired?
for /r %%f in (*) do ( echo "blah blah blah 'filename'" >> blahblah_filename.txt )
-
Dale Barnard over 7 yearsGreat answer. Any idea how I could search for a substring in a full path? For example, I want to know if c:\code\myapp\.git\somefolder includes the text ".git".
-
tvdo over 7 yearsYou can perform a textual or regex search by piping the path into the
find
orfindstr
command, e.g.echo %%f | find "\.git\" > NUL
orecho %%f | findstr /R /C:"\\\.git\\" /C:"\\\.git\>" > NUL
and then checking the errorlevel with theif
command. (findstr regex is very limited, so that's my attempt at enforcing a leading\
and [either a trailing\
or end of string]). Do note thatif errorlevel
has quirks with fall-through, and you probably can'tif %errorlevel%
within a loop so you might need toSetLocal EnableDelayedExpansion
first and thenif !errorlevel!
instead. -
tvdo over 7 years@DaleBarnard Also, consider using PowerShell - much much less pain and probably more consistent results.
-
thebunnyrules about 7 yearsThe blah blah is confusing me, what is it supposed to represent? How does the full path of the file and the extracted file name fit into this answer?
-
tvdo about 7 years@thebunnyrules
%~nxf
is the filename, as I said in the first sentence. When you use it within a batch file, you need to double up the%
, making it%%~nxf
. Theblah blah blah
is just taking the example in the question and replacing thefilename
bit with the%%~nxf
substitution - theblah
s are just filler and have no particular meaning. -
Jesse Chisholm over 6 yearsNOTE: These expansions also work on command line arguments. e.g.,
pushd %~dp0
orcd %~dp0
puts you in the directory where the batch file lives. If your first batch file argument is a full path, thenset DIR=%~dp1
gives you the directory of that path, andset FIL=%~nx1
gives you the filename and extension of that path. -
JinSnow over 4 yearswhen you use
%~n0
as argument, don't forget to quote it ("%~n0"
) if it contains space (or it each part separated by space will count as a different arg)