大家好我编写了以下脚本,根据用户输入将文件夹或文件复制到新位置,以desc顺序列出文件夹或文件的数量。这个脚本工作得很完美但我不想复制文件夹而只是复制它的内容,例如,如果有一个名为transaction的文件夹,所以我想复制内部事务但不是事务文件夹本身。
$content = get-childitem 'C:\Users\srk\Desktop\Srk_test'
#Put the sorted data into a variable
$sortedContent = $content | Sort-Object LastWriteTime -Descending
#Create a counter to allow you to index items
$count = 0
foreach ($item in $sortedContent)
{
#Edit: Now with auto-incrementing counter
Write-Host ("{0}: {1}" -f $count++, $item.Name)
$count++
}
$itemNumber = Read-Host "Enter a number for the item to copy"
#Copy the item at the number provided above by the user
$confirmation = Read-Host "Are you Sure You Want To Proceed:"
if ($confirmation -eq 'y') {
$sortedContent[$itemNumber] | Copy-Item -Destination 'C:\test\'
write-output "Your folder has copied to C:\test"
}
elseif
{
write-host "Please try again"
}
答案 0 :(得分:0)
您可以将get-childitem结果发送到管道:
get-childitem $sortedContent[$itemNumber]|Copy-Item -Destination 'C:\test'
答案 1 :(得分:0)
试试这个,首先检查item是否是文件夹,然后递归复制其内容:
...
if ($confirmation -eq 'y') {
if($sortedContent[$itemNumber].PSIsContainer -eq $true){
$src = $sortedContent[$itemNumber].FullName + "\*"
Copy-Item -Path -Recurse -Destination 'C:\test\'
Write-Output "Your folder has copied to C:\test"
}
else{
$sortedContent[$itemNumber] | Copy-Item -Destination 'C:\test\'
Write-Output "Your file has copied to C:\test"
}
}
elseif
...