简体   繁体   English

如何使用Powershell复制根文件夹

[英]How to copy the root folder using powershell

I have a script which displays the latest folder created and copies that folder. 我有一个脚本,该脚本显示创建的最新文件夹并复制该文件夹。 The folder structure is like this 文件夹结构是这样的

$localPath = c:\deployments\dates\codes\html5\users\admin
$DestinationPath = c:\project\html5\users\admin

and than inside admin folder we have other files and folders. 比起admin文件夹,我们还有其他文件和文件夹。 I would like to copy the latest deployments from localpath to destination path which would be replacing admin's content with new deployments 我想将最新的部署从localpath复制到目标路径,这将用新的部署替换管理员的内容

Get-ChildItem -Path $LocalPath | 
Sort-Object -Property LastWriteTime | 
Select-Object -Last 1 | Copy-Item -Destination $DestinationPath -Recurse -Force

The only confusion is to get the latest folder and copying the content of the root folder or the end folder and pasting it on to the destination 唯一的困惑是获取最新的文件夹,然后复制根文件夹或最终文件夹的内容并将其粘贴到目标位置

Maybe simply splitting into a couple of commands is a better approach: 也许简单地分成几个命令是一个更好的方法:

$localPath = c:\deployments\dates\codes\html5\users\admin
$destinationPath = c:\project\html5\users\admin
$latestDir = $(get-childitem $localPath | sort lastwritetime | select -Last 1).FullName
Copy-Item "$latestDir\*" "$destinationPath" -recurse -force

不要太复杂,只需使用选择的输出指定Copy-Item的源路径:

copy (ls $localPath | sort LastWriteTime | select -Last 1) $DestinationPath -Recurse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM