Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get filename from string-path?

How do I get the filename from this string?

"C:\Documents and Settings\Usuario\Escritorio\hello\test.txt" 

output:

"test.txt" 

I really tried to find this one before posting, but all the results were contaminated, they talk about getting filenames from current dir (I must work with strings only)

like image 690
ajax333221 Avatar asked May 01 '12 03:05

ajax333221


People also ask

How do I separate the filename and path in Python?

split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file. txt' component of path name is tail and '/home/User/Desktop/' is head.

How do I get the file path in Python?

To retrieve a file in Python, you need to know the exact path to reach the file, in Windows, you can view a particular file's path by right-clicking the File-> Properties-> General-> Location. Similarly, to run a script, the working directory needs to be set to the directory containing the script.


2 Answers

Method 1

for %%F in ("C:\Documents and Settings\Usuario\Escritorio\hello\test.txt") do echo %%~nxF 

Type HELP FOR for more info.

Method 2

call :sub "C:\Documents and Settings\Usuario\Escritorio\hello\test.txt" exit /b  :sub echo %~nx1 exit /b 

Type HELP CALL for more info.

like image 145
dbenham Avatar answered Sep 29 '22 22:09

dbenham


if your path comes as a parameter, just use:

%~nx1 (1 is for the first param and we suppose it's the first one)

echo %~nx1 would return directly test.txt

like image 33
stormofwar Avatar answered Sep 29 '22 23:09

stormofwar