Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

windows - Get filename in batch for loop

I have the following For loop in a batch file:

for /R c:estsrc %%i IN (*.*) DO (
MOVE %%i C:estdestination
ECHO %%i
exit
)

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

question from:https://stackoverflow.com/questions/1100452/get-filename-in-batch-for-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...