Powershell 复制没有文件的文件夹树
我需要复制包含子文件夹的文件夹,但除了包含文件夹“Project”的数据外,不包含任何文件 所以我需要创建新文件夹树,但它应该只包含名为“Project”的子文件夹中存在的文件 好的,我的解决方案:Powershell 复制没有文件的文件夹树,powershell,copy,directory,Powershell,Copy,Directory,我需要复制包含子文件夹的文件夹,但除了包含文件夹“Project”的数据外,不包含任何文件 所以我需要创建新文件夹树,但它应该只包含名为“Project”的子文件夹中存在的文件 好的,我的解决方案: $folder = dir D:\ -r $folder foreach ($f in $folder) { switch ($f.name) { "project" { Copy-Item -i *.* $f.FullName D:\test2
$folder = dir D:\ -r
$folder
foreach ($f in $folder)
{
switch ($f.name)
{
"project"
{
Copy-Item -i *.* $f.FullName D:\test2
}
default
{
Copy-Item -exclude *.* $f.FullName D:\test2
}
}
}
$source = "c:\dev"
$destination = "c:\temp\copydev"
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { $_.psIsContainer } |
ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
ForEach-Object { $null = New-Item -ItemType Container -Path $_ -Force }
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { -not $_.psIsContainer -and (Split-Path $_.PSParentPath -Leaf) -eq "Project"} |
Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
使用
getchilditem
在文件夹上递归,并使用newitem
重新映射结构。在递归中,您可以轻松地检查“Project”。使用Get ChildItem
在文件夹上递归,并使用New Item
重新映射结构。在递归中,您可以轻松地检查“项目”。使用xcopy/t
仅复制文件夹结构,然后分别复制项目
文件夹。大概是这样的:
'test2\' | Out-File D:\exclude -Encoding ASCII
xcopy /t /exclude:d:\exclude D:\ D:\test2
gci -r -filter Project | ?{$_.PSIsContainer} | %{ copy -r $_.FullName d:\test2}
ri d:\exclude
使用
xcopy/t
仅复制文件夹结构,然后分别复制Project
文件夹。大概是这样的:
'test2\' | Out-File D:\exclude -Encoding ASCII
xcopy /t /exclude:d:\exclude D:\ D:\test2
gci -r -filter Project | ?{$_.PSIsContainer} | %{ copy -r $_.FullName d:\test2}
ri d:\exclude
另一个解决方案:
$folder = dir D:\ -r
$folder
foreach ($f in $folder)
{
switch ($f.name)
{
"project"
{
Copy-Item -i *.* $f.FullName D:\test2
}
default
{
Copy-Item -exclude *.* $f.FullName D:\test2
}
}
}
$source = "c:\dev"
$destination = "c:\temp\copydev"
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { $_.psIsContainer } |
ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
ForEach-Object { $null = New-Item -ItemType Container -Path $_ -Force }
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { -not $_.psIsContainer -and (Split-Path $_.PSParentPath -Leaf) -eq "Project"} |
Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
另一个解决方案:
$folder = dir D:\ -r
$folder
foreach ($f in $folder)
{
switch ($f.name)
{
"project"
{
Copy-Item -i *.* $f.FullName D:\test2
}
default
{
Copy-Item -exclude *.* $f.FullName D:\test2
}
}
}
$source = "c:\dev"
$destination = "c:\temp\copydev"
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { $_.psIsContainer } |
ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
ForEach-Object { $null = New-Item -ItemType Container -Path $_ -Force }
Get-ChildItem -Path $source -Recurse -Force |
Where-Object { -not $_.psIsContainer -and (Split-Path $_.PSParentPath -Leaf) -eq "Project"} |
Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
首先,创建目录结构:
xcopy D:\source D:\destination /t /e
现在,遍历源目录,复制项目目录中的每个文件:
Get-ChildItem D:\Source * -Recurse |
# filter out directories
Where-Object { -not $_.PsIsContainer } |
# grab files that are in Project directories
Where-Object { (Split-Path -Leaf (Split-Path -Parent $_.FullName)) -eq 'Project' } |
# copy the files from source to destination
Copy-Item -Destination ($_.FullName.Replace('D:\source', 'D:\destination'))
首先,创建目录结构:
xcopy D:\source D:\destination /t /e
现在,遍历源目录,复制项目目录中的每个文件:
Get-ChildItem D:\Source * -Recurse |
# filter out directories
Where-Object { -not $_.PsIsContainer } |
# grab files that are in Project directories
Where-Object { (Split-Path -Leaf (Split-Path -Parent $_.FullName)) -eq 'Project' } |
# copy the files from source to destination
Copy-Item -Destination ($_.FullName.Replace('D:\source', 'D:\destination'))
你什么都不做却失败了?我不知道如何将文件保存在“Project”文件夹中。伙计,在发布之前最好先阅读常见问题部分:-你应该展示你为解决问题所做的努力,向我们展示你尝试编写但不起作用的代码,告诉我们你们并没有要求我们做你们的硬件作业……好吧,我已经把我的代码放在主题里了。这两个文件夹都不起作用。举一个文件夹结构的例子会有所帮助。你什么都没做,但失败了?我不知道如何将文件保存在“项目”文件夹中。伙计,在发布之前最好先阅读常见问题部分:-你应该展示你为解决问题所做的努力,向我们展示您尝试编写但不起作用的代码,向我们展示您没有要求我们执行硬件作业……好的,我已经将代码放在主题中了。这两种方法都不起作用。举个文件夹结构的例子会有帮助。我做不到。子文件夹太多,并且每个子文件夹都有“Project”文件夹。如果太多子文件夹导致任何自动方法都失败,那么您已经有其他问题了。对不起,我想您指的是“然后分别复制项目文件夹”手动复制。我不能。子文件夹太多,并且每个子文件夹都有“Project”文件夹。如果太多子文件夹导致任何自动方法都失败,那么您已经有其他问题了。对不起,我想您指的是“然后分别复制项目文件夹”手动复制。