agah
agah

Reputation: 15

How to delete subpath in batch file?

How can I edit path which i get from command line arguments and delete last subdirectory?

e.g. I run my batch file with argument: C:\Users\Aga\Desktop\something

and I want to use only "C:\Users\Aga\Desktop" part in my batch file.

Upvotes: 0

Views: 142

Answers (1)

Compo
Compo

Reputation: 38709

Add this as the very first line of your batch file:

@If Not "%~dp1"=="" @(Set "Parent=%~dp1"&Call Set "Parent=%%Parent:~,-1%%")

Then use %Parent% throughout your script as necessary, (%Parent% will be the next directory up the tree if the input was a directory or the container folder if the input was a file).

Upvotes: 1

Related Questions