Reputation: 3
$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++
}
having trouble getting &fullpath to work
edit:
&fullpath displays all the folder in the directory then adds the subfolder+file at the end.
I want it to take the 1 file path at a time from &dir and add the subfolder+file
sorry if I haven't explained it very well.
Im a total beginner at this kind of stuff
Upvotes: 0
Views: 451
Reputation: 4348
I think what you need is this:
$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
}
}
Upvotes: 2