Problem with If InStr() Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 10:25

Hello!
I have a problem with:

If InStr(var2, var1)

My vars and matches are like this:

var1 := a

var2 := a
b
e
aaaa
abab
ababab
ababab baba
aebaeb
bbbb
eeee

Second case is:

var1 := abab

var2 := a
b
e
aaaa
abab
ababab
ababab baba
aebaeb
bbbb
eeee

The second var (var2) as the Msgbox display it is a list right?
___Screenshot   0001.jpg
___Screenshot 0001.jpg (7.46 KiB) Viewed 1245 times
how can i match only this:

var1 := a

var2 := a
b
e
aaaa
abab baba
aebaeb
bbbb aaaa
eeee

Second case:

var1 := abab

var2 := a
b
e
aaaa
abab
ababab
ababab baba
aebaeb
bbbb
eeee

Please help.
Thank you!

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Problem with If InStr()

Post by mikeyww » 20 Jan 2022, 11:09

Post your actual script. Below is one way to assign variables to a continuation section.

Code: Select all

var2 =
(
a
b
e
aaaa
abab
ababab
ababab baba
aebaeb
bbbb
eeee
)
Explained: Continuation section

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 11:27

Here it is.
The base code is not mine, so i am going blind here.

Code: Select all

#NoEnv
#SingleInstance, force
SetBatchLines, -1

<!a::
folderpath		:=	"C:\Users\Stem75\Desktop\Clipboard_to_folder_name\"
;loop, Parse, folderpath
objWShell		:=	ComObjCreate("WScript.Shell")
objFSO 			:=	ComObjCreate("Scripting.FileSystemObject")
objFolder 		:=	objFSO.GetFolder(folderpath)
objSubFolders	:=	objFolder.SubFolders 

For folder in objSubFolders
    Folders .= Folder.name "`n"
ars := Folders
Folders := ""
ers := Clipboard ; Save the entire clipboard to a variable.

MsgBox % Folders
MsgBox % ars
MsgBox % ers


