Jump to content

howto get current directory a script was started from?


rudi
 Share

Recommended Posts

Hi.

[howto get the working directory a compiled script was called from?]

<edit>

The suggestion of Juvigy works best for me:

RunWait(@ComSpec & " /c cd /d %temp%&&echo %cd%>temp.tmp", "", @SW_HIDE); create temp file to save %cd%

Comment 1: "cd /d <some-drive-and-path>" changes the drive as well. This is required in case %TEMP% is NOT located on the same drive as %SYSTEMDRIVE%

Comment 2: The suggestion of Juvigy to use explicitely the %TEMP% folder is also required: There the user always will have write access.

Comment 3: I couldn't figure out why, sometimes the temp.tmp will have a 2nd, empty line. So just to take the first line should be a good idea.

So here is the function I'll use now:

Func GetWD()
    ; Autoit v3.3.6.1
    ; thanks to Juvigy
    ; modified by Rudi
    RunWait(@ComSpec & " /c cd /d %temp%&&echo %cd%>temp.tmp", "", @SW_HIDE); create temp file to save %cd%
    $file = FileOpen(@TempDir & "\temp.tmp", 0)
    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "cannot open " & @TempDir & "\temp.tmp to retrieve the working directory!")
        Return False
    EndIf
    ; Read in just the 1st line. (There might be an empty 2nd line)
    $WD = FileReadLine($file)
    FileClose($file)
    FileDelete(@TempDir & "\temp.tmp")
    $WD &= "\"
    If StringRight($WD, 2) == "\\" Then $WD = StringTrimRight($WD, 1) ; the main script expects trailing "\" for path strings
    Return $WD
EndFunc

</edit>

I'm doing a "which.exe", that is searching all the path's directory for all possible executable alterations of a program name specified as $CMDLINE[1].

It's working fine, with one problem, that I can't work out so far:

The first dir, that Windows' command line is searching is the *CURRENT* directory. How can I retrieve from *within* the running autoit program the current directory it was called *FROM*?

Example:

A CMD box is opened, current drive & path = C:\Documents and settings\Rudi\

From that CMD box, I enter "which robocopy.exe" to find all the occurences of "robocopy.exe" in the DIRs pecified in %PATH%

How can I know from within "which.exe", that it was called from THAT current?

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Try this:

RunWait(@ComSpec & " /c %SystemDrive%&&cd %temp%&&echo %cd%>temp.tmp", "", @SW_hide); create temp file to save %cd%
$file = FileOpen(@TempDir & "\temp.tmp", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file." & $file)
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Current DIR:", $line)
WEnd
FileClose($file)
FileDelete(@TempDir & "\temp.tmp")

Credit to JoHanatCent

Link to comment
Share on other sites

Maybe with @ScriptDir?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • Moderators

rudi,

Something similar was discussed at length here. I hope it might shed some light. :graduated:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi.

No, I can't see "light" in this thread (I've found it before, and I did read all of it again).

With a simple, plain BAT / CMD file that's a really easy thing:

@echo off
echo "%CD%"

When I drop this to any folder within the folders listed in PATH, it will return the "working directory" it was called from smoothly.

So it looks like it is NOT possible to "see" the "working directory", a compiled script was started from?

Hm. :graduated: Amazing...

What's the technical background? The way the interpreter works for "compiled" AU3 EXE files?

Tx for your reply.

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

No it wont - he is starting it from CMD and wants to get the CMD current directory.

@WorkingDir will show the script location working dir. The only solution to this is

my first post in this topic. Somehow he fails to see it.

Link to comment
Share on other sites

Hi, The problem is when the program is in another place specified in the path. Then the script will report it's actual posistion for example c:\Windows\System32

But: and thanx to Juvigy to pointing to my orriginal suggestion, %cd% does report the current directory.

so here is my slightly improved (&tested) suggestion:

