首页 > 解决方案 > “找不到路径,因为它不存在”,但要处理的文件夹数与文件文本中的文件夹数不同

问题描述

我有这种情况

在此处输入图像描述

我不想在使用脚本之前手动更改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移动文件夹alfada definire作为要移动的文件夹。但这是错误的,因为C:\tomp我只有 4 个文件夹而不是 7 个文件夹!我不想要那个过程beta,,alfada definire

标签: powershellbatch-file

解决方案


推荐阅读