根据过滤器和名称匹配文件

时间:2019-11-21 14:58:01

标签: csv replace find compare

我正在扫描驱动器C:零长度文件。如果找到一个,则用D上同一目录中的同名文件(长度不为零)替换它: 我有它并且有效:

$Path = "C:\zeros"
# Recurse through the files to scan for 0-length files
    $files = Get-ChildItem -Path $Path -Recurse | Where-Object { $_.Length -eq 0 }

# Export some of the details of the file to a CSV
# Using "FullName" gets us the path+filename
    $files | Select-Object FullName, Length | Export-Csv "c:\log\zero-length.csv" -NoTypeInformation

# Based on csv, replace files from the non-zero files
# Edit drive letters for correct source/destination
    foreach ($file in $files)
    {
        $replacementFiles = ($file.FullName).Replace("C:", "D:")
        Copy-Item -Path $replacementFiles -Destination $file.FullName -Force
    }

这一切都很好,我在csv中列出了零长度文件,它是c:\ path + filename并列出了一个长度。例如:c:\ zeros \ test.txt,0

这是我无法进一步了解的地方:我还需要将这些内容添加到csv中:      替换文件d:\ patch + filename和长度OR指示它是否不存在。同时指出我们是否已成功将文件从D:复制到C:

目标类似于: c:\ file.txt,0,d:\ file.txt,12,复制或错误(忽略)

非常感谢您的帮助。我已经尝试了2天的最终作品,现在已经很困惑了:)

0 个答案:

没有答案