2016-06-22 4 views
1

私はそれが欲しいと思うように次のコードを実行していますが、7未満のファイルのみを報告するロジックを追加して進めたい日。私はあなたがこのようなファイルを含むフォルダを持っている想像して、単に起動するには...PowerShell - 最後に保存されたファイル - 最後の7日間に絞り込みたい

Remove-Item ("c:\temp\output.txt") 
$Date = Get-Date 
Write-Output $Date | Out-file 'c:\temp\output.txt' -Append 

$word = New-Object -Com Excel.Application 
$word.Visible = $false #to prevent the document you open to show 
$filename = "Test User" 
$filelocation = "\\server\Storage\Applications\Employee Sheets\Test.xlsm" 
$doc = $word.Workbooks.Open($filelocation, $false, $true) # open in read only mode 
Write-Host ‘Currently processing’ $filename ‘/ 1 out of 45’ 

$binding = "System.Reflection.BindingFlags" -as [type] 
Foreach($property in $doc.BuiltInDocumentProperties) { 
try { 
    $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null) 
    if ($pn -eq "Last author") { 
    $lastSavedBy = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) 
     } 
    if ($pn -eq "Last Save Time") { 
     $lastSavedTime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) 
     } 
    } 
catch { } 
} 
$doc.Close($false) 
$word.Quit() 

$1 = "Last Saved By: " + $lastSavedBy 
$2 = "Last Saved Date: " + $lastSavedTime 

Write-Output "$filename $1 $2" | Out-file 'c:\temp\output.txt' -Append 

Invoke-Item ('c:\temp\output.txt') 

答えて

3

私はこのロジックでそれを行うことができるかどうかわからない:

Directory: C:\users\Stephen\Downloads 


Mode    LastWriteTime   Length Name 
----    -------------   ------ ---- 
-a----  6/22/2016 11:05 AM   85638 GOINGSTATELESS (1).jpg 
-a----  6/22/2016 11:03 AM   75529 GOINGSTATELESS.jpg 
-a----  6/17/2016 9:45 PM   738880 JavaSetup8u91.exe 
-a----  6/15/2016 2:08 PM   8390 CheckIfSCOM2012r2WasUpgradedToUR3.ps1 
-a----  6/15/2016 12:01 PM  7811072 LWAPlugin64BitInstaller32.msi 
-a----  6/15/2016 10:59 AM   45648 36491_SCOM_AppMonitoringPhase3_Change Order.docx 
d-----   6/6/2016 3:27 PM    ffmpeg-20160531-git-a1953d4-win64-static 
-a----   6/6/2016 3:24 PM  15201547 ffmpeg-20160531-git-a1953d4-win64-static.7z 
d-----   6/6/2016 3:17 PM    dedication.tar 
-a----   6/6/2016 2:18 PM  14344935 20160606_135011.mp4 

(年齢、あなたが見ることができるようにlastWriteTimeで定義されているように)は全面にあり、1週間未満の少数しかありません。

先週アクセスしたファイルのみを取得したい場合は、代わりにこのファイルを実行します。

dir | Where LastAccessTime -ge ((Get-Date).AddDays(-7)) 

    Directory: C:\users\Stephen\Downloads 


Mode    LastWriteTime   Length Name 
----    -------------   ------ ---- 
-a----  6/22/2016 11:05 AM   85638 GOINGSTATELESS (1).jpg 
-a----  6/22/2016 11:03 AM   75529 GOINGSTATELESS.jpg 
-a----  6/17/2016 9:45 PM   738880 JavaSetup8u91.exe 

大きなコードブロックを実行する前に、コードに同じロジックを適用するだけで済みます。このようなファイルのリストを絞り込むと、あなたはあなたの道にうまくいくと思います。

$LastWeekFiles = dir | Where LastAccessTime -ge ((Get-Date).AddDays(-7)) 
    ForEach ($file in $lastWeekFiles){ 

    #Insert your code here 

    } 
+0

それは多くの私を助けて、昨日のお時間をいただき、ありがとうございます。しかし、私は、Excelファイルを開くための正しい配置に巻き込まれています。私はそれらをすべて開くようにしてコードを実行しないでください。そうしないと、ファイルを開くことができず、残りのファイルをその1つのファイルで処理します。以下のコードを参照してください。あなたの助けをもう一度ありがとう! –

+0

ああ、私は問題を見る。 ForEach($ lastWeekFilesの$ file)行を5行目まで移動します。 $ LastWeekFilesを開始する行のすぐ後にする必要があります。 – FoxDeploy

0
Remove-Item ("c:\temp\output.txt") 
$Date = Get-Date 
Write-Output $Date | Out-file 'c:\temp\output.txt' -Append 
$LastWeekFiles = Get-ChildItem "\\server\Storage\Applications\"| Where LastWriteTime -ge ((Get-Date).AddHours(-58)) 
$word = New-Object -Com Excel.Application 
$word.Visible = $false #to prevent the document you open to show 

$binding = "System.Reflection.BindingFlags" -as [type] 
$doc = $word.Workbooks.Open("\\server\Storage\Applications\" + $file, $false, $true) # open in read only mode 

ForEach ($file in $lastWeekFiles){ 

ForEach($property in $doc.BuiltInDocumentProperties){ 

try{ 

$pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null) 

if ($pn -eq "Last author") { 
$lastSavedBy = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) 
    } 
if ($pn -eq "Last Save Time") { 
    $lastSavedTime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null) 

    } 

} 


catch{} 

} 

} 

$doc.Close($false) 
$word.Quit() 
関連する問題