-1

我们的一位开发人员安装的系统导致电子邮件附件的文件一次又一次地复制到我们的服务器。

它将唯一的 GUID 附加到文件名的前面,并导致大约 35,000 个具有不同 GUID 的重复项。

我有一个我们想要保留的所有文件的列表,但需要一个脚本来引用这个文件并删除所有不在这个引用文件中的文件。

有人能帮忙吗?

4

1 回答 1

0

您的描述中缺少一些细节,所以这是我的假设:

附加文件遵循与此类似的形式:

62dc92e2-67b0-437e-ba06-bcbf922f48e8file14.txt
66e7cbb3-873a-429b-b4c3-46597b5b5828file2.txt
68c426a3-49b9-4a80-a3e8-ef73ac875791file13.txt
etc.

您要保留的文件列表如下所示:

file1.txt
file12.txt
file9.txt
file5.txt

代码:

# list of files you want to keep
$keep = get-content 'keep.txt'

# directory containing files
$guidfiles = get-childitem 'c:\some\directory'

# loop through each filename from the target directory
foreach($guidfile in $guidfiles) {
    $foundit = 0;

    # loop through each of the filenames that you want to keep 
    # and check for a match
    foreach($keeper in $keep) {
        if($guidfile -match "$keeper$") {
            write-output "$guidfile matches $keeper"

            # set flag that indicates we don't want to delete file
            $foundit = 1
            break
        }
    }

    # if flag was not set (i.e. no match to list of keepers) then 
    # delete it
    if($foundit -eq 0) {
        write-output "Deleting $guidfile"

        # As a sanity test, I'd suggest you comment out the line below when 
        # you first run the script.  The output to stdout will tell you which 
        # files would get deleted.  Once you're satisfied that the output
        # is correctly showing the files you want deleted, then you can 
        # uncomment the line and run it for real.
        remove-item $guidfile.fullname
    }
}

其他注意事项: 您提到这“导致了大约 35000 次重复”。听起来同一个文件可能被复制了不止一次。这意味着您可能还想删除要保留的文件的重复项,以便您只拥有一个。我无法从您的描述中确定这是否属实,但也可以修改脚本来做到这一点。

于 2012-10-22T19:55:47.177 回答