Instr() DOES NOT WORK, ALWAYS TRUE

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
letacio
Posts: 48
Joined: 08 Mar 2018, 16:05

Instr() DOES NOT WORK, ALWAYS TRUE

05 May 2019, 13:32

Hello there!

Im for more then 12 hours trying lots of combinations of sintaxes and functions and im getting mad.

What i have: a list of items in a text file (FILE A)
example: 111220,32302,3020203,1203030,1110,443403

each one of this have a wav file that should be downloaded to a folder.

What i want: after downloading, to check if all numbers in FILE A have been downloaded.

what i do: i read file, parse it with commas, and for each loopfield, i loop again for all files in the destiny folder, so see if any matches are done. if it matches, nice, if doesnt, it ads the number which did not match to a string.

to match it, i use instr(). but it ALWAYS matches, and im crazy with it.

my code

(...)

FileRead, OutputVar, %text_file% ;reads file to variable
StringReplace, OutputVar,OutputVar, `r,, All
StringReplace, OutputVar,OutputVar, `n,, All


loop, parse, OutputVar, `, ;list of files that will be downloaded
{
findit=0
ID_segment:=A_LoopField ;gets each id segment to use

Loop, %destiny%\*.*wav ; list of files already on the folder
{
filename:=A_LoopFileName

If InStr(filename, %A_LoopField%) ;if id segment inside filename, means the the file was downloaded -> ON THIS LINE IVE TRIED ALL COMBINATIONS OF %%, "", AND STUFF. DOES NOT WORK, IT EITHER ALWASYS ENTER, OR ALWAYS GO THROUGH
{
findit=1
break ;find once and does not need to check anymore the rest of files
}

}

if(findit==0) ;if came here with 0, means didnt find
{

missinglist:= ID_segment "," missinglist ;didnt find, include on list
}
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Instr() DOES NOT WORK, ALWAYS TRUE

05 May 2019, 13:42

assuming u dont have tautologies/contradictions in ur logic(havent checked and not going to):

Code: Select all

If InStr(filename, %A_LoopField%)
ure double dereferencing A_LoopField. if u dont know what that means: read https://www.autohotkey.com/docs/Variables.htm#ref. dont use %%
User avatar
ThumpieBunnyEve
Posts: 109
Joined: 03 Aug 2016, 19:03

Re: Instr() DOES NOT WORK, ALWAYS TRUE

05 May 2019, 13:45

what i am describeing is just a general rule of practice i use for checking my own work when im lost on variable outputs but:
what is an example output if you insert the message box below?

/////
Loop, %destiny%\*.*wav ; list of files already on the folder
{
filename:=A_LoopFileName
msgbox in %A_LoopFileName% looking for %A_LoopField%
If InStr(filename, %A_LoopField%)
////
~not a fix, just a way to be sure your getting what you want out of the vars and the way your using them.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Instr() DOES NOT WORK, ALWAYS TRUE

05 May 2019, 17:04

You'd want to drop the %s:
if InStr(filename, A_LoopField)
Although there may still be other problems with the script.

Here are two ideas. I generally prefer the array approach.

Code: Select all

q:: ;check if files exist (use comma-separated list)
vNeedles := "1,22,333,4444,55555" ;files to search for
vFiles := "22,4444" ;files that exist

vNeedles := "," vNeedles ","
Loop, Parse, vFiles, % ","
	vNeedles := StrReplace(vNeedles, "," A_LoopField ",", ",")
vNeedles := Trim(vNeedles, ",")
MsgBox, % "files not found:`r`n" vNeedles
return

w:: ;check if files exist (use array)
vNeedles := "1,22,333,4444,55555" ;files to search for
vFiles := "22,4444" ;files that exist

oArray := {}
Loop, Parse, vNeedles, % ","
	oArray[A_LoopField] := 0
Loop, Parse, vFiles, % ","
	if oArray.HasKey(A_LoopField)
		oArray[A_LoopField] := 1
vMissing := ""
for vKey, vValue in oArray
	if !vValue
		vMissing .= vKey ","
vMissing := SubStr(vMissing, 1, -1)
MsgBox, % "files not found:`r`n" vMissing
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Instr() DOES NOT WORK, ALWAYS TRUE

06 May 2019, 03:03

When inside of an expression or when using :=, you don't use %% around the variable.
Example Pseudo Code

Code: Select all

Variable = 123
NewVariable = %Variable%  ; this line and below are the same, just a different way to write it.
NewVariable := Variable  ; notice we don't need %%
if InStr(NewVariable, A_LoopField)  ; notice we don't need %%
You need to be clear about which is the needle and which is the haystack. You will always get a match, if the variables contains all the data (haystack), and you can't isolate something specific (needle). Use a msgbox to check exactly what the contents of the variables are, as a test to see if your code is working. If your variables contains all kinds of unwanted content, then that can be the reason why InStr() is not working the way that you want.

Code Snippet

Code: Select all

msgbox % filename
msgbox % A_LoopField
if InStr(A_LoopField, filename)
You might also want to check out in and contains. Contains is the equivalent of InStr, but in is more specific.

Code: Select all

if var in %MyItemList%  ; var does not need %% either, as recognized automatically as a variable, but the 2nd variable to the right will need %%
MsgBox %var% is in the list.
https://autohotkey.com/docs/commands/IfIn.htm

Another to check out is StrSplit
https://autohotkey.com/docs/commands/StringSplit.htm
Example

Code: Select all

FileNameRead = blue.wav
MsgBox % FileNameRead
FileName := StrSplit(FileNameRead, ".")
MsgBox % FileName[1]  ; FileName has become an array.  FileName[1] = blue, FileName[2] = wav

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CaseyMoon and 325 guests