44

I have the following For loop in a batch file:

for /R c:\test\src %%i IN (*.*) DO (
MOVE %%i C:\test\destination
ECHO %%i
exit
)

The result of the ECHO outputs the entire file path Ex: C:\Foldername\Filename I need to ECHO out only the Filename.Is there a specific command which would give me the filename ? Thanks !

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Murtaza Mandvi
  • 10,708
  • 23
  • 74
  • 109
  • 6
    BTW, if you are using a version of windows made this century, it is not DOS that you are using, it is the windows command prompt. – whatsisname Jul 08 '09 at 21:12

5 Answers5

83

When Command Extensions are enabled (Windows XP and newer, roughly), you can use the syntax %~nF (where F is the variable and ~n is the request for its name) to only get the filename.

FOR /R C:\Directory %F in (*.*) do echo %~nF

should echo only the filenames.

he_the_great
  • 6,554
  • 2
  • 30
  • 28
AKX
  • 152,115
  • 15
  • 115
  • 172
  • 18
    add %~xF at the end if you want to see the file extension as well. – akf Jul 08 '09 at 21:20
  • 39
    Or %~nxf to get file names with extensions. – Helen Jul 08 '09 at 21:22
  • IIRC, most of the extensions have been in CMD.EXE since at least NT 3.5, and some of them were even there in the pre-gui beta releases of NT. The NT team saw no reason to preserve all of the pain of COMMAND.COM when building a "new" version of Windows. They appeared to have not bothered to tell the documentation team about them for a long time, however. – RBerteig Jul 10 '09 at 00:28
  • this recurses all the subdirectories. is there a flag to not recurse? – Dave Jun 11 '11 at 16:39
  • 1
    @Dave: `for %f in (*.*)` (or `for %f in (c:\directory\*.*)` ) should work. – AKX Jun 14 '11 at 11:23
  • correct command which worked `FOR /R C:\Directory %%F in (*.*) do echo %~nF` – KK99 Jul 07 '14 at 11:34
  • @KarthikKrishnan: that's right - inside a script file you need one more `%` than at the command prompt... goodness knows why. – Tony Delroy Aug 29 '14 at 10:02
  • 6
    this cmd isn't working for me on win7 64bits. "~nF was unexpected at this time." – Dimitri Kopriwa Jul 02 '15 at 13:44
  • 2
    Don't forget to use double-percents if this command is inside a batch file: `echo %%~nF` – Richard Whitehead Oct 30 '18 at 12:14
  • @TonyDelroy The reason for that is that single percents are already taken for the command line arguments of the batch file. At least that's what would make most sense, since the command line arguments do use single percent variables. – Egor Hans Jan 28 '20 at 15:25
  • @EgorHans: interesting idea to explore - but aren't the command line args always numbers (e.g. `%2`), while these have to start with a letter (or modifiers such as the `~n`)? – Tony Delroy Feb 21 '20 at 05:59
  • @TonyDelroy Correct from all I know, but I just remembered there is, in fact, a dedicated meaning for single percents with letters: Environment variables. Not sure why it can't distinguish in-script variables from them by the missing trailing percent, probably historical or something. – Egor Hans Feb 22 '20 at 11:43
  • This isn't storing the file name as a variable though? – dcary Apr 17 '20 at 19:43
4

or Just %~F will give you the full path and full file name.

For example, if you want to register all *.ax files in the current directory....

FOR /R C:. %F in (*.ax) do regsvr32 "%~F"

This works quite nicely in Win7 (64bit) :-)

Shef
  • 44,808
  • 15
  • 79
  • 90
Chris
  • 41
  • 1
4

The answer by @AKX works on the command line, but not within a batch file. Within a batch file, you need an extra %, like this:

@echo off
for /R TutorialSteps %%F in (*.py) do echo %%~nF
Martijn Dirkse
  • 543
  • 7
  • 16
3

I am a little late but I used this:

dir /B *.* > dir_file.txt

then you can make a simple FOR loop to extract the file name and use them. e.g:

for /f "tokens=* delims= " %%a in (dir_file.txt) do (
gawk -f awk_script_file.awk %%a
)

or store them into Vars (!N1!, !N2!..!Nn!) for later use. e.g:

set /a N=0
for /f "tokens=* delims= " %%a in (dir_file.txt) do (
set /a N+=1
set v[!N!]=%%a
)
Guy Soffer
  • 31
  • 1
1

If you want to remain both filename (only) and extension, you may use %~nxF:

FOR /R C:\Directory %F in (*.*) do echo %~nxF