If InStr(ars, ers)
{
MsgBox, The string was found

If ( SubStr( Folders, 9, 4 ) + 1 > numb )
numb := SubStr( Folders, 9, 4 )
numb := SubStr("0000" (numb+1), -3)
StringReplace, Clip, Clipboard, `r`n, `n, all 

FileCreateDir, C:\Users\Stem75\Desktop\Clipboard_to_folder_name\%clipboard%%numb%

}
Else
{
MsgBox, The string was not found
FileCreateDir, C:\Users\Stem75\Desktop\Clipboard_to_folder_name\%clipboard%

}

Exit

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Problem with If InStr()  Topic is solved

Post by mikeyww » 20 Jan 2022, 12:43

Code: Select all

find = a
str =
(
aaaa
a
b
e
aaaa
abab baba
aebaeb
bbbb aaaa
eeee
)
MsgBox, 64, Position, % pos := lineInstr(str, find)

lineInstr(str, find) {
 Return Instr("`n" str "`n", "`n" find "`n")
}

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 13:58

Is this applicable to my script or is it theory?
should i replace something or paste it somewhere?

When i hit run it gives me 6 i don't understand how to use this, sorry.
But thank you for your replay. If you can give me a better example or explanation i'll be thankful because i am on baby steps here.

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Problem with If InStr()

Post by mikeyww » 20 Jan 2022, 14:09

I was actually working with your original post. Instead of Instr, you could try lineInstr, and put that function definition (lines 16-18) at the bottom of your script. If you have trouble, feel free to describe it here.

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 14:12

AAAAAAAHHH!!!

All right i get it. Thank you it works.
This is the script for anyone who wants to use it for creating folders from clipboard.

Code: Select all

#NoEnv
#SingleInstance, force
SetBatchLines, -1

<!a::
folderpath		:=	"C:\Users\Stem75\Desktop\Clipboard_to_folder_name\"
;loop, Parse, folderpath
objWShell		:=	ComObjCreate("WScript.Shell")
objFSO 			:=	ComObjCreate("Scripting.FileSystemObject")
objFolder 		:=	objFSO.GetFolder(folderpath)
objSubFolders	:=	objFolder.SubFolders 

For folder in objSubFolders
    Folders .= Folder.name "`n"
ars := Folders
Folders := ""
ers := Clipboard ; Save the entire clipboard to a variable.


MsgBox % ers
MsgBox % ars

If Instr("`n" ars "`n", "`n" ers "`n")
{
MsgBox, The string was found

If ( SubStr( Folders, 9, 4 ) + 1 > numb )
numb := SubStr( Folders, 9, 4 )
numb := SubStr("0000" (numb+1), -3)
StringReplace, Clip, Clipboard, `r`n, `n, all 

FileCreateDir, C:\Users\Stem75\Desktop\Clipboard_to_folder_name\%clipboard%%numb%

}
Else
{
MsgBox, The string was not found
FileCreateDir, C:\Users\Stem75\Desktop\Clipboard_to_folder_name\%clipboard%

}

Exit

User avatar
MrDodel
Posts: 96
Joined: 28 Apr 2021, 09:03
Location: Event Horizon

Re: Problem with If InStr()

Post by MrDodel » 20 Jan 2022, 15:08

Could you use %userprofile% rather than hardcoding Stem75 ?
So much universe, and so little time. GNU Sir Terry.

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 16:28

MrDodel wrote:
20 Jan 2022, 15:08
Could you use %userprofile% rather than hardcoding Stem75 ?
If you mean to replace it in the script personally i don't know but for the desktop itself you can use A_Desktop.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Problem with If InStr()

Post by boiler » 20 Jan 2022, 17:09

Yes, better to just use A_Desktop in place of all of C:\Users\Stem75\Desktop, but there is a built-in variable for just the username: A_UserName

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 17:40

boiler wrote:
20 Jan 2022, 17:09
Yes, better to just use A_Desktop in place of all of C:\Users\Stem75\Desktop, but there is a built-in variable for just the username: A_UserName
And can you use A_Desktop and add the \Clipboard_to_folder_name\ in the end?

or A_UserName and add the \Clipboard_to_folder_name\ in the end?

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Problem with If InStr()

Post by boiler » 20 Jan 2022, 18:33

Stem75 wrote: And can you use A_Desktop and add the \Clipboard_to_folder_name\ in the end?
Yes. Like this:

Code: Select all

folderpath		:=	A_Desktop "\Clipboard_to_folder_name\"

Stem75 wrote: or A_UserName and add the \Clipboard_to_folder_name\ in the end?
If you use this, then it would be:

Code: Select all

 folderpath		:=	"C:\Users\" A_UserName "\Desktop\Clipboard_to_folder_name\"

The biggest advantage to A_Desktop is it works no matter what drive the user might have his/her desktop folder located.

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 20 Jan 2022, 18:59

Hmmm!!! That was a lesson.

And a nice tip in the end. To tell you the truth, i never thought that someone would have the desktop in an other drive.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Problem with If InStr()

Post by Chunjee » 20 Jan 2022, 19:05

mikeyww wrote:
20 Jan 2022, 12:43

Code: Select all

lineInstr(str, find) {
	Return Instr("`n" str "`n", "`n" find "`n")
}
Would recommend a different argument order. lineInStr(haystack, needle) follows ahk conventions and order of importance.


For the OP I think you may want to learn arrays soon, as newline separated lists/strings aren't the easiest to work with.

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

array := ["aaaa", "a", "b", "e", "aaaa", "abab"]
msgbox, % A.includes(array, "abab")
; => true

msgbox, % A.indexOf(array, "abab")
; => 6
https://biga-ahk.github.io/biga.ahk/#/?id=includes
https://biga-ahk.github.io/biga.ahk/#/?id=indexof

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Problem with If InStr()

Post by boiler » 20 Jan 2022, 19:36

Chunjee wrote:
20 Jan 2022, 19:05
Would recommend a different argument order. lineInStr(haystack, needle) follows ahk conventions and order of importance.
That is the order he has.

User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Problem with If InStr()

Post by Stem75 » 21 Jan 2022, 10:42

I think i made it better.

Code: Select all

; Quick make folders (semi-sequential) from Clipboard - Stem75

#NoEnv
#SingleInstance, force
SetBatchLines, -1

MyLabel:
<!a::   ; shortcut key   ( alt+A )

folderpath := "C:\Test"
objWShell	:= ComObjCreate("WScript.Shell")
objFSO := ComObjCreate("Scripting.FileSystemObject")
objFolder := objFSO.GetFolder(folderpath)
objSubFolders := objFolder.SubFolders 

For folder in objSubFolders
    Folders .= Folder.name "`n"
    fff := Folders
    Folders := ""
    ccc := Clipboard
    nnnnnn := Min(SubStr( nnn, -3))   ; This i think you can also put it as SubStr( nnn, -3)

If nnnnnn is Number
   {
        numb := Format("{:04}", nnnnnn+1)       ; or SubStr("0000" (numb+1), -3)
        }
        Else
        {
        numb := 0000
   }

If Instr("`n" fff "`n", "`n" ccc "`n")
    {
        FileCreateDir, C:\Test\%clipboard%   %numb%
        nnn = %clipboard%   %numb%
        nnnnnn =
        numb =
        }
        Else
        {
        FileCreateDir, C:\Test\%clipboard%
        nnn = %clipboard%
        nnnnnn =
        numb =
    }

If Instr("`n" fff "`n", "`n" nnn "`n")
    {
        MsgBox 8213, Clipboard to quick Folder, %nnn%`nFolder allready exists
            IfMsgBox Retry
                Goto MyLabel
            IfMsgBox Cancel
                Exit
    }
    
Exit

Post Reply

Return to “Ask for Help (v1)”