Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gets the common parts in all paths
#1
Hi,
Is there an easy way to get the common parts of all paths? 
In the following example, I expect the output to be
C:\Users\Administrator\Desktop\Test0\DEF\

Thanks in advance for any advice and help
david
 
Quote: C:\Users\Administrator\Desktop\Test0\DEF\A
 C:\Users\Administrator\Desktop\Test0\DEF\B
 C:\Users\Administrator\Desktop\Test0\DEF\1.txt
 C:\Users\Administrator\Desktop\Test0\DEF\2.txt

Macro Macro2
Code:
Copy      Help
_s=
;C:\Users\Administrator\hello
;C:\Users\Administrator\hello\B
;C:\Users\Administrator\hello\C\a.bat
;C:\Users\Administrator\hello\B\1.txt
;C:\Users\Administrator\Desktop\Test0\DEF\A
;C:\Users\Administrator\Desktop\Test0\DEF\B
;C:\Users\Administrator\Desktop\Test0\DEF\1.txt
;C:\Users\Administrator\Desktop\Test0\DEF\2.txt

;Todo: Gets the common parts in all paths
;C:\Users\Administrator\hello
;C:\Users\Administrator\Desktop\Test0\DEF\
#2
Here is the Powershell code

Can anyone provide a simpler QM equivalent code?  Smile
 
Code:
Copy      Help
$s = @'
C:\Users\Administrator\hello
C:\Users\Administrator\hello\B
C:\Users\Administrator\hello\C\a.bat
C:\Users\Administrator\hello\B\1.txt
C:\Users\Administrator\Desktop\Test0\DEF\A
C:\Users\Administrator\Desktop\Test0\DEF\B
C:\Users\Administrator\Desktop\Test0\DEF\1.txt
C:\Users\Administrator\Desktop\Test0\DEF\2.txt
'@

$s = $s -split "\r?\n"
for ($i = 0; $i -lt $s.length; $i++)
{
    if (!(gv a 2>$null))
    {
        for ($j = 0; $j -lt $s[$i + 1].length; $j++)
        {
            if (($s[$i][0 .. $j] -join '') -ne ($s[$i + 1][0 .. $j] -join '')) { $a = $s[$i][0 .. ($j - 1)] -join ''; break }
        }
    }
    if ($s[$i + 1] -notmatch [regex]::Escape($a)) { $a; rv a }
}



Forum Jump:


Users browsing this thread: 1 Guest(s)