I have a curious problem. I have a function that takes a $srcDir and $destDir and $topDir $srcDir is in the format of \$topDir\subDir1\subDir2\subDir..n What I need is to append all subDir parts to the $destDir
My approach so far is to split-path until I reach $topDir and then append the resulting string to $destDir using join-path.
If no sub directories are appended to $destPath then the return is perfect.
If I append a path to $destPath then the return is $destPath $destPath
Here is the output in sample values
- srcIn: C:\path\topdir\
- destIn: \\server\path\
- destOut: \\server\path\
Now if I have subdirectories
- scrIn: C:\path\topdir\subpath\subpath1
- destIn: \server\path\
- destOut: \\server\path\subpath\subpath1 \\server\path\subpath\subpath1
Inside the function the path looks correct. There is no dbl of the destOut value. once I return from the function it has the double value.
How do I prevent this? I just wanted a simple function to obtain the sub directories and append to the destDir so I can preserve folder structure and move files to their appropriate directories.
Ty.
function GetSubDir
{
param(
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[string]$destDir,
[string]$topDir="Disk1"
)
$tmpPath = Split-Path $filePath -parent
$fileName = Split-Path $filePath -leaf
$tmp= Split-Path $filePath -leaf
while ($tmp -ne $topDir)
{
if (test-path $tmpPath -PathType container)
{
if ($subDir){$subDir = "$tmp\$subDir"}
else {$subDir = "$tmp\"}
}
else {$subDir = "$tmp"}
$tmp = Split-Path $tmpPath -leaf
$tmpPath = Split-Path $tmpPath -parent
}
$destPath = Join-Path $destDir $subDir
if (!(Test-Path $destPath)) {md $destPath}
if (Test-Path $destPath -PathType container)
#name is set in calling function
{$destPath = Join-Path $destPath $name}
return $destPath
}
($srcPath -replace ".*\\$topDir", "$destDir\\$topDir")...