使用csv数据Powershell时仅复制某些文件夹 [英] Copy only certain folders while using csv data powershell
问题描述
我不想复制所有文件夹,如何告诉我不要复制某些文件夹(如appData和application Data)
I don't want to copy all the folder how do I tell it not to copy certain folders like ( appData and application Data )
csv文件包含:
FullName Name
\\computer6587\c$\Users\A2846 A2846
\\computer6587\c$\Users\B2846 B2846
代码:
Import-Csv -path C:\computer6587-PREP.csv | foreach {Copy-item -recurse $_.FullName "c:\backup" -Force -Verbose}
推荐答案
为此,我前段时间做了一个辅助函数:
For this I made a helper function some time ago:
function Copy-ExcludeFolders {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateScript({ $_ | Test-Path -PathType Container })]
[string]$SourceFolder,
[Parameter(Mandatory = $true, Position = 1)]
[string]$Destination,
[Parameter(Mandatory = $true, Position = 2)]
[string[]]$Exclude,
[switch]$Force
)
# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($Exclude | ForEach-Object { [Regex]::Escape($_) }) -join '|'
Get-ChildItem -Path $SourceFolder -Recurse -File |
Where-Object{ $_.DirectoryName -notmatch $notThese } |
ForEach-Object {
$target = Join-Path -Path $Destination -ChildPath $_.DirectoryName.Substring($SourceFolder.Length)
if (!(Test-Path -Path $target -PathType Container)) {
Write-Verbose "Creating folder '$target'"
$null = New-Item -Path $target -ItemType Directory
}
Write-Verbose "Copying '$($_.FullName)' to '$target'"
$_ | Copy-Item -Destination $target -Force:$Force
}
}
在脚本顶部安装该功能,如下所示:
With that function in place at the top of your script, use it like this:
$exclude = 'appData', 'application Data' # add more folders to exclude here
Import-Csv -Path 'C:\computer6587-PREP.csv' | ForEach-Object {
Copy-ExcludeFolders -SourceFolder $_.FullName -Destination 'C:\backup' -Exclude $exclude -Force -Verbose
}
如果要将用户数据复制到目标路径中的某个子文件夹,则建议您也使用csv中的 Name
列.
$exclude = 'appData', 'application Data' # add more folders to exclude here
Import-Csv -Path 'C:\computer6587-PREP.csv' | ForEach-Object {
$dest = Join-Path -Path $destination -ChildPath $_.Name
# just to see if the input from the csv will give you the desired source and destination paths:
Write-Host "Copying files from '$($_.FullName)' to folder '$dest'"
Copy-ExcludeFolders -SourceFolder $_.FullName -Destination $dest -Exclude $exclude -Force -Verbose
}
编辑
我们不应在一个问题上添加太多评论.这就是我测试上述代码的方式,结果表明该代码完全符合预期.
We should not add so many comments to a question. This is how I tested the above code and the result shows it is exactly as expected.
我将其用于名为 Source-PREP.csv
的输入csv(您的文件名当然会有所不同)
I used this for input csv called Source-PREP.csv
(your filename will be different of course)
FullName,Name,LastWriteTime
D:\Test\Users\H107371a,H107371a,11/25/2020 7:19
D:\Test\Users\m763671,m763671,12/3/2020 9:12
D:\Test\Users\W575288,W575288,9/18/2020 15:23
D:\Test\Users\W575288a,W575288a,9/18/2020 15:23
然后,我在D:\ Test路径中创建了一些虚拟用户,其中包含"appData"和"application Data"文件夹以及一些应复制的子文件夹:
Then I created a few dummy users in my D:\Test path, with 'appData' and 'application Data' folders aswell as some subfolders that should be copied:
D:\TEST\USERS
+---H107371a
| | test.txt
| |
| +---appData
| | mustnotcopy.txt
| |
| +---application Data
| | donotcopy.txt
| |
| +---Folder1
| | blah.txt
| | somepicture.jpg
| |
| \---Folder2
| myvideo.mp4
| whatever.txt
|
+---m763671
| | test.txt
| |
| +---appData
| | mustnotcopy.txt
| |
| +---application Data
| | donotcopy.txt
| |
| +---FolderA
| | blah1.txt
| | somepicture1.jpg
| |
| \---FolderB
| myvideo1.mp4
| whatever1.txt
|
+---W575288
| | test.txt
| |
| +---appData
| | mustnotcopy.txt
| |
| +---application Data
| | donotcopy.txt
| |
| +---FolderX
| | blah2.txt
| | somepicture2.jpg
| |
| \---FolderY
| myvideo2.mp4
| whatever2.txt
|
\---W575288a
| test.txt
|
+---Another-Folder
| myvideo3.mp4
| whatever3.txt
|
+---appData
| mustnotcopy.txt
|
+---application Data
| donotcopy.txt
|
\---Some-Folder
blah3.txt
somepicture3.jpg
接下来,我运行代码
$exclude = 'appData', 'application Data' # add more folders to exclude here
$destination = "\\<myMachine>\D$\CopiedProfiles"
Import-Csv -Path 'D:\Source-PREP.csv' | ForEach-Object {
$dest = Join-Path -Path $destination -ChildPath $_.Name
# just to see if the input from the csv will give you the desired source and destination paths:
Write-Host "Copying files from '$($_.FullName)' to folder '$dest'"
Copy-ExcludeFolders -SourceFolder $_.FullName -Destination $dest -Exclude $exclude -Force -Verbose
}
这是复制后的结果:
D:\COPIEDPROFILES
+---H107371a
| | test.txt
| |
| +---Folder1
| | blah.txt
| | somepicture.jpg
| |
| \---Folder2
| myvideo.mp4
| whatever.txt
|
+---m763671
| | test.txt
| |
| +---FolderA
| | blah1.txt
| | somepicture1.jpg
| |
| \---FolderB
| myvideo1.mp4
| whatever1.txt
|
+---W575288
| | test.txt
| |
| +---FolderX
| | blah2.txt
| | somepicture2.jpg
| |
| \---FolderY
| myvideo2.mp4
| whatever2.txt
|
\---W575288a
| test.txt
|
+---Another-Folder
| myvideo3.mp4
| whatever3.txt
|
\---Some-Folder
blah3.txt
somepicture3.jpg
如果结果不同,请更正输入的csv,以便该文件在 FullName
列中具有正确的SourceFolder和正确的目标子文件夹名称,以在参数 Destination <中创建目标文件夹/code>
If you have different results, then please correct your input csv so the file has the correct SourceFolder in column FullName
and the correct destination subfolder name to create the target folder in parameter Destination
这篇关于使用csv数据Powershell时仅复制某些文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!