-1

開発者の 1 人が導入したシステムにより、メールに添付されたファイルがサーバーに何度もコピーされました。

ファイル名の前に一意の GUID を追加し、異なる GUID で約 35,000 の重複を引き起こしました。

保持したいすべてのファイルのリストがありますが、このファイルを参照し、この参照ファイルにないすべてのファイルを削除するスクリプトが必要です。

誰でもこれを手伝ってもらえますか?

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 の重複を引き起こした」と述べました。同じファイルが複数回コピーされているようです。つまり、保持したいファイルの重複を削除して、1 つだけにすることもできます。あなたの説明からそれが正しいかどうかはわかりませんが、スクリプトを変更してそれを行うこともできます。

于 2012-10-22T19:55:47.177 に答える