rudi Posted November 9, 2010 Share Posted November 9, 2010 (edited) 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 November 15, 2010 by rudi Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
Juvigy Posted November 9, 2010 Share Posted November 9, 2010 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 More sharing options...
UEZ Posted November 9, 2010 Share Posted November 9, 2010 (edited) Maybe with @ScriptDir? Br, UEZ Edited November 9, 2010 by UEZ may-li 1 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 More sharing options...
Moderators Melba23 Posted November 9, 2010 Moderators Share Posted November 9, 2010 rudi, Something similar was discussed at length here. I hope it might shed some light. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
rudi Posted November 10, 2010 Author Share Posted November 10, 2010 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. 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 More sharing options...
Juvigy Posted November 10, 2010 Share Posted November 10, 2010 Did you try what i suggested ? It works ! Link to comment Share on other sites More sharing options...
hannes08 Posted November 10, 2010 Share Posted November 10, 2010 Hi rudi, try this: MsgBox(0,"test",@ScriptDir & @CRLF & @WorkingDir) I think the second macro (@WorkingDir) will lead you to the right way! Regards, Hannes AndrewSchultz and atetester132 2 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
Juvigy Posted November 10, 2010 Share Posted November 10, 2010 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 More sharing options...
JoHanatCent Posted November 11, 2010 Share Posted November 11, 2010 (edited) 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 November 11, 2010 by JoHanatCent Link to comment Share on other sites More sharing options...
hannes08 Posted November 11, 2010 Share Posted November 11, 2010 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? Regards, Hannes Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
Juvigy Posted November 11, 2010 Share Posted November 11, 2010 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 More sharing options...
hannes08 Posted November 11, 2010 Share Posted November 11, 2010 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 More sharing options...
JoHanatCent Posted November 11, 2010 Share Posted November 11, 2010 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 More sharing options...
hannes08 Posted November 11, 2010 Share Posted November 11, 2010 Thanks, just thought about going mad... Hannes Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
Juvigy Posted November 11, 2010 Share Posted November 11, 2010 Aahh i see now - you need to run it as another user. The problem appears when another user starts the exe from the cmd Link to comment Share on other sites More sharing options...
rudi Posted November 15, 2010 Author Share Posted November 15, 2010 Hi.@WorkingDir seems now all of a sudden to work as it is suppose to!Not for me, tripple-checked that, really... 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 More sharing options...
legend Posted February 12, 2014 Share Posted February 12, 2014 (edited) 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 February 12, 2014 by legend Link to comment Share on other sites More sharing options...
Danp2 Posted February 12, 2014 Share Posted February 12, 2014 Take a look at the @ScriptName macro reference. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
legend Posted February 12, 2014 Share Posted February 12, 2014 I don't thin that helps me in this case Link to comment Share on other sites More sharing options...
allSystemsGo Posted February 12, 2014 Share Posted February 12, 2014 I know this thread is a few days old, or a couple of day. Try four years. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now