powershell - 将 pdf 文件从子文件夹移动到 1 个主文件夹并为多个文件夹重复使用脚本
问题描述
我有一个季度返回流程,其中创建了大约 60.000 个 pdf。我解压缩这些文件以确定创建 pdf 文件的运行时间。总共有 7 个不同的组,所以我有 7 个集合名称。但时期不同,导致文件夹名称结构发生变化。zipfiles 的结构包含其他人使用的子文件夹,我想将 pdf 文件移动到 1 个文件夹。
所以情况:M06-Q2_Juni 将是 M09-Q3_September
Location: C:\Lolke\2020\M06-Q2_Juni\Run\PBNL\Digital (this contains subfolders)
Destination: C:\Lolke\2020\M06-Q2_Juni\Run\PBNL\allpdfs
当我对源和目标进行硬编码时,我有以下测试脚本,但这最好是可变的。但是我在谷歌上找不到任何地方如何结合移动 pdf 文件来做到这一点。
New-Item -Path '\\solon.prd\files\P\Global\Users\C43048\UserData\Desktop\Test_Powershell\allpdfs' -ItemType Directory
Get-Childitem \\solon.prd\files\P\Global\Users\C43048\UserData\Desktop\Test_Powershell\Digital -recurse -filter "*.pdf" | %{
Move-Item -Path $_.FullName -Destination \\solon.prd\files\P\Global\Users\C43048\UserData\Desktop\Test_Powershell\allpdfs}
解决方案
洛克,
我生成了一个接受三个参数的函数。
- BasePath,源和目标相同的路径部分。
- FilePath,需要定位PDF文件的部分。
- 目的地,要添加到用于存储 PDF 的位置的 BasePath 的部分。
如果目标尚不存在,程序将创建目标。
我已经评论了这个程序,所以你可以看到发生了什么。
Function Move-PDFs {
Param (
[Parameter(Mandatory=$True)]
[String]$BasePath,
[Parameter(Mandatory=$True)]
[String] $FilePath,
[Parameter(Mandatory=$True)]
[String] $Destination
)
#Test to see if BasePath is valid
If (-not (Test-Path -Path "$BasePath")){
"Base Path: $BasePath does NOT exist!"
Return ,$False
} #End If
#Test to see if BasePath+FilePath is valid
If (-not (Test-Path -Path "$BasePath\$FilePath")) {
"Full Path: $BasePath\$FilePath does NOT exist!"
Return ,$False
} #End If
#Test to see if Destination is valid if not create it
If (-not (Test-Path -Path "$BasePath\$Destination")) {
"Destination Path: $BasePath\$Destination does NOT exist!"
New-Item -Path "$BasePath\$Destination" -ItemType Directory
} #End If
#Get your PDF file list
$GCIArgs = @{Path = "$BasePath\$FilePath"
Include = "*.PDF"
Recurse = $True}
$FileList = Get-ChildItem @GCIArgs
#Copy the files
ForEach ($File in $FileList) {
Copy-Item -Path "$File" -Destination "$BasePath\$Destination" }
} #End Function Move-PDFs
#Call the function Using My Test data.
$MPArgs = @{BasePath = "G:\Test\WPOA Files"
FilePath = "Digital"
Destination = "AllPDFS"}
Move-PDFs @MPArgs
当然,由于您有多个位置,您可以轻松地添加一个包含 BasePaths 的数组,然后遍历该数组并调用 Move-PDF。这假设 FilePath 和 Destination 没有改变。这可以处理,但它需要更多的工作。
如果需要可以添加更多测试,您还可以传回复制的文件数,并且可以在复制项中包含 Force 参数,以防两个文件具有相同的名称。
这个基础程序应该会给你一个好的开始。
编辑:我刚刚意识到我错误地命名了例程或误解了您的要求。编写的程序会复制它不移动的文件。如果要移动文件,请将 Copy-Item 更改为 Move-Item。如果要复制它们,您可能需要将名称更改为 Copy-PDFs。
高温高压
推荐阅读
- css - 在容器内放置一个固定元素
- javascript - 在 php 和 javascript 之间传递数据时,Json_Encode 不起作用
- java - 从 Instagram 个人资料中读取文本
- r - 在 R 中 - 使用百分比回报计算向量中的值
- javascript - 为什么我的 React App 有 index.pack.js 的语法错误?
- database - 如何将云firestore中的单个文档字段保存到flutter中的局部变量中?
- r - R编程中的街道地址图绘制
- vb.net - 将字符串转换为双精度并添加总计
- javascript - 如何在 FabricJS 的父对象的中心创建对象棒?
- android - VSCode 中的 Flutter beta 热重载适用于 Web,但不适用于 iOS 和 Android(适用于 Android Studio)