Page 1 of 1

Getting Working directory & concatenating 2 stringds

Posted: 30 Jul 2021, 17:15
by Bob8K
Hi,

Autohokey is essential for me, but I wrote only 4 scripts so far, mostly by example, so I do not know really its syntax like I know C for example.

I need 2 things:

1) A way to set a variable to the working directory of the Windows I clicked on. I don't want the executable directory, but the directory that getcwd() would return in C

2) I have parsed the title of the Window of my app, and got a %Filename% containing the name of the file the app (SubtiltEdit.exe) was called with.

Unfortunately, with the last release, if you use:

SubtiltEdit.exe d:\MySubs\test.srt

the windows title contains only test.srt, and I need to know "d:\MySubs". I could not find how in the help, a bit clumsy of my part since that API must exists.

If somebody could show the syntax to get that into %dir% it would be appreciated.

3) I need to know the syntax to concatenate them %Filename% and %dir% into a variable named for example FilenameFul.

I tried:

FilenameFull := %A_WorkingDir% & / & %FileName%"

to see if that works (%A_WorkingDir% is NOT what getcwd() would return), but this does not work, so this is not the correct way to concatenate 2 strings (actually 3, since I need a "/" in between).

Thanks in advance for any help.

Re: Getting Working directory & concatenating 2 stringds

Posted: 30 Jul 2021, 18:43
by mikeyww
AutoHotkey does not have its own commands to get command lines directly, but the following post may help show how to do it.

https://autohotkey.com/board/topic/8228-process-listfile-namescommand-lines/

For concatenation, see concatenation. The "traditional" method is demonstrated below.

Code: Select all

FileName = jim.txt
FilenameFull = %A_WorkingDir%\%FileName%
MsgBox, 64, Result, %FilenameFull%

Re: Getting Working directory & concatenating 2 stringds

Posted: 30 Jul 2021, 19:44
by Bob8K
Thanks, but I *think* I have something easier. I have to write it to test it.

1) Make a C++ console program which will read the command line from the windows handle using the Win API GetWindowModuleFileNameA() function and parse it to printf() the directory of the first argument. Say gcmdlfh.exe

2) use

hanlde := Winget, handle
dir := % ComObjCreate("WScript.Shell").Exec("cmd.exe /q /c d:\m1\cmd\gcmdlfh.exe" handle).StdOut.ReadAll() (could be %handle%, not sure)

3) concat using the dot (found it finally!)

FilenameFull := dir . "\" . FileName

So, a few easy lne of C++, and 3 lines of AutoHotkey

I post here the whole solution if it works.

Edit: GetWindowModuleFileNameA() is not the correct API. searching...

Found:
wmic process where "name like '%SubtitleEdit%'" get processid,commandline
CommandLine ProcessId
C:\Program Files\Subtitle Edit\subtitleedit.exe" "B:\prob\test.srt" 42332

I need to write a C++ program that will use wmic and output B:\prob\test.srt

Re: Getting Working directory & concatenating 2 stringds

Posted: 30 Jul 2021, 22:10
by Bob8K
The solution is simple for me, but impossible to give to others in detail.

I sell a commercial product, a REXX interpreter. I won't give any information about it, in order not to look like I am promoting it on this board, so please don't ask.

This interpreter has a unix() routine that uses CreateProcess() to pipe the output of any command to a new thread that saves the output of the command.

That makes simple to use wmic with a few lines of REXX:

parse arg prog
cmd ='C:\windows\system32\wbem\wmic.exe process where "name like ''%'prog'%''" get processid,commandline'
call unix cmd, foo
parse var foo.2 '"' foo '"' foo '"' cmdl '"'
say cmdl

I have a program, not commercially available because it defeats the protection, to compile this routine.

So, inside AutoHotkey, I just have 1 line to get the file being edited by Subtitle Edit:

Filename := % ComObjCreate("WScript.Shell").Exec("d:\m1\rcmd\Getcmdl.exe SubtitleEdit").StdOut.ReadAll()

where Getcmdl.exe is the compilation of my REXX program above.

This might also work without compilation using the rexx EXE but I'm too lazy to try, and would require for others purchasing my product. So, again, I'm not spamming about it, so don't ask. Even by PM.