I am running the code,

set /p "filepath=Drag and drop your file >"
echo Your file name is : %filepath%

output i am getting

Your file name is : c:/users/philip/desktop/file.txt

When user drag and drop there file in command prompt and press enter it shows full file path as output..

expected output

Your file name is : file.txt

But, what i am expecting is, only the file name with extention..like shown above as output.

Note : File path and File name may varies..


You can use this:

set /p "filepath=Drag and drop your file >" 
for %%a in ("%filepath%") do echo Your file name is: %%~nxa
pause >nul

Here n and x are the modifiers of the for meta variables. To learn more about them, open cmd and type for /? and look at the end.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.