如何将目录及其子目录中的所有PDF文件复制到一个位置? [英] How to copy all PDF files from a directory and its subdirectories to one location?

查看:124
本文介绍了如何将目录及其子目录中的所有PDF文件复制到一个位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将目录及其子目录中的所有 PDF 文件复制到单个目录中?

How do I copy all PDF files from a directory, and it's subdirectories, to a single directory?

实际上有更多文件,并且深度多少有些随意.假设四个目录的最大深度是公平的.

there are actually many more files, and of of somewhat arbitrary depth. It's fair to assume maximum depth of four directories.

例如,如果 a.pdf 位于多个目录中,则我认为文件需要重命名.因为我要将文件添加到 Calibre ,所以与重复删除相比,首选重复文件.(不希望相互检查文件是否重复.)

I suppose the files need to renamed, in the event that a.pdf, for example, is in several directories. Because I'll be adding the files to Calibre, duplicates are preferred over leaving out files. (Not looking to check files against each other for duplicates.)

遵循KISS原则:

PS /home/nicholas/to> 
PS /home/nicholas/to> Copy-Item -path "/home/nicholas/from" -include "*.pdf" -Destination "/home/nicholas/to"
PS /home/nicholas/to> 
PS /home/nicholas/to> ls /home/nicholas/to
PS /home/nicholas/to> 
PS /home/nicholas/to> ls /home/nicholas/from
one  two
PS /home/nicholas/to> 
PS /home/nicholas/to> tree /home/nicholas/from
/home/nicholas/from
├── one
│   ├── a.pdf
│   ├── b.pdf
│   └── foo.txt
└── two
    ├── bar.txt
    ├── c.pdf
    └── d.pdf

2 directories, 6 files
PS /home/nicholas/to> 

显然,以上尝试无法遍历子目录,并且不处理名称冲突.

Obviously, the above attempt fails to traverse into sub-directories, and doesn't deal with name clashes.

在复制每个 PDF 时,重命名它们可能是有意义的. recurse 标志似乎很有用:

Probably makes sense to rename each PDF as it's copied. The recurse flag seems useful:

PS /home/nicholas/to> 
PS /home/nicholas/to> ls
PS /home/nicholas/to> 
PS /home/nicholas/to> Copy-Item -Path "/home/nicholas/from" -Destination "/home/nicholas/to" -Recurse
PS /home/nicholas/to> 
PS /home/nicholas/to> tree
.
└── from
    ├── one
    │   ├── a.pdf
    │   ├── b.pdf
    │   └── foo.txt
    └── two
        ├── bar.txt
        ├── c.pdf
        └── d.pdf

3 directories, 6 files
PS /home/nicholas/to> 

不确定如何过滤出 txt 文件并将所有内容放入一个目录中.

not sure how filter out txt files and put everything into a single directory, however.

使用但是我如何添加一些逻辑以使用 1.pdf 2.pdf 等模式来重命名和增加文件?

but how do I add some logic to rename and increment the files with a pattern like 1.pdf, 2.pdf, etc?

希望合并";带有PDF的文件夹到单个目录.

Looking to "merge" folders with PDF's to a single directory.

推荐答案

在大多数情况下,您处在正确的轨道上:

You're on the right track for the most part:

$PDFs = "C:\"
$i = 1

Get-ChildItem -Path $PDFs -Filter "*.pdf" -Recurse | ForEach-Object -Process {
    Copy-Item $_.FullName -Destination "C:\NewFileDir" -Verbose}
        
Start-Sleep 3

Get-ChildItem -Path C:\NewFileDir -File "*.pdf" -Recurse | ForEach-Object -Process {
    Rename-Item $_.FullName -NewName $("$_{0}.pdf" -f $i++) -Verbose}

这篇关于如何将目录及其子目录中的所有PDF文件复制到一个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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