获取Read-Host根文件夹的子文件夹名称

时间:2016-07-15 09:05:11

标签: powershell

我需要创建一个脚本,要求用户输入文件夹名称\ path $File。从$File获取子文件夹名称,例如$File\docs\ref\$host。然后将$host复制到新的网络共享,并创建该文件夹名称$host并将其内容粘贴到其中。

我无法复制$host的原因是因为$host名称与$File一样发生变化,但两者之间的子文件夹是相同的。 我希望这有道理吗?

这就是我所拥有的:

$file = Read-Host -Prompt 'What is your folder?'
# This is where I'm stuck
$host = Read-Host $file\docs\ref\$host
Test-Path \\Hulk\note\$host
If false
MD \\Hulk\note\$host
Copy-Item –Path $file\docs\ref\$host\ -Destination \\Hulk\note\$host\ –Recurse

1 个答案:

答案 0 :(得分:0)

假设\ Hulk \ note \ ref是你可以根据输入主机在该目录上创建文件夹的目标,并将同一文件夹复制到任何地方试试这个

    $location="\\Hulk\note\ref"
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null  
     $folderName = Read-Host -Prompt 'Input folder name'
     $target = Join-Path $location $folderName 
     if ( -not (Test-Path '$target' -PathType Container) ) { new-item "\\Hulk\note\$foldername" -type directory }
Copy-Item –Path \\path\$foldername -Destination \\where you need –Recurse