RunWait(@ComSpec & " /c echo %cd%>%temp%\temp.tmp", "", @SW_hide); create temp file to save %cd%
$file = FileOpen(@TempDir & "\temp.tmp", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file." & $file)
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Current DIR:", $line)
WEnd
FileClose($file)
FileDelete(@TempDir & "\temp.tmp")
Edited by JoHanatCent
Link to comment
Share on other sites

Hi, The problem is when the program is in another place specified in the path. Then the script will report it's actual posistion for example c:\Windows\System32

But: and thanx to Juvigy to pointing to my orriginal suggestion, %cd% does report the current directory.

so here is my slightly improved (&tested) suggestion:

RunWait(@ComSpec & " /c echo %cd%>%temp%\temp.tmp", "", @SW_hide); create temp file to save %cd%
$file = FileOpen(@TempDir & "\temp.tmp", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file." & $file)
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Current DIR:", $line)
WEnd
FileClose($file)
FileDelete(@TempDir & "\temp.tmp")

Tried the following:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
ConsoleWrite("------------" & @CRLF)
ConsoleWrite("WorkingDir:" & @TAB & @WorkingDir & @CRLF)
ConsoleWrite("ScriptDir:" & @TAB & @ScriptDir & @CRLF)
ConsoleWrite("------------" & @CRLF)

Ran it from CMD:

D:\applicat\Test\Query>test2
------------
WorkingDir:     D:\applicat\Test\Query
ScriptDir:      C:\WINDOWS\system32
------------

Seems I don't get the problem?

:graduated:

Regards,

Hannes

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

1. Change consolewrite to msgboxes - you need to use compiled executable

2. Lets say it is called test.exe - store it to c:\windows

3. Open CMD it should point to c:\documents and settings\your profile dir

4. Now type c:\windows\test.exe and press ENTER

You will get "c:\windows" in your message boxes and you want to get "c:\documents and settings\your profile dir"

This is the problem. And the solution is right in this topic.Try it.

Link to comment
Share on other sites

1. Change consolewrite to msgboxes - you need to use compiled executable

2. Lets say it is called test.exe - store it to c:\windows

3. Open CMD it should point to c:\documents and settings\your profile dir

4. Now type c:\windows\test.exe and press ENTER

You will get "c:\windows" in your message boxes and you want to get "c:\documents and settings\your profile dir"

This is the problem. And the solution is right in this topic.Try it.

Juvigy,

1. This is a compiled executable.

2. This example is located in c:\windows\system32 (named test2.exe).

3. See my example. It has been run from "d:\applicat\test\query" So there is for sure no test2.exe in there

4. Even If I use the full path to execute it it works...

D:\applicat\Test\Query>c:\WINDOWS\system32\Test2.exe
------------
WorkingDir:     D:\applicat\Test\Query
ScriptDir:      C:\WINDOWS\system32
------------

Changing from ConsoleWrite to MsgBox doesn't change it. (See line 3: #AutoIt3Wrapper_Change2CUI=y ... in my source)

Please test it again.

Regards,

Hannes

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Well I did and must admit some people does not test properly and it seems myself included. I just assumed because someone else stated something that it is true and tried to find a solution for it.

@WorkingDir seems now all of a sudden to work as it is suppose to!

tx

Link to comment
Share on other sites

Hi.

@WorkingDir seems now all of a sudden to work as it is suppose to!

Not for me, tripple-checked that, really... :graduated:

I <edit> inserted a working function to my first posting starting this thread.

Thanks for all the suggestions, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

  • 3 years later...

I know this thread is a few days old, or a couple of day.

But is there a way to also get the filename, and not only the path, it were executed from?

If I compile a file named: c:test.exe : shellexecute(@desktopdir & "file.exe") 

Then I would like file.exe to return c:test.exe. I'm asking since the file name won't always be "test.exe"

code:

RunWait(@ComSpec & " /c %SystemDrive%&&cd %temp%&&echo %cd%>temp.tmp", "", @SW_hide); create temp file to save %cd%
$file = FileOpen(@TempDir & "\temp.tmp", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file." & $file)
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Current DIR:", $line)
WEnd
FileClose($file)
FileDelete(@TempDir & "\temp.tmp")
Edited by legend
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...