Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Variable for folder above the script folder


  • Please log in to reply
17 replies to this topic
jsm
  • Members
  • 110 posts
  • Last active: Jun 18 2016 09:23 AM
  • Joined: 30 Apr 2010
What can i use to tell the script to look in the folder that the folder that the script is in.

EG. script is in C:\Program Files\Cheese\Ricotta\Script.ahk
and i want to tell the script to move a file to C:\Program Files\Cheese\ but i can't type the absolute path because the drive or file path might change.

wolf_II
  • Members
  • 343 posts
  • Last active: Jul 23 2010 05:19 PM
  • Joined: 18 Oct 2007
FileWithPath := "C:\Program Files\Cheese\Ricotta\Script.ahk"

; this concatenation is equivalent but not identical to "C:\Program Files\Cheese\Ricotta"
ParentOfFile := FileWithPath "\.."

; this concatenation is equivalent but not identical to "C:\Program Files\Cheese"
GrandParentOfFile := ParentOfFile "\.."

; show the simple concatenations
MsgBox, %FileWithPath%`n%ParentOfFile%`n%GrandParentOfFile%

; test
FileMove, %FileWithPath%, %GrandParentOfFile%
Tested.

The file starts off being in its parent's directory anyway. To move it up one notch in the tree structure, use grandparent.
Wolf

Schön wär's, wenn's schön wär!

jsm
  • Members
  • 110 posts
  • Last active: Jun 18 2016 09:23 AM
  • Joined: 30 Apr 2010
u tried that code and got a msgbox saying:

C:\Program Files\Cheese\Ricotta\Script.ahk
C:\Program Files\Cheese\Ricotta\Script.ahk\..
C:\Program Files\Cheese\Ricotta\Script.ahk\..\..

Im assuming then that im not meant to copy the code straight into my script. How would i put it in my script?

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Use the built in variable A_ScriptDir and perhaps splitpath or some string functions (stringtrim, instr, substr)

jsm
  • Members
  • 110 posts
  • Last active: Jun 18 2016 09:23 AM
  • Joined: 30 Apr 2010
If i were to use split string then i would need a command that tells me how many arrays were created becasue i need to go back 2. so if 5 arrays where created the 5th one would be Script.ahk the 4th would be Riccotta but i need only up until the 3rd which is Cheese (if there are 5 arrays total). is there any command to do that?

also could you give a snippet of code as an example because the code itself would be more helpful than just the command because then i know how to actually apply it to my script.

wolf_II
  • Members
  • 343 posts
  • Last active: Jul 23 2010 05:19 PM
  • Joined: 18 Oct 2007

Im assuming then that im not meant to copy the code straight into my script.

You are right, this is a demonstration how to use Windows functions that resolve paths (called by FileMove command) as an answer for your initial question:

What can i use to tell the script to look in the folder that the folder that the script is in.

You could create the directory structure and a (zero-byte) file "C:\Program Files\Cheese\Ricotta\Script.ahk" and see the effect of the script. I did that to test the working of the code.

How would i put it in my script?

Try the following: using exactly the same technique, this time as a user-defined function. You could create the directory structure and a (zero-byte) file "C:\Program Files\Cheese\Ricotta\Script.ahk" and see the effect of the script.
;---------------------------------------------------------------------------
Script:
;---------------------------------------------------------------------------
    ; some code
    File := "C:\Program Files\Cheese\Ricotta\Script.ahk"
    MoveUpDirTree(File)
    ; more code
Return



;---------------------------------------------------------------------------
MoveUpDirTree(File) {
;---------------------------------------------------------------------------
    ; This function will move a file one notch up in the directory tree
    ; No error checking included if File exists
    ;-----------------------------------------------------------------------
    FileMove, %File%, %File%\..\..
}
Hope that made it clearer. If it is not sufficiently clear yet, come back and ask again.
Wolf

Schön wär's, wenn's schön wär!

jsm
  • Members
  • 110 posts
  • Last active: Jun 18 2016 09:23 AM
  • Joined: 30 Apr 2010
Cool, Thanks for all your help.
Just a quick Qustion:
What exactly is the difference between := and just =.
i used
File = %A_scriptDir%\Script.ahk
instead of
File := "%A_scriptDir%\Script.ahk"
becasue the := didn't work.
Am i right in assuming := takes the %A_scriptDir% Literally and dosent replace it with C:\Program Files\Cheese\Ricotta\?

