54
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
)
3
  • 3
    For future reference, Microsoft has an online Windows XP - Command-line reference A-Z which is still mostly applicable to later versions. The documentation in the answer you accepted is at the end of the section on the for command.
    – martineau
    Oct 18, 2012 at 3:31
  • @martineau: Unfortunately, the online 'Windows XP - Command-line reference A-Z' page is no longer on-line (removed by microsoft) and an alternative for this documentation is not yet available (as far as I can find any codumentation on '%~' argument documentation)
    – PapaAtHome
    Jul 2, 2018 at 9:08
  • 2
    @PapaAtHome: Fortunately one can still view the command line reference via web.archive.org's wayback machine.
    – martineau
    Jul 2, 2018 at 12:36

2 Answers 2

91

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.
7
  • 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". Nov 3, 2016 at 20:46
  • 1
    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.
    – Bob
    Nov 3, 2016 at 23:24
  • @DaleBarnard Also, consider using PowerShell - much much less pain and probably more consistent results.
    – Bob
    Nov 3, 2016 at 23:24
  • 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? Apr 28, 2017 at 16:38
  • @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.
    – Bob
    Apr 28, 2017 at 16:51
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .