使用PowerShell复制无文件的文件夹,无文件夹的文件或所有内容 [英] Copy folders without files, files without folders, or everything using PowerShell

查看:290
本文介绍了使用PowerShell复制无文件的文件夹,无文件夹的文件或所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够编写一个PowerShell脚本,它将获取一个文件夹(或一组文件夹),并复制它们可以创建文件夹结构(无文件),其中文件结构(没有文件夹),或者可以创建这两个文件(所有或选定的) AND 文件夹。

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.

    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.

我想要一个PowerShell脚本在其他地方复制该文件夹结构,让我可以选择仅文件夹结构仅文件文件和文件夹

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.

在上面,我想要能够复制C:\ ProdData重命名为另一个文件夹,基本上将ProdData重命名为TestData(即 C:\ProdData 复制到 C:\TestData )或复制整个文件夹结构。

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 导入另一个文件夹,该文件夹是其他文件的子文件夹: C:\TestArea\TestFolders\2012-04-01\TestData\ProdData\Fld1 等...

C:\ProdData into another folder that is a subfolder of something else: C:\TestArea\TestFolders\2012-04-01\TestData\ProdData\Fld1, etc...

并选择包含文件夹,文件或两者

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

并将其复制到:

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.

仅文件(展平结构)

有几种方法来处理,但我不知道一个特别好或优雅的方式。这是一个尴尬但有效的方法 - 对于每个子目录,它运行非递归复制到根目标文件夹。批评或更好的替代品将是最受欢迎的。

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 }

/ strong>

Everything

这种情况是最简单的。

robocopy $source $dest /e

您可以自己试验交换机$ c> robocopy /?),以微调过程。在我看来,最棘手的方案是平坦的目录结构。您将有一些决定,如何处理出现在多个目录中的相同文件名。

You can experiment with the switches yourself (referencing robocopy /? along the way) to fine tune the process. 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.

至于如何从命令行接受选项,我建议将Powershell参数集合为您的三种情况。有很多博客讨论这个概念,但您也可以查看 TechNet a> 或在Powershell中运行帮助about_Functions_Advanced_Parameters

As for how to accept options from the command line, I'd suggest putting together Powershell parameter sets for your three situations. 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复制无文件的文件夹,无文件夹的文件或所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