powershell - 使用另一条路径

时间:2015-04-24 08:00:04

标签: powershell

$path = "c:\folder a"
$destFolder = "C:\" 
$subFolder = "\folder c\folder d\" 
$file = "file.txt" 

$dir = Get-ChildItem $path | select -first 10 | Sort-Object -Property CreationTime 

[array]::Reverse($dir) 

$dir | format-table FullName  

$fullPath = @() 

ForEach ($i in $dir) { 
    $fullPath += $i + $subFolder + $file 
} 

$i = 0 

while ($i -lt $fullPath.Count) { 

    $exists = Test-Path $fullPath[$i] 

    if ($exists){ 
        Copy-Item -Path $fullPath[$i] -Destination $destFolder 
        break 
    } 
    $i++ 
}

无法获得& fullpath工作

编辑:

& fullpath显示目录中的所有文件夹,然后在末尾添加子文件夹+文件。

我希望它从& dir一次获取1个文件路径并添加子文件夹+文件

对不起,如果我还没有解释清楚。

我是这种东西的初学者

1 个答案:

答案 0 :(得分:2)

我认为你需要的是:

$path = "c:\temp"
$destFolder = "C:\" 
$subFolder = "\folder c\folder d\" 
$file = "file.txt" 

$childItems = Get-ChildItem $path | select -first 10 | Sort-Object -Property CreationTime -Descending

forEach ($item in $childItems) 
{
    $fullPath = Join-Path -Path $item.FullName -ChildPath "$subFolder$file" 
    if (Test-Path -Path $fullPath)
    {
        Copy-Item -Path $fullPath -Destination $destFolder
    }
} 
相关问题