Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Filename from Filepath

Tags:

batch-file

cmd

I want to get the filename from a filepath. My problem is, that I found many solutions for handling it with input via parameters. But I want to use a userinput instead of a parameter.

The soultion for parameters is:

%~nxI       //I could be number for the parameter count

My script actually looks like this:

   @echo off
   set /p path=Film: 
   echo %path2%
   pause

Now I want to get the filename + extension from %path% and write it to %path2%.

Could anyone help me please?

like image 940
Michael Walter Avatar asked May 18 '26 15:05

Michael Walter


2 Answers

set /p x=Film:
echo %x%
for %%F in (%x%) do set q=%%~nF
echo %q%

...and for Pete's sake do not prompt the user to set the PATH variable! use some other variable name!

like image 157
Mike Nakis Avatar answered May 21 '26 01:05

Mike Nakis


an alternative, instead of using FOR, that may be useful in some situations, is to substitute the variable passing the values as parameters in a CALL.

call :extractfn %x% q
echo %q%
goto :eof
:extractfn
set %2=%~n1
goto :eof
like image 27
PA. Avatar answered May 21 '26 01:05

PA.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!