Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何将所有PDF文件从一个目录及其子目录复制到一个位置?_Linux_Powershell_File_Pdf_Copy - Fatal编程技术网

Linux 如何将所有PDF文件从一个目录及其子目录复制到一个位置?

Linux 如何将所有PDF文件从一个目录及其子目录复制到一个位置?,linux,powershell,file,pdf,copy,Linux,Powershell,File,Pdf,Copy,如何将所有PDF文件从目录及其子目录复制到单个目录 实际上,还有更多的文件,而且深度有些任意。可以假定最大深度为四个目录 例如,如果a.pdf位于多个目录中,我想这些文件需要重命名。因为我将是Calibre的文件,所以重复的文件比遗漏的文件更可取。(不希望相互检查文件是否存在重复项。) 以下是接吻原则: PS /home/nicholas/to> PS /home/nicholas/to> Copy-Item -path "/home/nicholas/from"

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

实际上,还有更多的文件,而且深度有些任意。可以假定最大深度为四个目录

例如,如果
a.pdf
位于多个目录中,我想这些文件需要重命名。因为我将是
Calibre
的文件,所以重复的文件比遗漏的文件更可取。(不希望相互检查文件是否存在重复项。)

以下是接吻原则:

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> 
显然,上述尝试无法遍历到子目录中,并且不会处理名称冲突

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

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
文件并将所有内容放在一个目录中

成功复制所有
PDF
文件:

但是,我如何添加一些逻辑来重命名和增加具有类似
1.pdf
2.pdf
等模式的文件

正在将带有PDF的文件夹“合并”到一个目录中。

主要有效:

nicholas@mordor:~/powershell/files$ 
nicholas@mordor:~/powershell/files$ pwsh copy_pdfs.ps1 
Copy-Item: /home/nicholas/powershell/files/copy_pdfs.ps1:9
Line |
   9 |      Copy-Item -path $pdf -Destination /home/nicholas/to/$i.pdf
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: The
     | possible origins of 2019-nCoV coronavirus [DOI 10.13140@RG.22.21799.29601] [originsof2019-n

Copy-Item: /home/nicholas/powershell/files/copy_pdfs.ps1:9
Line |
   9 |      Copy-Item -path $pdf -Destination /home/nicholas/to/$i.pdf
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: The
     | possible origins of 2019-nCoV coronavirus [DOI 10.13140@RG.22.21799.29601] [originsof2019-n

done
nicholas@mordor:~/powershell/files$ 
nicholas@mordor:~/powershell/files$ cat copy_pdfs.ps1 





$file = Get-ChildItem /home/nicholas/pdfs -filter *.pdf -recurse 
$i = 1                           
foreach ($pdf in $file) {            
    Copy-Item -path $pdf -Destination /home/nicholas/to/$i.pdf
    $i++              
}

$file = Get-ChildItem -filter *.pdf -recurse 




write-host "done"
nicholas@mordor:~/powershell/files$ 

欢迎批评或其他解决方案。感谢IRC上的weq提供的逻辑。

您在大多数情况下走上了正确的道路:

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

您可以使用
Copy Item
进行筛选<代码>复制项目-路径$pdf-过滤器“*.pdf”-目的地\-\-\-\-您可以使用my forthat@Theo,那真的很有用!如果可能,将抓取该脚本当具有相同名称的文件已复制到目标文件夹时,这将导致名称冲突。事后改名也无济于事。@Theo,那有什么更好的办法呢?提前重命名?我建议复制唯一的,但文件名是相同的,而不是内容。除非我们先重命名唯一的,然后再复制?编辑:参见@Theos,函数。我将这些添加到
calibre
中,并使用
calibre
清除重复项。宁愿有重复的文件也不愿丢失文件。至少在这种情况下。修剪文件似乎是一个独立的过程。
$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}