0

I want to extract the only path to the file from a full path.

for %a in (/path/to/file/filename.txt) do echo %%~dpa

example:

/some/path/to/file/filename.txt

I want to get the only path like

/some/path/to/file/

shreyshrey
  • 515
  • 6
  • 20
Sudhakar
  • 97
  • 1
  • 1
  • 5

2 Answers2

0

Maybe this would do the trick for you:

x=/path/to/file/filename.txt

y=${x%\/*}

echo $y

you should get

/path/to/file

0

You can use string operations to parse the file_name out (https://stackoverflow.com/a/3162500).

Delete the shortest matching file_name from the end of full_path using % operator.

full_path=/path/to/file/filename.txt
file_name=${full_path##*/}
echo "${full_path%$file_name}"

Replace full_path with your desired input and it will return /path/to/file/