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.

Share:
230,362

Related videos on Youtube

Jay
Author by

Jay

rookie dba

Updated on September 18, 2022

Comments

  • Jay
    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
    Dale Barnard over 7 years
    Great 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
    tvdo over 7 years
    You can perform a textual or regex search by piping the path into the find or findstr command, e.g. echo %%f | find "\.git\" > NUL or echo %%f | findstr /R /C:"\\\.git\\" /C:"\\\.git\>" > NUL and then checking the errorlevel with the if 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 that if errorlevel has quirks with fall-through, and you probably can't if %errorlevel% within a loop so you might need to SetLocal EnableDelayedExpansion first and then if !errorlevel! instead.
  • tvdo
    tvdo over 7 years
    @DaleBarnard Also, consider using PowerShell - much much less pain and probably more consistent results.
  • thebunnyrules
    thebunnyrules about 7 years
    The 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
    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. The blah blah blah is just taking the example in the question and replacing the filename bit with the %%~nxf substitution - the blahs are just filler and have no particular meaning.
  • Jesse Chisholm
    Jesse Chisholm over 6 years
    NOTE: These expansions also work on command line arguments. e.g., pushd %~dp0 or cd %~dp0 puts you in the directory where the batch file lives. If your first batch file argument is a full path, then set DIR=%~dp1 gives you the directory of that path, and set FIL=%~nx1 gives you the filename and extension of that path.
  • JinSnow
    JinSnow over 4 years
    when 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)