简体   繁体   English

复制没有文件的文件夹,没有文件夹的文件或使用PowerShell的所有文件

[英]Copy folders without files, files without folders, or everything using PowerShell

I would like to be able to write a PowerShell script that would take a folder (or set of folders) and to duplicate them in which the folder structure (without the files) could be created, where the file structure (without the folders) could be created, or in which both the files (all or selected ones) AND folders could be created. 我希望能够编写一个PowerShell脚本来获取文件夹(或文件夹集)并复制它们,其中可以创建文件夹结构(没有文件),其中文件结构(没有文件夹)可以创建,或者可以创建文件(所有或选定的) AND文件夹。

    Example:

    C:\ProdData has 3 subfolders in it
    C:\ProdData\Fld1
    C:\ProdData\Fld2
    C:\ProdData\Fld3 - has 2 subfolders under it
    C:\ProdData\Fld3\MyFolder1
    C:\ProdData\Fld3\MyFolder2

Each folder above has a various number of files of various sizes in it, of various file extensions. 上面的每个文件夹都有各种不同大小的文件,各种文件扩展名。

I would like to have a PowerShell script that would duplicate that folder structure elsewhere, giving me a choice of Folder Structure Only , Files Only , or Files AND Folders to be copied. 我想有一个PowerShell脚本,可以在其他地方复制该文件夹结构,让我可以选择复制文件夹结构, 文件文件和文件夹

In the above, I'd like to be able to copy C:\\ProdData to another folder, essentially renaming ProdData to TestData (that is, C:\\ProdData copied to C:\\TestData ), or to copy the entire folder structure. 在上面,我希望能够将C:\\ ProdData复制到另一个文件夹,基本上将ProdData重命名为TestData(即C:\\ ProdData复制到C:\\ TestData ),或者复制整个文件夹结构。

C:\\ProdData into another folder that is a subfolder of something else: C:\\TestArea\\TestFolders\\2012-04-01\\TestData\\ProdData\\Fld1 , etc... C:\\ProdData到另一个文件夹中,该文件夹是其他东西的子文件夹: C:\\TestArea\\TestFolders\\2012-04-01\\TestData\\ProdData\\Fld1等...

And selectively choose to include folders, files or both . 并有选择地选择包含文件夹,文件或两者

The ideal solution would be to (for folders only) take the folder structure, export/save it, read it in, and create that same structure elsewhere. 理想的解决方案是(仅限文件夹)获取文件夹结构,导出/保存,读入,并在其他地方创建相同的结构。 Some solutions I've found said they copied the folder structure only, but if data exists, that gets copied also and you can't exclude one or the other. 我发现的一些解决方案说他们只复制了文件夹结构,但如果数据存在,那么它也会被复制,你不能排除其中一个。 I have a partial solution to the files-only requirement that semi-preserves the path, but it would take a file: 我对仅保留路径的文件专用要求有部分解决方案,但它需要一个文件:

c:\ProdData\Fld1\MyFile.dat

and copy it to: 并将其复制到:

c:\TestData\yyyyMMdd_hhmmss_ProdData_Fld1_MyFile.dat

But that's not something that could easily be decoded and would be impossible to re-integrate the source structure. 但这不是可以轻易解码的东西,也不可能重新整合源结构。 I'd appreciate any thoughts about how to accomplish this without re-inventing the wheel. 我很欣赏任何关于如何在不重新发明轮子的情况下实现这一目标的想法。

Here are some ideas for handling each situation you describe. 以下是处理您描述的每种情况的一些想法。

Folder Structure Only 仅限文件夹结构

This will copy the source to a destination, recursing subdirectories but excluding all files. 这会将源复制到目标,递归子目录但排除所有文件。

robocopy $source $dest /e /xf *.*

Files Only (Flatten Structure) 仅文件(展平结构)

There are a few ways to handle this, but I don't know of an especially good or elegant way. 有几种方法可以解决这个问题,但我不知道一种特别好或优雅的方式。 Here's an awkward but effective approach - for each subdirectory, it runs a non-recursive copy to a root destination folder. 这是一个尴尬但有效的方法 - 对于每个子目录,它运行非递归副本到根目标文件夹。 Criticism or nicer alternatives would be most welcome. 批评或更好的替代品将是最受欢迎的。

