Dos batch: How to get the file name from a variable that is not an argument?

I see many examples of using% nx1 to get only the name of the file transferred along with% 1.

But what if you have another variable set using the file path and want to programmatically get the file name from this

for instance

@echo off SET MYPATH="c:\program files\myapp\somefile.ext" SET MYPATHFILE = ?? 

I am trying to get only "somefile.ext". I tried replacing 1 with var

 %nxMYPATH 

But that does not work.

+4
source share
1 answer

You can use the following line:

 FOR /F %%I IN ("%MYPATH%") DO SET MYPATHFILE=%%~nxI 
+3
source

Source: https://habr.com/ru/post/1380622/


All Articles