0

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.

agah
  • 15
  • 3
  • 1
    Append `\..` to the path to point to the directory one level up (that is, the parent)... To resolve it (cosmetically), use `for %%P in ("C:\Users\Aga\Desktop\something\..") do echo %%~fP`... – aschipfl Oct 24 '16 at 15:35
  • 1
    Look also: http://stackoverflow.com/questions/14070252/batch-file-going-back-two-steps-in-a-directory-path – Fusseldieb Oct 24 '16 at 15:36

1 Answers1

1

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).

Compo
  • 36,585
  • 5
  • 27
  • 39