powershell - 修复脚本以允许扫描每一行的所有字段以查找该字段是否包含目标文件夹名称
问题描述
这是解决先前问题的深化扩展
由于输入的 csv 文件不一致,我不能以这种方式使用它。要移动到的文件夹并不总是在同一列中,因此任何尝试将其用作输入的代码都会遇到值与我要移动到的文件夹不对应的问题。它只是在需要文件夹名称的地方读取垃圾(“垃圾邮件”)。唯一的方法是检查每一行的所有字段,以查找该字段是否包含您可以使用的目标文件夹名称。这意味着很多测试路径行
这是有罪的代码部分
Foreach ($fileName in $_.Group.FileName) {
$ValidFileName = $filename -replace $invalid
$targetFile = Join-Path -Path $TargetFolder -ChildPath $fileName
当我遍历 FileName 列时,错误明确告诉它移动这些文件夹。Foreach ($fileName in $_.Group.FileName) {..
这是阅读:(1959 10) Showcase Presents n 22,(1959 12) Showcase Presents n 23,alfa, da definire.
我的要求:由于需要检查每一行的所有字段,所以我要求对此脚本进行代码编辑。另外,如果这意味着很多Test-Path
行,我想别无选择。但是,下面的这个脚本不会创建文件夹并移动任何东西,所以我的请求是尝试修复它
$csvpath = 'C:\temp\temp.csv'
$invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
$sourcePath = 'C:\temp\'
Import-Csv C:\temp\temp.csv -Header Title,FileName,Link -Delimiter ';' |
Group-Object Title |
Foreach {
# I prefer to add a trailing slash to folder names
$TargetFolder = Join-Path -Path $sourcePath -ChildPath (($_.Name -replace $invalid)+'\')
# We don't have to create the new folders, because -Force will create them for us
Foreach ($fileName in $_.Group.FileName) {
$ValidFileName = $filename -replace $invalid
$targetFile = Join-Path -Path $TargetFolder -ChildPath $fileName
# Write your values to the console - Make sure the folder is what it should be
Write-Output "Moving '$targetFile' to '$TargetFolder'"
Move-Item $targetFile $TargetFolder -Force -WhatIf
}
}
解决方案
我什至不愿对此发表评论,因为您只是没有接受我们对所有先前帖子的任何建议,而只是重复我们在新问题中所说的话。我们在这里帮助您编写代码,而不是为您编写代码,但是,学习不是犯罪,所以我假设您通常只是不熟悉 powershell。
$csvpath = 'C:\tomp\temp.csv'
#$invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
$sourcePath = 'C:\tomp\'
Import-Csv $csvpath -Header Title,FileName,Link -Delimiter ',' |
Group-Object Title |
ForEach-Object `
-Begin {
$FoldersLocation = Get-ChildItem -Path C:\tomp -Directory
$Source_Folder = ($FoldersLocation.Parent.FullName | Select-Object -Unique)
#Create array list to hold both columns
[System.Collections.ArrayList]$CombinedRows = @()
} `
-Process {
# Create folder if it doesn't exist
$Destination = Join-Path -Path $Source_Folder -ChildPath $_.Name
if ((Test-Path -Path $Destination) -eq $false) {
"Created: $Destination"
#$null = New-Item -Path ($FoldersLocation.Parent.FullName | Select -Unique) -Name $_.Name -ItemType Directory
}
#region Combine two columns
Foreach ($fileName in $_.Group.FileName) {
$null = $CombinedRows.Add($fileName)
}
Foreach ($link in $_.Group.Link) {
$null = $CombinedRows.Add($link)
}
#endregion
} `
-End {
Foreach ($name in $FoldersLocation) {
if ($name.Name -in $CombinedRows) {
if ($name.Name -match "Showcase"){
# No need for output when supplying the -verbose switch
"Moving: '$($Name.FullName)' to 'C:\tomp\Lanterna Verde - Le Prime Storie'"
# Move-Item -Path $Name.FullName -Destination "C:\tomp\Lanterna Verde - Le Prime Storie"
}
else {
# No need for output when supplying the -verbose switch
"Moving: '$($Name.FullName)]' to 'C:\tomp\Batman DC'"
# Move-Item -Path $Name.FullName -Destination "C:\tomp\Batman DC"
}
}
}
}
它归结为对流逻辑的控制。如果满足某个条件,则执行此操作,否则执行其他操作。添加了非常少的内联注释,因为其中大部分都是不言自明的。
我建议阅读一些 powershell,而不是希望其他人可以为你做所有事情。
推荐阅读
- c++ - 使用最小堆的优先级队列,能够在 O(log(n)) [向量方法] 处删除任何节点
- flutter - Flutter 想要导航到谷歌地图
- javascript - 使用 Ajax 将一些图像插入 div 后,猫头鹰轮播不显示
- jquery - vue 使用的 jquery mCustomScrollBar 问题
- bash - 在 Bash 中使用变量赋值的动态列表调用命令
- django - Django中的单元测试用例未执行
- pyserial - 如何从传入的串行端口数据创建动画图
- wordpress - 一页和多个不相关的网址?
- google-apps-script - “选择事件源”部分中缺少“来自表单”选项
- python - 在上下文管理器中修改 globals() 并在另一个文件中使用它