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

查看:36
本文介绍了使用 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:ProdDataFld1
    C:ProdDataFld2
    C:ProdDataFld3 - has 2 subfolders under it
    C:ProdDataFld3MyFolder1
    C:ProdDataFld3MyFolder2

上面的每个文件夹中都有不同数量、不同大小、不同文件扩展名的文件.

Each folder above has a various number of files of various sizes in it, of various file extensions.

我想要一个 PowerShell 脚本,可以在别处复制该文件夹结构,让我可以选择仅文件夹结构仅文件文件AND 文件夹 被复制.

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:TestAreaTestFolders2012-04-01TestDataProdDataFld1等...

C:ProdData into another folder that is a subfolder of something else: C:TestAreaTestFolders2012-04-01TestDataProdDataFld1, 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:ProdDataFld1MyFile.dat

并将其复制到:

c:TestDatayyyyMMdd_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.

推荐答案

复制文件夹层次结构中的所有内容

Copy-Item $source $dest -Recurse -Force

要复制层次结构,您可以尝试:

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

要扁平化文件结构,您可以尝试:

$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} 

祝你好运!

这篇关于使用 PowerShell 复制没有文件的文件夹、没有文件夹的文件或所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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