Get the file name from a full or relative path knowing that this one is not a parameter of the batch

耗尽温柔 提交于 2019-12-13 08:27:50

问题


When a file, along with relative path or full path..., is given as a parameter of a batch, I know how to expand %1 to get the file name

    %~n1 

It's quite easy!

Nevertheless, I want to get the name of file from its relative/full path which is right handled inside the batch.

Please look at my code. I'd like to expand %%x (which is a relative path in my case) to take account only its file name. Please do you have any idea? Thanks

 @echo off
 SETLOCAL EnableDelayedExpansion
 set currDir=%CD%
 for /f "tokens=*" %%x in (Lists.out) do (
 echo %%x
 set filetxt=%%x)

Here's an example "Lists.out" file with some different files built-in relative paths. "Lists.out" can be made of

   Temp\my file1_X
   Temp\my file2_X
   ...............

回答1:


...
 echo %%~nx
 set filetxt=%%~nx)

should cure your problem.

or use ~nxx if you want the extension too.



来源:https://stackoverflow.com/questions/16071783/get-the-file-name-from-a-full-or-relative-path-knowing-that-this-one-is-not-a-pa

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