Problem with passing parameters between scripts with a space in a variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Problem with passing parameters between scripts with a space in a variable

20 Aug 2018, 20:49

I've read through some documentation and some forum posts before deciding to post this, mostly because I'm experiencing an oddity that doesn't quite fit what I've read.

Calling Script:

Code: Select all

Record_Number := 155
DatabaseFilename := "C:\My databases\The Folder that I Want\The File That I want.xlxs"
AppendRecord := True ; Set to false to create a new record if it exists already
FileToParseForRecord := "X:\Network Folder 1\File Folder 103\Record Set.dat"

;A)
RunWait, C:\AutoHotKey Scripts\My Parsing Script.ahk %Record_Number% %DatabaseFilename% %AppendRecord% %FileToParseForRecord%

;B)
RunWait, C:\AutoHotKey Scripts\My Parsing Script.ahk %1% %2% %3% %4% %Record_Number% %DatabaseFilename% %AppendRecord% %FileToParseForRecord%

;C)
RunWait, C:\AutoHotKey Scripts\My Parsing Script.ahk %Record_Number% %DatabaseFilename% %AppendRecord% %FileToParseForRecord% 155 "C:\My databases\The Folder that I Want\The File That I want.xlxs" True "X:\Network Folder 1\File Folder 103\Record Set.dat"

Called Script:

Code: Select all

global
Record_Number := A_Args[1]
dFilename := A_Args[2]
aRecord := A_Args[3]
rFileName := A_Args[4]
MsgBox % "Database is supposed to be " dFilename 
If !FileExist(dFilename)
{
	MsgBox % dFilename " wasn't found."
}
When called using A) or B), both pass along only up to the first space in the variable (i.e. C:\My databases\The), but the whole variable with spaces gets passed along when I use the C) convention.

I've read through this and found:
Known limitation: dragging files onto a .ahk script may fail to work properly if 8-dot-3 (short) names have been turned off in an NTFS file system. One work-around is to compile the script then drag the files onto the resulting EXE.
which makes me scratch my head if this even applies since I can get the parameters to pass using C) above.

Just for giggles and grins, I've tried changing DatabaseFilename to an arbitrary filename that also has spaces, but when MsgBox displays that variable, I'm getting garbage characters preceding the "C:\"
Garbage.png
(1.55 KiB) Downloaded 246 times
. :headwall:

As for C) above, If I needed this to be more dynamic, how would I go about setting it up so that spaces work?
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Problem with passing parameters between scripts with a space in a variable

20 Aug 2018, 21:06

Hello.
Try this

Code: Select all

...
RunWait, % "C:\AutoHotKey Scripts\My Parsing Script.ahk" Record_Number " " DatabaseFilename " " AppendRecord " " FileToParseForRecord
...
(Without warranty :mrgreen: )

EDIT: For C.) you can try this

Code: Select all

RunWait, % "C:\AutoHotKey Scripts\My Parsing Script.ahk " Record_Number " " DatabaseFilename " " AppendRecord " " FileToParseForRecord " " 155 ""C:\My databases\The Folder that I Want\The File That I want.xlxs"" True ""X:\Network Folder 1\File Folder 103\Record Set.dat""
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Problem with passing parameters between scripts with a space in a variable

21 Aug 2018, 04:15

Code: Select all

Record_Number := 155
DatabaseFilename := "C:\My databases\The Folder that I Want\The File That I want.xlxs"
AppendRecord := True ; Set to false to create a new record if it exists already
FileToParseForRecord := "X:\Network Folder 1\File Folder 103\Record Set.dat"

cmd = C:\AutoHotKey Scripts\My Parsing Script.ahk "{}" "{}" "{}" "{}"
MsgBox % Format(cmd, Record_Number, DatabaseFilename, AppendRecord, FileToParseForRecord)
idk what u meant by %1%, %2%, ..., this has been superseded by A_Args since 1.1.27
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Problem with passing parameters between scripts with a space in a variable

21 Aug 2018, 05:51

swagfag wrote: idk what u meant by %1%, %2%, ..., this has been superseded by A_Args since 1.1.27
%1%, %2%, etc are not being used in the called script, but rather the calling script... they’re just placeholders for the variables being sent. They could be anything if I understand RunWait correctly.
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Problem with passing parameters between scripts with a space in a variable

