views:

2817

answers:

2

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 !

+6  A: 

When Command Extensions are enabled (Windows XP and newer, roughly), you can use the syntax %~nF to only get the filename.

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

should echo only the filenames.

AKX
add %~xF at the end if you want to see the file extension as well.
akf
Or %~nxf to get file names with extensions.
Helen
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
A: 

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) :-)

Chris