function removes all symbolic links (like '\..\'

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

function removes all symbolic links (like '\..\'

24 Feb 2018, 05:16

hi i searching:
function removes all symbolic links (like '\..\'

thanks

update:
first prototype:

ToolTip,% RegExReplace("\a\..\b\b\.\c\","\\[^\\\n]+\\\.\."),1,1
Last edited by SL5 on 24 Feb 2018, 05:31, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: function removes all symbolic links (like '\..\'

24 Feb 2018, 06:24

Here's some RegEx code which is similar to yours, and an example with A_LoopFileLongPath. Btw is it just '\..\' or are there other things that need to be handled?

Code: Select all

;replace '\Subfolder\..\' with '\'
;C:\Users\me\AppData\Roaming\..\..\Documents
;                   ------------
;C:\Users\me\AppData\..\Documents
;           ------------
;C:\Users\me\Documents

q:: ;relative paths to absolute paths
vPath := "C:\Users\" A_UserName "\AppData\Roaming\..\..\Documents"
;vPath := "C:\Users\" A_UserName "\AppData\Roaming\..\..\..\Public"
;vPath := "C:\Users\" A_UserName "\AppData\Roaming\..\..\..\..\Windows"
vPath2 := vPath

while InStr(vPath, "\..\")
	;vPath := RegExReplace(vPath, "\\[^\\]+\Q\..\\E", "\") ;can fail e.g. if contains '\..\..\..\..\'
	vPath := RegExReplace(vPath, "\\[^\\]*[^\\.]\Q\..\\E", "\") ;assumes folder names cannot end with '.'
MsgBox, % vPath

Loop, Files, % vPath2, FD
	vPath2 := A_LoopFileLongPath
MsgBox, % vPath2
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: function removes all symbolic links (like '\..\'

26 Feb 2018, 08:07

update 26.02.2018 15:39
the Version with A_LoopFileLongPath works only for absoluthe path.
For e.g. this works not (never enters the loop.

Code: Select all

relativePath := "AppData\Roaming\..\Documents"
Loop Files, %relativePath%, DF
	MsgBox % A_LoopFileLongPath

update 26.02.2018 15:07:
I tried to remove the while and do it all with regex (was double faster). But i cant.
Your Version with InStr(vPath, "\..\") was much shorter.
I changed the code a little bit. May interesting vor you:

Code: Select all

if(1){
path =
(
C:\Users\me\AppData\Roaming\..\..\Documents
C:\Users\me\AppData\Roaming\..\..\.\Documents
C:\Users\me\AppData\Roaming\.\..\..\.\Documents
)
pathClean:=""
Loop,Parse,path,`n
{
  p := A_LoopField
  while(pLength <> StrLen(p)){
    pLength := StrLen(p)
    p := RegExReplace(p,"(\\[^\\]+\\\.\.)+")
  }
  pathClean .= p . "`n"
}
MsgBox,% pathClean 
Reload
}

Guest wrote:

Code: Select all

Loop Files, %relativePath%, DF
	MsgBox % A_LoopFileLongPath
Thanks dear Gust! Thats really clever :)
I like it because it's short and no new function needs to be declared. not so great maybe the intelligibility.
jeeswg wrote:Here's some RegEx code which is similar to yours, and an example with A_LoopFileLongPath. Btw is it just '\..\' or are there other things that need to be handled?
Thanks dear jeeswg :)
I learnd the:
"In a regular expression, all chars between the \Q and \E are escaped." (http://www.perlmonks.org/bare/?node_id=468052)
Nice :) it makes it more readable
Yes actually there eventually also some \.\ possible who i dont need.
jeeswg wrote:

Code: Select all

... 	vPath := RegExReplace(vPath, "\\[^\\]*[^\\.]\Q\..\\E", "\") ;assumes folder names cannot end with '.'
...
update 26.02.2018 14:30 show regex-Version is double faster:

Code: Select all

t1 := A_TickCount
  Loop,999
  {
    Loop Files, C:\Users\me\AppData\Roaming\..\Documents, DF
      v := A_LoopFileLongPath
  }
  t2 := A_TickCount
  result1 := A_LineNumber . ": " . t2 - t1 ; = 62
  
  t1 := A_TickCount
  Loop,999
  {
    v := "C:\Users\me\AppData\Roaming\..\Documents"
    while InStr(v, "\..\")
      v := RegExReplace(v,"\\[^\\]+\\\.\.")
  }
  t2 := A_TickCount
  result2 := A_LineNumber . ": " . t2 - t1 ; = 62
  Clipboard := result1 . "`n" . result2 
  MsgBox % Clipboard
log =
(
middle loong path:
9: 94
19: 62

very long path:
9: 188
19: 62

very short path:
9: 125
19: 62

9: 94
19: 31
)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], wilkster and 306 guests