21 Aug 2018, 05:55

divanebaba wrote:
EDIT: For C.) you can try this

Code: Select all

RunWait, % "C:\AutoHotKey Scripts\My Parsing Script.ahk " Record_Number " " DatabaseFilename " " AppendRecord " " FileToParseForRecord " " 155 ""C:\My databases\The Folder that I Want\The File That I want.xlxs"" True ""X:\Network Folder 1\File Folder 103\Record Set.dat""
I’m trying to get away from C) because it can’t be dynamically changed during runtime, but thank you nonetheless. :)
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Problem with passing parameters between scripts with a space in a variable

21 Aug 2018, 18:52

divanebaba wrote:Hello.
Try this

Code: Select all

...
RunWait, % "C:\AutoHotKey Scripts\My Parsing Script.ahk" Record_Number " " DatabaseFilename " " AppendRecord " " FileToParseForRecord
...
(Without warranty :mrgreen: )

EDIT: For C.) you can try this

Code: Select all

RunWait, % "C:\AutoHotKey Scripts\My Parsing Script.ahk " Record_Number " " DatabaseFilename " " AppendRecord " " FileToParseForRecord " " 155 ""C:\My databases\The Folder that I Want\The File That I want.xlxs"" True ""X:\Network Folder 1\File Folder 103\Record Set.dat""
For reasons I stated before about needing a dynamic setup, I didn't try your 'edit' suggestion. However, I did try your original suggestion, and likely the reason FOR your edit was because of a lack of a space between .ahk and the end quote... "C:\blah blah... .ahk" should be "C:\blah blah... .ahk ". I first tried your suggestion without the space and got an error pointing to it. I then added a space and no error was given. That being said, going without the space, the error dialog box indicated that the entire set of parameters were indeed being passed to the script correctly, which tells me this isn't so much of a passing issue as much as it is a receiving issue.

So... I guess that I need to amend my original post to ask for help in receiving parameters between scripts with a space in the variable.
User avatar
divanebaba
Posts: 804
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Problem with passing parameters between scripts with a space in a variable

21 Aug 2018, 23:43

I'm sorry TXShooter,

my english is not the best. I thought the "spaces" in your code are generating errors.
Thanks for pointing to the lack of space I left in your A.) code.

Nice greetings from Aachen, the nicest city in Germany. :mrgreen: :mrgreen:
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Problem with passing parameters between scripts with a space in a variable

22 Aug 2018, 18:11

How does one go about passing two full-path filenames (each being different from the other), which both contain spaces, between scripts? Using ANY method?? I'm at my wits end trying to get this to work dynamically.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Problem with passing parameters between scripts with a space in a variable

22 Aug 2018, 18:37

Code: Select all

; master.ahk
path1 := "one file path containing spaces"
path2 := "another file path containing spaces"

cmd = C:\slave.ahk "{}" "{}"
Run % Format(cmd, path1, path2)


; slave.ahk
MsgBox % A_Args[1]
MsgBox % A_Args[2]
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Problem with passing parameters between scripts with a space in a variable

24 Aug 2018, 16:03

swagfag wrote:

Code: Select all

; master.ahk
path1 := "one file path containing spaces"
path2 := "another file path containing spaces"

cmd = C:\slave.ahk "{}" "{}"
Run % Format(cmd, path1, path2)


; slave.ahk
MsgBox % A_Args[1]
MsgBox % A_Args[2]
Welp, that's a new one on me. I've not seen or used Format before. Thank you very much for that.

However, I can't seem to get !FileExist to work with that information, no matter how I write it. I've tried...

Code: Select all

Master.ahk :
	Record_Number := 155
	DatabaseFilename := "C:\My databases\The Folder that I Want\The File That I want.xlxs"
	AppendRecord := True ; Set to false to create a new record if it exists already
	FileToParseForRecord := "X:\Network Folder 1\File Folder 103\Record Set.dat"
	aFilename = C:\My AHK Files\Slave 3.0.ahk "{}" "{}" "{}" "{}"
	Run % Format(aFilename, Record_Number, DatabaseFilename, AppendRecord, FileToParseForRecord)

