复制由修改日期和customerName分隔的目录内容

时间:2016-06-27 14:37:30

标签: powershell directory

我有一个具有以下结构的信件备份文件夹:

F:\Shares\Ltrstore\Backup\20160626-modifieddate\pathto\x$\Production\Letters\customer1\contentIwant1
F:\Shares\Ltrstore\Backup\20160626-modifieddate\pathto\x$\Production\Letters\customer2\contentIwant1
F:\Shares\Ltrstore\Backup\20160625-modifieddate\pathto\x$\Production\Letters\customer1\contentIwant2
F:\Shares\Ltrstore\Backup\20160625-modifieddate\pathto\x$\Production\Letters\customer2\contentIwant2

有没有人知道我如何使用copy-item,并且在Powershell中使用get-chilitem从这些不同的文件夹中复制contentIwant?

这些信件内容每天都会在该位置备份给不同的客户。但是,在这种情况下,我想要所有日期的cust1和cust2的字母。任何有关如何完成此任务的帮助将不胜感激。提前致谢。

1 个答案:

答案 0 :(得分:0)

假设您要在该路径中获取客户1和客户2的所有文件,请按照以下步骤操作。

$files = Dir F:\Shares\Ltrstore\Backup -recurse
$cust1 = $files | Where FullName -like "*\customer1\*"
$cust2 = $files | Where FullName -like "*\customer2\*"

#Do something with the files
$cust1 | copy-item -destination C:\NewPath -WhatIf

这是如何工作的。我们首先设置$files以包含该备份路径中的每个文件。然后,我们将$cust1设置为等于$files,并使用Where-Object cmdlet(这是PowerShell的许多技巧之一来搜索和查找匹配项)并查看每个文件的FullName属性。

FullName将保存文件的完整路径,因此我们可以使用它来通配符搜索我们想要的客户名称。

最后,有一个例子说明如何将这些文件复制到新文件夹中进行审核。

相关问题