How do I most efficiently move, rename files and log this action?

前端 未结 1 1846
旧时难觅i
旧时难觅i 2021-01-28 01:23

I have the following CSV list (in reality 1000s of lines):

needle,code
123456,AB
121212,BB
33333333,CVV

And I have a directory (C:\\old_files) c

1条回答
  •  粉色の甜心
    2021-01-28 02:04

    You need to actually check if you have a match before copying the matching file.

    Get-ChildItem "C:\old_files" | ForEach-Object {
      $n = ($_.Name -split '.')[1]
      if ($pair[$n]) {
        $oldname = $_.FullName
        $newname = Join-Path 'C:\new_files' ($pair[$n] + $_.Name)
        Copy-Item $oldname $newname
      }
    }
    

    Do the logging after the copy operation:

    Copy-Item $oldname $newname
    if ($?) {
      # log success information here
    } else {
      # log error information here
    }
    

    0 讨论(0)
提交回复
热议问题