Thanks again.

EDIT: also how can i run the file that the script just copied. Is there any way to save the file path into a variable?

wolf_II
  • Members
  • 343 posts
  • Last active: Jul 23 2010 05:19 PM
  • Joined: 18 Oct 2007

Am i right in assuming := takes the %A_scriptDir% Literally

Yes.

how can i run the file that the script just copied. Is there any way to save the file path into a variable?

There are many was to accomplish this. This extended user function will now return the name of the file you are after:

;---------------------------------------------------------------------------
Script:
;---------------------------------------------------------------------------
    ; some code
    File := "C:\Program Files\Cheese\Ricotta\Script.ahk"
    MovedFile := MoveUpDirTree(File)
    Run, %MovedFile%
    ; more code
Return



;---------------------------------------------------------------------------
MoveUpDirTree(File) {
;---------------------------------------------------------------------------
    ; this function will move a file one notch up in the directory tree
    ; returns the path and filename of the moved file
    ;-----------------------------------------------------------------------
    FileMove, %File%, %File%\..\..
    SplitPath, File, FileName, FileDir
    Return, FileDir "\..\" FileName
}

OFFTOPIC:
May I ask you, please, how my suggested code in this thread worked. You said you would test it, but you have never bothered to report failure/success.
Wolf

Schön wär's, wenn's schön wär!

jsm
  • Members
  • 110 posts
  • Last active: Jun 18 2016 09:23 AM
  • Joined: 30 Apr 2010
still testing will let you know in a day or so

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

Use the built in variable A_ScriptDir and .. string functions (stringtrim, instr, substr)

+1
msgbox % var:=subStr( subStr( a_scriptDir, 1, ( sP:=inStr( a_scriptDir, "", 0, 0 ) - 1 ) ), 1, sP )
;)

Posted Image

don't duplicate, iterate!


wolf_II
  • Members
  • 343 posts
  • Last active: Jul 23 2010 05:19 PM
  • Joined: 18 Oct 2007

use Windows functions

Give simplicity a chance
FileMove, %File%, %File%\..\..
:wink:


@TLM: No, really, why would you want to re-invent the wheel in this case? I am not questioning your personal taste. But could you be kind enough and enlighten me, please?
Wolf

Schön wär's, wenn's schön wär!

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

why would you want to re-invent the wheel in this case? ...could you be kind enough and enlighten me, please?

I was mostly agreeing with hugov and was not reinventing anything.
I'm just using what is already in AHK rather than creating a custom function.

It all depends on what you view as simple I guess :?: :?:

Posted Image

don't duplicate, iterate!


wolf_II
  • Members
  • 343 posts
  • Last active: Jul 23 2010 05:19 PM
  • Joined: 18 Oct 2007
Thanks for your reply, TLM.

File := "C:\Program Files\Cheese\Ricotta\Script.ahk"
FileMove, %File%, %File%\..\..
This is also using what is already in AHK.
My first (verbose) example was to demonstrate how that works.
My (admittedly unnecessary) first user defined function was to help OP include the technique into a script.

It all depends on what you view as simple I guess

Full confirmation by me.

EDIT: also how can i run the file that the script just copied. Is there any way to save the file path into a variable?

In the most respectful way, sir, how would you use your code to move a file such as "C:\Program Files\Cheese\Ricotta\Script.ahk" one notch up the tree and have a variable (e.g. MovedFile) set to the resulting file to use with Run, %MovedFile%?

File := "C:\Program Files\Cheese\Ricotta\Script.ahk"
Run, % MovedFile := MoveUpDirTree(File)

MoveUpDirTree(File) {
    FileMove, %File%, %File%\..\..
    SplitPath, File, FileName, FileDir
    Return, FileDir "\.." FileName
}

Wolf

Schön wär's, wenn's schön wär!

wolf_II
  • Members
  • 343 posts
  • Last active: Jul 23 2010 05:19 PM
  • Joined: 18 Oct 2007
@ TLM: quad erat demonstrandum.

@ jsm:

still testing will let you know in a day or so

still waiting
Wolf

Schön wär's, wenn's schön wär!

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

@ TLM: quad erat demonstrandum.

:lol:

Hopefully, rather than ὅπερ ἔδει δεῖξαι ( hoper edei deixai ),
you mean:
Posted Image
logical deduction :cool:.

Posted Image

don't duplicate, iterate!