Page 1 of 1

[Solved] Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 06:47
by dipstick5000
I'm using Pulover's Macro Creator, and it has no option to use InStr. I realize I'm not supposed to be using StringGetPos anymore, but I must find the position of a character. StringGetPos works perfectly every time I don't use it in a loop, but It always returns -1 in a file loop.

I posted this in the Macro Creator thread, but then I thought that maybe it's not a Macro Creator issue. Is it just an AHK issue?

What would you do if you didn't want to start over and code manually?

Code: Select all

filelist := ""
clipboard := ""
counter := 0
resourcefolder := gamepath "\resources"
Loop, Files, C:\Users\gameb\AppData\Roaming\Stencyl\stencylworks\stencylworks\games\01 Resource Test\resources\*.*, F
{
    StringGetPos, poshyphen, A_LoopFileName, -, L12
    MsgBox, 0, , 
    (LTrim
    file name =
    %A_LoopFileName%
    
    hyphen position =
    %poshyphen%
    )
    filelist := 
    (LTrim
    filelist "
    " A_LoopFileName
    )
    counter += 1
}
[Mod Edit: [code][/code] tags added]
File Loop.png
File Loop.png (19.68 KiB) Viewed 398 times

Re: Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 09:23
by TLM
Using InStr()

Code: Select all

filelist := ""
clipboard := ""
counter := 0
resourcefolder := gamepath "\resources"
Loop, Files, C:\Users\gameb\AppData\Roaming\Stencyl\stencylworks\stencylworks\games\01 Resource Test\resources\*.*, F
{
	poshyphen := InStr( A_LoopFileName, "-" )
    MsgBox file name = %A_LoopFileName%`n`nhyphen position = %poshyphen% 
}
To use the resourcefolder variable using an expression, only detect file names with hyphens, assign the position to a variable and display everything using an expression:

Code: Select all

filelist := ""
clipboard := ""
counter := 0
resourcefolder := gamepath "\resources"
Loop, Files, % resourcefolder "\*.*", F
{
	if ( poshyphen := InStr( A_LoopFileName, "-" ) )
	{
	    MsgBox % "file name = " A_LoopFileName "`n`nhyphen position = " poshyphen
	}
}

Re: Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 10:35
by just me

Code: Select all

StringGetPos, poshyphen, A_LoopFileName, -, L12
are you sure about L12?

Re: Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 15:01
by dipstick5000
just me wrote:
23 Jan 2020, 10:35
[/code]are you sure about L12?
No I'm not sure, but I was experimenting. The result is always -1, no matter if it's L12 or L-anything or left blank.

Re: Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 16:14
by dipstick5000
TLM wrote:
23 Jan 2020, 09:23
Using InStr()
TLM, first of all, thank you very much. I appreciate your knowledge and time. But I wasn't asking how to use InStr, I was asking what you would do if you couldn't. There is no InStr in Macro Creator. Instead it uses the outdated StringGetPos.

“Deprecated: This command is not recommended for use in new scripts. Use the InStr function instead.”

Maybe I should have mentioned a couple things, but I tend to be too long, so I was trying to avoid that:

1) The goal of this project is to end up making an executable (exe) file.
2) I can ultimately get the result I want without StringGetPos or InStr, but it's fairly ridiculous, opening Notepad, putting the file list (filelist variable) in it, having the user select, or highlight a number in a file name, in an out of order file list, possibly with several thousand files. Sort doesn't work for the file names I'm using, they are non standard and don't show up in an easily sort-able list. They're listed like this:

[email protected]
[email protected]
[email protected]
11.fnt
11.png
111-0.png
[email protected]

I'm trying to find the greatest number that comes before they hyphen, or period if there is no hyphen.

3) The lines of script I'm working on are in the middle of the bunch of script.

So if I understand correctly, I've got to figure out a way to use InStr, and until I hear different I should just continue assuming StringGetPos will not work in a file loop. Or at least it won't work with A_LoopFileName. I hope someone can tell me I'm wrong on this assumption, and I'm doing something wrong in the script.

So am I correct, that other than scripting the rest of the project manually, my only solution is to?:

1) Continue to code the rest of the project in Macro Creator,
2) Leave out the part in the middle I'm having trouble with, while figuring out some kind of work around for testing purposes,
3) When finished in Macro Creator, edit the script manually using InStr,
4) Test it by making it an exe, since I can't test it with Macro Creator.

I hope you can tell me that's not my last option.

Or does anyone know of an acceptable AHK gui creator that uses InStr? Do I just have to try them all until I find one that does?

Anyway, thanks again for your help.

Re: Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 22:32
by dipstick5000
Okay I figured it out. The solution was quite simple. It dawned on me that the problem may not lie with StringGetPos being inside file loops. I thought maybe it had something to do with it not working with A_LoopFileName. When I set a variable equal to A_LoopFileName and used it instead, it works perfectly.

Sigh... so much time and frustration wasted on something so simple. I guess it's part of the learning process.

Loop, Files, %resourcefolder%\*.*, F
{
newname := A_LoopFileName
StringGetPos, poshyphen, newname, -
}

Re: [Solved] Is it possible to use StringGetPos in a file loop?

Posted: 23 Jan 2020, 22:52
by gregster
It seems quite unlikely that this would change anything in this loop or StringSplit.

But A_LoopFileName will be empty outside this loop, newname will still have its last assigned value.
So, there might be a reason to use newname, but it shouldn't have to do with StringSplit.
dipstick5000 wrote:
23 Jan 2020, 22:32
Loop, Files, %resourcefolder%\*.*, F
{
newname := A_LoopFileName
StringGetPos, poshyphen, newname, -
}
In this loop, you are not checking the value of poshyphen at all. So if you check it after the loop, it will contain whatever the value was for the last file it checked (and it will check all files in this folder).
If the last checked file name did contain a hyphen, poshyphen will hold its position, otherwise -1. Well, at least newname should still hold the name of this last checked file, A_LoopFileName should be empty by then.

That L12 that you had there before, is much more likely to be the culprit for getting -1, which means "not found".
https://www.autohotkey.com/docs/commands/StringGetPos.htm#Parameters wrote:Ln: The search will start looking at the left side of InputVar and will continue rightward until the nth match is found.
You were looking for the 12th match from the left.

Of course, there might have been other versions with other problems, which we didn't see.