Can AHk guess a file directory using the first characters? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Can AHk guess a file directory using the first characters?

14 May 2019, 18:21

Hi everyone. I built a script that can access me into the web directory. We have lots of folders and files, this makes it easy.

And it works!!

but sometimes, people make a typo in the datasheets where I'm pulling the information from, so there is a mismatch in directory path, soo it fails. Obviously.

My question is:

1. Could I get Ahk, to guess the folder based on the first 10-15 characters? (in case there's a typo in the middle)

2. could I add a custom error box, if the directory is off, instead of the normal really long error box that comes up when AHK script isn't working. Something like (Please change the directory to match, and watch for typos) would be a lot more intuitive
for my co-workers instead of the regular error box that has too much technical information.

This is my Script

Code: Select all

!x::
inputbox, userinput, Enter Full Project ID, , ,300 ,100
clipboard = %userinput%
No := SubStr(userinput, 1, 4)
run, B:\Webcam - Projects\%no%\%clipboard%\
return
And Here's an example of what a file looks like

TX07 - 52410 - Orange County Department of Paperclips - SAB North University - 15022 Regular Street Name Ave

Any help would be much appreciated.
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Can AHk guess a file directory using the first characters?

14 May 2019, 23:49

FileExist()
https://www.autohotkey.com/docs/commands/FileExist.htm

Super Fast and Accurate string distance algorithm
http://www.autohotkey.com/forum/topic59407.html

캡처.PNG
캡처.PNG (4.98 KiB) Viewed 2902 times
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Re: Can AHk guess a file directory using the first characters?

15 May 2019, 12:25

Klarion wrote: FileExist()
https://www.autohotkey.com/docs/commands/FileExist.htm

Super Fast and Accurate string distance algorithm
http://www.autohotkey.com/forum/topic59407.html

캡처.PNG
I will check this and start reading. thank you.
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Can AHk guess a file directory using the first characters?

15 May 2019, 12:55

You could also use a file loop using the first 10-15 characters and a wildcard (*).
- https://www.autohotkey.com/docs/commands/LoopFile.htm
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Re: Can AHk guess a file directory using the first characters?

15 May 2019, 17:29

awel20 wrote:
15 May 2019, 12:55
You could also use a file loop using the first 10-15 characters and a wildcard (*).
- https://www.autohotkey.com/docs/commands/LoopFile.htm
How would I go about this? That solution has me a bit stumped.

Could you maybe provide an example?

This is what the format of the folders in the file directory always looks like

TX07 - 52410 - Orange County Department of Paperclips - SAB North University - 15022 Regular Street Name Ave

4 char code - 5 char code - Name of company - location - address

So If I could use the first 12-25 Characters it should always find the path. 5 character string is always unique as well as business name. we have no duplicates.

If you could start a script Idea I might be able to finish the rest. Thanks in Advance!


UPDATE: I've been reading about If FileExist command for the past cpl hours. getting closer I think.
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Can AHk guess a file directory using the first characters?  Topic is solved

16 May 2019, 09:43

Code: Select all

!x::
	inputbox, userinput, Enter Full Project ID, , ,300 ,100
	; If the user pressed cancel or the string is too short
	if (ErrorLevel || StrLen(userinput) < 15) 
		return
	FoundFile := ""
	Loop, Files, % "B:\Webcam - Projects\" SubStr(userinput, 1, 4) "\" SubStr(userinput, 5, 15) "*", D
	{
		FoundFile := A_LoopFileFullPath
		break
	}
	if (FoundFile != "")
		Run, % FoundFile
return
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Can AHk guess a file directory using the first characters?

16 May 2019, 12:53

simple testing..

Code: Select all

#SingleInstance, Force	
#WinActivateForce			
SetBatchLines, -1	
myVar =
(
TX07 - 52411
CA07 - 52412
WA07 - 52413
)
Gui, 메인창:  New,, % "      testing... "  
Gui, 메인창:  Add, Text, y+15 											, % myVar
Gui, 메인창:  Add, Text, y+15 											, your Input  :
Gui, 메인창:  Add, Edit, x+7 yp-3 w400 v입력, CA
Gui, 메인창:  Add, Button, w400 -Tabstop Default g단추	, Go  (Click/Enter)
Gui, 메인창:  Add, Text, x10  v표시부 , % "                                                                   "    	
Gui, 메인창:  Show
GuiControl, 메인창:  Focus, 타겟
Send, {End}
Return
메인창GuiClose:
메인창GuiEscape:
Soundbeep, 100, 200
ExitApp
단추:
Gui, 메인창:  Submit, NoHide
myFound := False
Loop, Parse, myVar, `n, `r
	If(A_LoopField == 입력)
		myFound := True, Break
If(myFound)
{
	Loop, 7
		Soundbeep, A_Index * 1111, 44
	myShow := "Good !        " 입력
}
Else
{
	Soundbeep, 333, 55
	Soundbeep, 333, 55
	myMax =
	Loop, Parse, myVar, `n, `r
	{
		myPercent := RegExReplace((1 - 편집거리(입력, A_LoopField)) * 100, "\..*$")
		If(myPercent > myMax)
		{
			myMax := myPercent
			myShow := "How about ?   " A_LoopField
		}
	}
}
GuiControl, 메인창: , 표시부, % myShow 
Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
편집거리(string1, string2, maxOffset := 1)    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  By Toralf:
;  Super Fast and Accurate string distance algorithm: Sift4
;  https://siderite.blogspot.com/2014/11/super-fast-and-accurate-string-distance.html                  
;  basic idea for SIFT3 code by Siderite Zackwehdex 
;  http://siderite.blogspot.com/2007/04/super-fast-and-accurate-string-distance.html 
;  took idea to normalize it to longest string from Brad Wood 
;  http://www.bradwood.com/string_compare/ 
;  Own work: 
;   - when character only differ in case, LSC is a 0.8 match for this character 
;   - modified code for speed, might lead to different results compared to original code 
;   - optimized for speed (30% faster then original SIFT3 and 13.3 times faster than basic Levenshtein distance) 
;  http://www.autohotkey.com/forum/topic59407.html 
{
	If (string1 = string2) 
		Return (string1 == string2 ? 0 / 1 : 0.2 / StrLen(string1))     ;   either identical or (assumption:) "only one" char with different case 
	If (string1 = ""  OR  string2 = "") 
		Return (string1 = string2 ? 0/1 : 1/1) 
	StringSplit, n, string1 
	StringSplit, m, string2 
	ni  	:= 1
	mi		:= 1
	lcs	:= 0 
	While((ni <= n0 ) AND (mi <= m0)) 
	{ 
		If (n%ni% == m%mi%) 
			EnvAdd, lcs, 1 
		Else If (n%ni% = m%mi%) 
			EnvAdd, lcs, 0.8 
		Else
		{ 
			Loop, % maxOffset
			{ 
				oi := ni + A_Index
				pi := mi + A_Index 
				If ((n%oi% = m%mi%) AND (oi <= n0))
				{ 
					ni := oi
					lcs += (n%oi% == m%mi% ? 1 : 0.8) 
					Break 
				} 
				If ((n%ni% = m%pi%) AND (pi <= m0))
				{ 
					mi := pi
					lcs += (n%ni% == m%pi% ? 1 : 0.8) 
					Break 
				} 
			} 
		} 
		EnvAdd, ni, 1 
		EnvAdd, mi, 1 
	} 
	Return ((n0 + m0)/2 - lcs) / (n0 > m0 ? n0 : m0) 
}
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Can AHk guess a file directory using the first characters?

16 May 2019, 13:57

Klarion wrote:
14 May 2019, 23:49
FileExist()
https://www.autohotkey.com/docs/commands/FileExist.htm

Super Fast and Accurate string distance algorithm
http://www.autohotkey.com/forum/topic59407.html


캡처.PNG
Assuming the user input is the "Source", how do you know what the "Target" is?
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Re: Can AHk guess a file directory using the first characters?

16 May 2019, 18:27

awel20 wrote:
16 May 2019, 13:57
Klarion wrote:
14 May 2019, 23:49
FileExist()
https://www.autohotkey.com/docs/commands/FileExist.htm

Super Fast and Accurate string distance algorithm
http://www.autohotkey.com/forum/topic59407.html


캡처.PNG
Assuming the user input is the "Source", how do you know what the "Target" is?
Thanks for all the help!

I got through PHASE 1. Using file exist: Now it finds if directory is exact and it takes me to territory folder if folder directory isn't exact.

Code: Select all

;File Directory retriever
!x::
inputbox, userinput, Enter Full Project ID, , ,300 ,100
if ErrorLevel cancel
	Return
clipboard = %userinput%
No := SubStr(userinput, 1, 4)
IfExist, B:\Webcam - Projects\%no%\%clipboard%\
Run, B:\Webcam - Projects\%no%\%clipboard%\
else {
  MsgBox, Folder Name not found, Possibly folder has a different name? Opening Territory Folder
  Run, B:\Webcam - Projects\%no%\
}
return
I will be reading and experimenting over the next couple of days, how this loop code you send me works. Once I understand how it's working i should be able to use it.

also to answer your question. About user input. Seems sometimes people who are inputting the information are forgetting to put the street at the end or sometimes they'll use a semi different address at the end, but it's always 95% at the end. Territory and project code always seem to be correct.
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Can AHk guess a file directory using the first characters?

16 May 2019, 18:43

I hope the code I posted helps.
eblanc wrote:
16 May 2019, 18:27
also to answer your question. About user input.
That questioln was directed at Klarion. I was basically saying that his suggestions won't work, because you know the user input but you don't know what file you are looking for. So there is nothing to compare the user input to.

Te functions he posted are for comparing two strings. But you don't know what one of those two strings is so there is no way to compare them.
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Can AHk guess a file directory using the first characters?

16 May 2019, 21:21

@awel20
I am Not talking to you
I am Not interested in you and your codes at all
Mind your own codes
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Re: Can AHk guess a file directory using the first characters?

20 May 2019, 14:09

Hi @awel20 ,

Tried the cod you sent me. I understand some of it. I've tried running the script but I'm not seeing anything happen. I'm gonna do more reading on each command. But do you have any Idea why?

I'm also new to the syntax you're using:

Like using only 1 spaced percent sign, The D at the end of path directory (I'm guessing it's an option), and A_LoopFileFullPath. That's where my knowledge gap starts I think.

I'm gonna start reading, but if you care to explain, why you are using those I might have a stronger understanding.

Thanks Awel!
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Re: Can AHk guess a file directory using the first characters?

20 May 2019, 15:52

I got it to work now! Thanks @awel20 , I just had to change the substring. to 1 for the directory to work properly

Code: Select all

!z::
    inputbox, userinput, Enter Full Project ID, , ,300 ,100
    ; If the user pressed cancel or the string is too short
    if (ErrorLevel || StrLen(userinput) < 15) 
        return
    FoundFile := ""
    Loop, Files, % "B:\Webcam - Projects\" SubStr(userinput, 1, 4) "\" SubStr(userinput, 1, 15) "*", D
    {
		
        FoundFile := A_LoopFileFullPath
        break
    }
    if (FoundFile != "")
        Run, % FoundFile
return
also want to say thanks to @Klarion , because He prompted me to read on fileexist command, which enabled me to build a simpler version of this code at start, and also I will be using for future projects.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 120 guests