Get the parent directory path of the exe file

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
yadhukrishna36
Newbie
Posts: 13
Joined: Tue Oct 06, 2020 6:28 pm

Get the parent directory path of the exe file

Post by yadhukrishna36 » Tue Jan 05, 2021 4:29 pm

I need to get the directory path of the .exe file ( created using Ctrl+F9 ) in my script to get additional files.

I keep some config files inside the directory of .exe itself and I need to get those without hardcoding the path.
Suppose a user runs my .exe from C:\Users\<userName>\Documents\<appName> and another user may run it from some other directory so, Is there any way that I get the Parent directory path of the .exe?

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Get the parent directory path of the exe file

Post by Grovkillen » Tue Jan 05, 2021 8:40 pm

This is how I do it:

Code: Select all

SRT>GET_ROOT_PATH
  Let>TEMP=%SCRIPT_DIR%
  Separate>TEMP,\,TEMP_ARRAY
  Let>ROOT_PATH=
  Let>k_stop=TEMP_ARRAY_count-ROOT_PATH_STOP
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>TEMP_LEVEL=TEMP_ARRAY_%k%
    ConCat>ROOT_PATH,TEMP_LEVEL
    IfNot>k=k_stop
      ConCat>ROOT_PATH,\
    Endif>
  Until>k=k_stop
  MidStr>ROOT_PATH,0,1,VOLUME_LETTER
  DelArray>TEMP_ARRAY
END>GET_ROOT_PATH
Let>ROOT_PATH_STOP=1
GoSub>GET_ROOT_PATH
I also get the volume letter this way. Change how many steps up in the hierarchy you want your root to be by setting the ROOT_PATH_STOP variable.
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Get the parent directory path of the exe file

Post by JRL » Tue Jan 05, 2021 10:43 pm

Long story short. The variable Script_Dir will contain the location from which your executable was run.

yadhukrishna36
Newbie
Posts: 13
Joined: Tue Oct 06, 2020 6:28 pm

Re: Get the parent directory path of the exe file

Post by yadhukrishna36 » Thu Jan 14, 2021 1:03 pm

Thanks, JRL :P

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts