如何将一个目录中最新文件夹中的项目复制到另一个目录中

如何将一个目录中最新文件夹中的项目复制到另一个目录中

我正在尝试将文件从目录中的最新文件夹复制到另一个文件夹。我编写了以下代码,但似乎 copy-item 命令不适用于变量。有人有其他想法吗?

# Code to get newest directory

$newestFolder = gci \\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\ | ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1

<# get the latest created folder from a path #>


# Copy folder to a folder with different name

Copy-Item $newestFolder.FullName -Destination "\\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\SP Capacity Planning" -Recurse

错误信息:

Copy-Item : Cannot find path '\\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\SP Capacity Planning\Average-Action.csv' because it does not exist. At line:26 char:1 + Copy-Item -Path $sourcedirectory\Average-Action.csv -Destination $tar ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\\Server123...rage-Action.csv:String) [Copy-Item], ItemNo tFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 

Copy-Item:找不到路径“\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\SP Capacity Planning\MaxDuration20.csv”,因为它不存在。第 47 行,字符:1 + Copy-Item -Path $sourcedirectory1\MaxDuration20.csv -Destination $tar ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


答案1

仔细检查您是否实际将目录复制到目录,因为您发布的错误使用的是文件名而不是文件夹。

使用您的路径尝试以下操作:

$sourcefolder = '\\path\to\folder\'
$targetfolder = '\\path\to\target\'
$newestfolder = gci -Directory $sourcefolder | sort CreationTime -desc | select -f 1  ## get the newest folder
copy -recurse $newestfolder $targetfolder

相关内容