Get-ChildItem $source -Recurse |
  Where-Object { $_.PSIsContainer -eq $true } |
  Foreach-Object { robocopy $_.FullName $dest }

Everything 一切

This scenario is the most straightforward of the bunch. 这种情况是最直接的。

robocopy $source $dest /e

You can experiment with the switches yourself (referencing robocopy /? along the way) to fine tune the process. 您可以自己试验这些开关(沿途引用robocopy /? )来微调过程。 The trickiest scenario in my opinion is the one that flattens the directory structure. 在我看来,最棘手的情况是使目录结构变得平坦。 You'll have some decisions to make, like how you want to handle the same file name appearing in multiple directories. 您将做出一些决定,例如您希望如何处理出现在多个目录中的相同文件名。

As for how to accept options from the command line, I'd suggest putting together Powershell parameter sets for your three situations. 至于如何从命令行接受选项,我建议为你的三种情况组合Powershell参数集。 There are a number of blog posts around that discuss the concept, but you can also see TechNet or run help about_Functions_Advanced_Parameters inside Powershell. 有很多博客文章讨论这个概念,但你也可以在Powershell中看到TechNet或运行help about_Functions_Advanced_Parameters

To copy everything in a folder hierarchie 复制文件夹hierarchie中的所有内容

Copy-Item $source $dest -Recurse -Force

To copy the hierarchie you can try: 要复制hierarchie,您可以尝试:

$source = "C:\ProdData"
$dest = "C:\TestData"
Copy-Item $source $dest -Filter {PSIsContainer} -Recurse -Force

To flatten a file structure you can try: 要展平文件结构,您可以尝试:

$source = "C:\ProdData"
$dest = "C:\TestData"
New-Item $dest -type directory
Get-ChildItem $source -Recurse | `
    Where-Object { $_.PSIsContainer -eq $False } | `
    ForEach-Object {Copy-Item -Path $_.Fullname -Destination $dest -Force} 

Good luck! 祝好运!

The following copies just the folder structure form ProdData to TestData without any files 以下只复制ProdData到TestData的文件夹结构,没有任何文件

 $from = "C:\\ProdData" $to = "C:\\TestData" Get-ChildItem -Path $from -Recurse | ?{ $_.PSIsContainer } | Copy-Item -Destination {Join-Path $to $_.Parent.FullName.Substring($from.length)} -Force 

I propose my solution : 我提议我的解决方案:

$source = "C:\temp\"
$dest = "C:\TestData\"

#create destination directory if dont exist
New-Item -ItemType Directory $dest -Force


#To copy everything in a folder hierarchie
Get-ChildItem $source -Recurse | Copy-Item -Destination {$_.FullName.Replace($source, $dest)}  -Force

#To copy only directory hierarchie:
Get-ChildItem $source -Recurse -directory | Copy-Item -Destination {$_.FullName.Replace($source, $dest)}  -Force

#To copy only file (remove doublon be carefull):
Get-ChildItem $source -Recurse -file | Copy-Item -Destination $dest -Force

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS / Swift 4:如何在不删除文件和文件夹现有目标的情况下复制包含多个包含文件的文件夹的文件夹? - iOS/Swift 4: How to copy a folder containing several folders containing files without removing the files and folders existing destination? 一个用于将文件夹(不含文件)从一个位置复制到另一个位置的衬垫 - One liner to copy folders (without files) from one location to another 使用 powershell 2 压缩多个文件夹和文件 - Zip multiple folders and files using powershell 2 如何使用Powershell将这些文件备份到特定文件夹中 - How to backup these files into specific folders using powershell 仅返回包含使用 Powershell 的文件的文件夹 - Return only folders containing files using Powershell 复制子文件夹,ZIP和RAR的所有文件,而不会覆盖目标文件夹中具有相同名称的文件 - Copy all files from subfolders, ZIP's and RAR's, without overwriting files with same name in destination folders 在几个Powershell版本中复制文件和文件夹(2.0,3.0-4.0) - Copy files and folders in several Powershell versions (2.0, 3.0-4.0) 使用PowerShell v5,搜索特定的文件夹,将这些文件夹/文件复制到其他驱动器的路径中 - Using PowerShell v5, search for specific folder(s) copy those folders/files, AND the paths to another drive Applescript将文件复制到特定文件夹 - Applescript copy files to specific folders java ftp删除所有文件而不删除文件夹 - java ftp delete all files without deleting folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM