windows 在PowerShell中递归地将一组文件从一个目录复制到另一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25817034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively copy a set of files from one directory to another in PowerShell
提问by Mike Christensen
I'm trying to copy all *.csproj.user
files recursively from C:\Code\Trunk
to C:\Code\F2
.
我正在尝试将所有*.csproj.user
文件递归复制C:\Code\Trunk
到C:\Code\F2
.
For example:
例如:
C:\Code\Trunk\SomeProject\Blah\Blah.csproj.user
C:\Code\Trunk\SomeProject\Blah\Blah.csproj.user
Would get copied to:
将被复制到:
C:\Code\F2\SomeProject\Blah\Blah.csproj.user
C:\Code\F2\SomeProject\Blah\Blah.csproj.user
My current attempt is:
我目前的尝试是:
Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse -WhatIf
复制项目 C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse -WhatIf
However I get:
但是我得到:
What if: Performing operation "Copy Directory" on Target "Item: C:\Code\Trunk Destination: C:\Code\F2\Trunk".
如果:在目标“项目:C:\Code\Trunk 目的地:C:\Code\F2\Trunk”上执行“复制目录”操作。
First, it wants to put them all in a new folder called F2\Trunk
which is wrong. Second, it doesn't list any of the files. There should be about 10 files to be copied over.
首先,它想将它们全部放在一个名为F2\Trunk
which 错误的新文件夹中。其次,它没有列出任何文件。应该有大约 10 个文件要复制。
What's the correct syntax for the command? Thanks!
命令的正确语法是什么?谢谢!
Update:
更新:
Okay, it seems to have something to do with the fact that C:\Code\F2
already exists. If I try copying the files over to a destination that does notexist, it works.
好吧,这似乎与C:\Code\F2
已经存在的事实有关。如果我尝试将文件复制到不存在的目的地,它会起作用。
I want to overwriteany existing .csproj.user
files in the destination.
我想覆盖.csproj.user
目标中的任何现有文件。
采纳答案by TheMadTechnician
Seen this before, and I don't know why PowerShell can't seem to get it right (IMHO). What I would do is more cumbersome but it works.
以前见过这个,我不知道为什么 PowerShell 似乎无法正确处理(恕我直言)。我会做的更麻烦,但它有效。
$Source = 'C:\Code\Trunk'
$Files = '*.csproj.user'
$Dest = 'C:\Code\F2'
Get-ChildItem $Source -Filter $Files -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Copy-Item $_.FullName -Destination $Path -Force
}
回答by Jaykul
You guys are making this hideously complicated, when it's really simple:
你们把这搞得异常复杂,其实很简单:
Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse
Will copy the Directory, creating a "Trunk" directory in F2. If you want to avoid creating the top-level Trunk folder, you have to stop telling PowerShell to copy it:
将复制Directory,在 F2 中创建一个“Trunk”目录。如果你想避免创建顶级 Trunk 文件夹,你必须停止告诉 PowerShell 复制它:
Get-ChildItem C:\Code\Trunk | Copy-Item -Destination C:\Code\F2 -Recurse -filter *.csproj.user
回答by user3141326
While the most voted answer is perfectly valid for single file types, if you need to copy multiple file types there is a more useful functionality called robocopy exactly for this purpose with simpler usage
虽然投票最多的答案对单一文件类型完全有效,但如果您需要复制多种文件类型,则有一个更有用的功能,称为 robocopy 正是为此目的,使用更简单
robocopy C:\Code\Trunk C:\Code\F2 *.cs *.xaml *.csproj *.appxmanifest /s
回答by Stuart Smith
I tried Jaykul answer and it did not work for me. I had to change it as below to get it to work. I also created the C:\Code\F2 folder before it worked.
我尝试了 Jaykul 的回答,但对我不起作用。我必须按如下方式更改它才能使其正常工作。在它工作之前,我还创建了 C:\Code\F2 文件夹。
Get-ChildItem C:\Code\Trunk Recurse -filter *.csproj.user | Copy -Destination C:\Code\F2
回答by Daniel Panario
Recently I had to replace a file present in several sub folders, I did it as below.
最近我不得不替换存在于几个子文件夹中的一个文件,我做了如下。
foreach($file in (Get-ChildItem File_you_want_to_be_copied.txt -Recurse)) {$target=$file.directoryname; Copy-Item -path C:\Source_Path\File_you_want_to_be_copied.txt -Destination $target}
回答by Steve
Answer 1 looked good, and I changed to Move-Item
for my purposes. However I found that in each folder it recursively went through, it only moved the first file. Below is my complete script which also includes some conversion of doc files to pdf's:
答案 1 看起来不错,Move-Item
为了我的目的,我改为。但是我发现在它递归遍历的每个文件夹中,它只移动了第一个文件。下面是我的完整脚本,其中还包括一些 doc 文件到 pdf 的转换:
$Source = 'C:\Users\sgrody\Desktop\NSPM-Vol1'
$MoveFiles = '*.PDF'
$Dest = 'C:\Users\sgrody\Desktop\MedPassPDF'
$Folders = Get-ChildItem $Source -Directory -Recurse
ForEach ($Folder in $Folders)
{
$wdFormatPDF = 17
$word = New-Object -ComObject word.application
$word.visible = $false
$folderpath = "$($Folder.FullName)\*"
$fileTypes = "*.docx","*doc"
Get-ChildItem -path $folderpath -include $fileTypes |
foreach-object
{
$path = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
"Converting $path to pdf ..."
$doc = $word.documents.open($_.fullname)
$doc.saveas([ref] $path, [ref]$wdFormatPDF)
$doc.close()
}
$word.Quit()
}
Get-ChildItem $Source -Filter $MoveFiles -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Move-Item $_.FullName -Destination $Path -Force
}
}