powershell - 如何解析数百个文件并通过 powershell 将它们移动到他们尊重的文件中
问题描述
我正在尝试编写一个脚本,将数百个帐户 PDF 移动到各自的文件夹中。我对 powershell 和我的基本脚本非常陌生,现在我能够使用此脚本将一个文件移动到另一个文件夹并匹配其名称和日期格式 052020:
cd \\Sageshare\share
copy-item -path "\\Sageshare\share\Reconciliation\PDF Recon Center\DEA RECON 05292020.pdf" -destination "\\Sageshare\share\Reconciliation\Account Rec. Sheets\Separate Accounts\DEA" | Where-Object {$_.Name -like '*DEA RECON 052020*'}
由于这只是一个文件进入另一个目录中的命名文件夹,我将如何处理 400 个文件,每个文件进入一个相应的文件夹?如果我能做到这一点,那么我将遇到一个问题,即 05 日期需要更改为 06 等等,直到年底它还必须更改月份和年份。但我现在真的很想弄清楚第一部分。
解决方案
我为您制作了一个代码示例,向您展示如何在 Powershell 中工作。当你运行我的代码时,你不能破坏任何东西,直到你取消注释最后的两行
$sourceFolder = '\\Sageshare\share\Reconciliation\PDF Recon Center'
$targetFolder = '\\Sageshare\share\Reconciliation\Account Rec. Sheets\Separate Accounts\DEA'
$files = Get-ChildItem $sourcefolder -Filter *.pdf
$files | ForEach-Object {
echo ('Processing file ' + $_.Name)
$regex = [regex]::Match($_.Name, '\s([0-9]{2})([0-9]{2})([0-9]{4})[.]pdf')
echo ('Month ' + $regex.Groups[1].Value)
echo ('Date ' + $regex.Groups[2].Value)
echo ('Year ' + $regex.Groups[3].Value)
$targetFolderTmp = (Join-Path $targetFolder -childpath ($regex.Groups[3].Value + '\' + $regex.Groups[1].Value + '\' + $regex.Groups[2].Value))
Write-Host ('Copy to: ' + $targetFolderTmp)
#if(-not $targetFolderTmp) {mkdir $targetFolderTmp} uncomment if code is good
#copy-item -path $_.FullName -destination targetFolderTmp uncomment if code is good
}
推荐阅读
- java - Mapping between Swagger OpenAPI model and JPA entity
- typescript - DataSnapshot' 已声明,但其值永远不会被读取。ts(6133)
- c++ - Why can an enumeration inside class A (public) be accessed in class B using scope operator and class A name?
- java - 在onedrive中上传文件时触发azure容器注册表中的java函数以输出文件内容
- sqlite - 更改在 Kubernetes Pod 中运行的 SQLite 的默认存储位置
- google-play-console - How to publish an Alpha Internal Track app version from AppCenter to PlayStore
- java - Is there a way to put some images in an array (values)?
- regex - Looking for a fix in my RegEx to parse the correct value
- visual-studio-2013 - Visual Studio: Load SQL file from SQL Studio Unit Test
- ibm-doors - How to get dxl scripts to run faster