如何只在文件夹有特定类型的文件(例如PowerShell、.csv
、.xls
、.xlsx
)时才编写该脚本来存档文件夹。我的源路径是C:\Temp\Test\
,有5个子文件夹:
只有foldera
、folderb
、folderc
有要归档的文件,归档路径是C:\Temp\Test\Archive
,这里有folders
foldera
,folderb
,因此这些文件夹不需要创建,但是需要压缩和复制文件,需要创建folderc
,必须压缩和复制文件。
使用下面的代码,我可以得到文件,但不是特定文件所在的文件夹。
$ignorefolders = @(
"C:\Temp\misc"
)
$archive = "C:\Temp\Archive"
[regex]$exclude = ‘(?i)^(‘ + (($ignorefolders |foreach {[regex]::escape($_)}) –join “|”) + ‘)’
$includefiletype = @("*.txt", "*.csv", "*.dat", "*.xls", "*.xlsx")
$workingDir = "C:\Temp"
Get-ChildItem $workingDir -recurse -include $fileFilter | Where {$_.FullName -notmatch $exclude} | Copy-Item -Destination $archive
发布于 2016-06-07 20:27:09
要获取目录,请选择从FileInfo
返回的Get-ChildItem
对象的Directory属性,我还假设您不需要任何重复的目录,因此,您必须使用sort
过滤掉这些目录
Get-ChildItem -Recurse -Path $source -include*.txt,*.csv,*.xls,*.xlsx | select directory | sort -Unique -Property Directory | Select -expand Directory | Copy-Item -Destination $archive -WhatIf
https://stackoverflow.com/questions/37686972
复制相似问题