Slave.ahk
	global
	Record_Number := % A_Args[1]
	DatabaseFilenameN := % A_Args[2]
	DatabaseFilename = A_Args[2] ; Tried this just to see if the colon makes a difference. Still not finding the file.
	AppendRecord := % A_Args[3]
	FileToParseForRecord := % A_Args[4]
	; DatabaseFilename = "%DatabaseFilename%" ; Even tried this... no file found.
	MsgBox % DatabaseFilenameN " is the database file."
	If !FileExist("C:\My databases\The Folder that I Want\The File That I want.xlxs") ; THIS finds the File
		MsgBox % DatabaseFilenameN " 1 wasn't found." ; A) Does find the file
	If !FileExist(A_Args[4])
		MsgBox % DatabaseFilenameN " 2 wasn't found." ; B) Does not find the file 
	If !FileExist(DatabaseFilename)
		MsgBox % DatabaseFilenameN " 3 wasn't found" ; C) Doesn't find the file
	If !FileExist("DatabaseFilename")
		MsgBox % DatabaseFilenameN " 4 wasn't found" ; D) Doesn't find the file
	If !FileExist("%DatabaseFilename%")
		MsgBox % DatabaseFilenameN " 5 wasn't found" ; E) Doesn't find the file
	; If !FileExist("" %DatabaseFilename% "")
	;	MsgBox % DatabaseFilename " 6 wasn't found" ; F) Results in an Illegal Character error
	; If !FileExist(%DatabaseFilename%)
	;	MsgBox % DatabaseFilename " 7 wasn't found" ; G) Results in an Illegal Character error
:headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall: :headwall:


EDIT: My copy & paste fu was weak. Corrected. Hi-YA
Last edited by TXShooter on 24 Aug 2018, 19:34, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Problem with passing parameters between scripts with a space in a variable

24 Aug 2018, 16:40

check if the variable names ure passing around are correct, u have this:

Code: Select all

Run % Format(aFilename, Rec_Num, rPath, For_Radio, rFilename)
rPath is not initialized, rFilename is not initialized, in fact none of the variables uve declared coincide with what ure passing to Format()
also, forcing an expression when ure already in an expression is pointless: delete % from := %
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Re: Problem with passing parameters between scripts with a space in a variable

24 Aug 2018, 19:59

swagfag wrote:check if the variable names ure passing around are correct, u have this:

Code: Select all

Run % Format(aFilename, Rec_Num, rPath, For_Radio, rFilename)
rPath is not initialized, rFilename is not initialized, in fact none of the variables uve declared coincide with what ure passing to Format()
also, forcing an expression when ure already in an expression is pointless: delete % from := %
Corrected my posting mistakes, deleted the %... no change to the outcome.

The variables are passing correctly as they are displaying fine in the MsgBox call. I even went back and added another MsgBox to display all of the variables after the 'A_Args / expression' transfer, and every one is displaying correctly. To go even further, I bypassed all of this and just went with saving the variables in a temp file, opened the temp file in Slave.ahk, and even that is creating the same error or not finding the files.

As it pertains to validating a filename that has spaces... The only thing that seems to work with If !FileExist is a directly typed and quote-encapsulated filename. Any other form of variably induced action either causes an error or can't find the file.

Any other thoughts or ideas to validating a filename that would work as a variable that contains spaces after being passed from one script to another? Is there another way to do this?

(I haven't even tried to open and use these files yet... still stuck on making sure that they exist first.)
Gibbons
Posts: 93
Joined: 20 Sep 2016, 20:39

Re: Problem with passing parameters between scripts with a space in a variable

17 Sep 2018, 21:57

I'd be interested in revisiting this. I'm also trying to figure out how to deal with spaces in filenames.

In the past I just change the file name and replace "spaces" with "_" and then continue on......

Code: Select all

NewFileName := StrReplace(OldFileName, A_Space , "_")
FileMove, %OldFileName%, %NewFileName%
but I'd like a more versatile answer so I can deal with any file I encounter.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], peter_ahk, ShatterCoder and 149 guests