Improving a simple code snippet

Get help with using AutoHotkey and its commands and hotkeys
Billykid
Posts: 33
Joined: 16 Sep 2019, 08:59

Improving a simple code snippet

17 Nov 2020, 11:49

Hallo guys,
please take a short look at my code snippet.

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := []
F := str
Loop % strSplit(str, "\").length() - 1
{
  F := substr(str,1,InStr(F, "\",0,0) - 1)
  d[A_Index] := F  
}
Loop % d[A_Index].length()
MsgBox % d[A_Index]

How can I do this better?
Like always, alternative Code is also more than welcome.
Thanks for your help!
User avatar
mikeyww
Posts: 3964
Joined: 09 Sep 2014, 18:38

Re: Improving a simple code snippet

17 Nov 2020, 12:16

Just another way here.

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := []
While RegExMatch(str, "O)\\", m, m ? m.Pos + m.Len : 1)
 d[A_Index] := SubStr(str, 1, m.Pos - 1)
d.Push(str)
Loop % d.Count()
 MsgBox % d[A_Index]
User avatar
boiler
Posts: 6631
Joined: 21 Dec 2014, 02:44

Re: Improving a simple code snippet

17 Nov 2020, 12:23

Another:

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := []
loop
	d.Push(str := RegExReplace(str, "^(.*)\\.*", "$1"))
until !InStr(str, "\")

for k, v in d
	MsgBox, % v
Made mine match the output of: (note the fix at the end)

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := []
F := str
Loop % strSplit(str, "\").length() - 1
{
  F := substr(str,1,InStr(F, "\",0,0) - 1)
  d[A_Index] := F  
}
Loop % d.length()
MsgBox % d[A_Index]
Rohwedder
Posts: 3782
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Improving a simple code snippet

17 Nov 2020, 12:49

Hallo,
my versions:

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := [], F := str
While, InStr(F,"\")
	d.Push(F:=RegExReplace(F, "\\[^\\]*$"))
Loop,% d.Count()
	MsgBox,% d[A_Index]
Or without RegEx:

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := [], F := str
While, Pos:=InStr(F, "\",,0)
	d.Push(F:=SubStr(F, 1, Pos-1))
Loop,% d.Count()
	MsgBox,% d[A_Index]
User avatar
mikeyww
Posts: 3964
Joined: 09 Sep 2014, 18:38

Re: Improving a simple code snippet

17 Nov 2020, 13:25

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := [], F := str "\"
While pos := InStr(F, "\")
 d.Push(Trim(all .= "\" SubStr(F, 1, pos-1), "\")), F := SubStr(F, pos+1)
For k, v in d
 MsgBox,, %k%, %v%
User avatar
flyingDman
Posts: 880
Joined: 29 Sep 2013, 19:01

Re: Improving a simple code snippet

17 Nov 2020, 14:37

I guess the original OP script should have been:

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
d := []
F := str
Loop % strSplit(str, "\").length() - 1
	{
	F := substr(str,1,InStr(F, "\",0,0) - 1)
	d[A_Index] := F  
	}
Loop % d.length()                              ;<<<<<<
	MsgBox % d[A_Index]
Now try this:

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
while str~="\\"  {
	 splitpath, str,, str
	 msgbox % str
	 }
User avatar
mikeyww
Posts: 3964
Joined: 09 Sep 2014, 18:38

Re: Improving a simple code snippet

17 Nov 2020, 15:07

I'm not able to top that one!
User avatar
flyingDman
Posts: 880
Joined: 29 Sep 2013, 19:01

Re: Improving a simple code snippet

17 Nov 2020, 18:53

Or this:

Code: Select all

str := "C:\MyFiles\AHK All\AHK 07.03.20\B FolderAndFiles"
StrReplace(str, "\",  , cnt)
loop % cnt
	msgbox % substr(str,1,instr(str,"\",,,cnt-a_index+1)-1)
User avatar
mikeyww
Posts: 3964
Joined: 09 Sep 2014, 18:38

Re: Improving a simple code snippet

17 Nov 2020, 19:35

Another amazing one! Thanks for sharing it.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], effel, Google [Bot], grossermanitu, HiSoKa and 48 guests