0

ループから CSV ファイルに出力する際の正しい方法は何ですか。私の目標は、1 か月以上アクセスされていない一連の拡張子に一致するすべてのファイルの属性をチェックし、後で読み取るために csv ファイルに出力することです。

最初のファイルの情報のみを出力します。そして次のエラー:

Add-Member : Cannot add a member with the name "FileName" because a member with that name already exists. If you want to overwrite the member a
nyway, use the Force parameter to overwrite it.
At C:\Users\claw\Desktop\checkFileAge.ps1:10 char:25
+             $out2csv | add-member <<<<  -MemberType NoteProperty -Name FileName -Value $i.fullname
    + CategoryInfo          : InvalidOperation: (@{FileName=C:\u...012 9:33:46 AM}:PSObject) [Add-Member], InvalidOperationException
    + FullyQualifiedErrorId : MemberAlreadyExists,Microsoft.PowerShell.Commands.AddMemberCommand

これが私の例です:

    $out2csv = New-Object PSObject;

foreach ($i in get-childitem c:\users -recurse -include *.doc, *.xls, *.ppt, *.mdb, *.docx, *.xlsx, *.pptx, *.mdbx, *.jpeg, *.jpg, *.mov, *.avi, *.mp3, *.mp4, *.ogg) 
    {if ($i.lastaccesstime -lt ($(Get-Date).AddMonths(-1))) 
        {
            $out2csv | add-member -MemberType NoteProperty -Name FileName -Value $i.fullname
            $out2csv | add-member -MemberType NoteProperty -Name LastAccess -Value $i.LastAccessTime
        } 
    } $out2csv | Export-Csv "C:\FileAccessInformation.csv" -NoTypeInformation -Force
4

1 に答える 1

2

このようなことを試してください。これは、もう少し標準的な PowerShell です。

$ext = '*.doc', '*.xls',...
Get-ChildItem C:\Users -r -inc $ext | 
    Where {$_.LastAccessTime -lt [DateTime]::Now.AddMonths(-1)} |
    Select FullName, LastAccessTime |
    Export-Csv -NoTypeInformation -Force C:\FileAccessInformation.csv
于 2012-09-28T00:03:32.807 に答える