从 CSV 脚本重命名会重命名文件,但会尝试重复该过程

发布于 2024-12-04 08:04:47 字数 2058 浏览 0 评论 0原文

我有一个脚本,可以重命名从 CSV 中获取的文件,但是当它在已经执行成功的过程后尝试重新命名文件时,它会抛出错误

CSV 文件如下:

old      new
AC100    DC100
AC101    DC102

代码尝试:

$sourceDir = read-host "Please enter source Dir:"
$csvL = $sourceDir + "\files.csv"
$csv = import-csv $csvL
$files = get-childitem $sourceDir
$csv | % {
            ForEach( $file in $files){
            if($file = $_.old){

            $old = $sourceDir + "\" + $_.old

            Rename-Item $old $_.new
                    }       
                }
            }

我相信这与循环有关和 csv,但我不确定哪里出了问题,我以前也遇到过类似的问题。

这是错误的示例。

+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new

提前致谢, 克雷格

I have a script which renames files taken from a CSV but it throws an error when it trys to re-rename files after it has already carried out the successful proceedure

CSV file is like:

old      new
AC100    DC100
AC101    DC102

Code tried:

$sourceDir = read-host "Please enter source Dir:"
$csvL = $sourceDir + "\files.csv"
$csv = import-csv $csvL
$files = get-childitem $sourceDir
$csv | % {
            ForEach( $file in $files){
            if($file = $_.old){

            $old = $sourceDir + "\" + $_.old

            Rename-Item $old $_.new
                    }       
                }
            }

I beleive it is something to do with looping and the csv but im not sure where im going wrong, i have had similar issue before.

Here is a sample of the error.

+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new
Rename-Item : Cannot rename because item at 'C:\scripts\2039X.67438.TXT' does not exist.
At C:\scripts\renamerTim.ps1:18 char:15
+             Rename-Item  <<<< $old $_.new

thanks in advance,
Craig

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心舞飞扬 2024-12-11 08:04:47

我认为 ForEach( $file in $files) 是多余的,因为您已经使用 $csv | 遍历了每个“文件” %{ 命令。我会尝试这样的事情:

$csv | Foreach-Object { //I prefer to print the full commands in scripts
    $oldfile = $sourcedir + "\" + $_.old
    if (Test-Path $oldfile)
    {
        Rename-Item $oldfile ($sourcedir + "\" + $_.new)
    }
}

I think the ForEach( $file in $files) is redundant, since you already traverse each "file" with the $csv | % { command. I'd try something like:

$csv | Foreach-Object { //I prefer to print the full commands in scripts
    $oldfile = $sourcedir + "\" + $_.old
    if (Test-Path $oldfile)
    {
        Rename-Item $oldfile ($sourcedir + "\" + $_.new)
    }
}
泪是无色的血 2024-12-11 08:04:47

您有一个多余的嵌套循环。 $csv | <代码> %{ ... } 将已经循环遍历 csv 中的文件,然后 foreach 循环遍历文件夹中的每个文件。

试试这个:

  $sourceDir = read-host "Please enter source Dir:"
  $csvL = $sourceDir + "\files.csv"
  $csv = import-csv $csvL
  $files = get-childitem $sourceDir
  $csv | % {
     $oldpath = join-path $sourcedir $_.old
     $newpath = join-path $sourcedir $_.new

     rename-item $oldpath $newpath
  }

You have a redundant nested loop. $csv | %{ ... } will already loop through file in the csv, and then the foreach loops through each file in the folder.

Try this:

  $sourceDir = read-host "Please enter source Dir:"
  $csvL = $sourceDir + "\files.csv"
  $csv = import-csv $csvL
  $files = get-childitem $sourceDir
  $csv | % {
     $oldpath = join-path $sourcedir $_.old
     $newpath = join-path $sourcedir $_.new

     rename-item $oldpath $newpath
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文