In a batch file, how do I get a file name from a file path?

百般思念 提交于 2021-02-16 14:36:09

问题


I have a batch file that requires the user to enter a file path. Later on in the file I want to isolate just the filename and extention from the path, ie anything after the last '\'.

set FILEPATH=\\srv-01\My Docs\Templates\My SpreadSheet.xls
...
set FILENAME=???

What do i need to set FILENAME to in order for it to equal 'My SpreadSheet.xls'?

Hopefully this is fairly simple to do. Thanks!


回答1:


@echo off
set FILEPATH=\\srv-01\My Docs\Templates\My SpreadSheet.xls
for /F "delims=" %%A in ("%FILEPATH%") do set "FILEPATH=%%~nxA"
echo.%FILEPATH%


来源:https://stackoverflow.com/questions/11104433/in-a-batch-file-how-do-i-get-a-file-name-from-a-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!