powershell - “找不到路径,因为它不存在”,但要处理的文件夹数与文件文本中的文件夹数不同
问题描述
我有这种情况
我不想在使用脚本之前手动更改temp.csv 。如果是脚本对我执行此操作,那我没问题
我可能需要在我的文件夹名称中添加一个斜杠,因为我要再次加入它们。我尝试将我的值写回控制台,看看它们是否真的正确,另外我可以删除一些路径检查,因为你已经在使用-Force
:
$csvpath = 'C:\tomp\temp.csv'
$invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
$sourcePath = 'C:\tomp\'
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 $sourcePath -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
}
}
所以输出看起来像:
Moving 'C:\tomp\(1959 10) Showcase Presents n 22' to 'C:\tomp\Lanterna Verde - Le Prime Storie\'
What if: Performing the operation "Move File" on target "Item: C:\tomp\(1959 10) Showcase Presents n 22 Destination: C:\tomp\Lanterna Verde - Le Prime Storie\".
但是脚本由于不同的原因不起作用。我得到这个输出
在 C:\tomp
路径内我已经有 4 个可用的文件夹,这些是
(1959 10) Showcase Presents n 22
(1959 12) Showcase Presents n 23
(1959 14) Showcase
le prime avventure
在同一路径上,我有带有此文本的temp.csv文件
Lanterna Verde - Le Prime Storie;(1959 10) Showcase Presents n 22;
Lanterna Verde - Le Prime Storie;(1959 12) Showcase Presents n 23;beta;
Lanterna Verde - Le Prime Storie;alfa;(1959 14) Showcase;gamma;
Batman DC;da definire;le prime avventure;
我希望脚本以这种方式移动
Lanterna Verde - Le Prime Storie
|
+-- (1959 10) Showcase Presents n 22
+-- (1959 12) Showcase Presents n 23
+-- (1959 14) Showcase
Batman DC
|
+--- le prime avventure
问题:
a) 脚本不创建 2 个新文件夹,也不在
b) 脚本进程中beta
移动文件夹alfa
,da definire
作为要移动的文件夹。但这是错误的,因为C:\tomp
我只有 4 个文件夹而不是 7 个文件夹!我不想要那个过程beta
,,alfa
da definire
解决方案
推荐阅读
- python - 下标运算符的不同位置导致熊猫 groupby 的结果相同
- sql - 订单管理 SQL 模式
- python - ImportError: DLL load failed: 找不到指定的模块?
- arrays - ruby 子数组添加了哪些内存要求?
- powershell - 如何为 Microsoft 365/AzureAD/Exchange Online 自动化的 powershell 脚本进行身份验证?
- google-compute-engine - 实例组:资源“us-instance-group-1”超过限制“QUOTA_FOR_INSTANCES”。限制:8.0
- javascript - 会话存储值未显示在第二页中
- c# - 过滤除一个以外的所有控制器的 .net 核心
- python - 排序函数
- python - Pandas 使用字典追加行以某种方式为某些新字典项添加列