2011-04-23 46 views
0

我正在尋找一種方法來查找三個或更多的同名文件,但創建與另一個應用程序。然後,下一個操作會比較所有三個文件,以查看它們是否在同一日期創建,並最終將該日期與當前操作系統日期進行比較。找到三個相同名稱的文件,但創建不同的應用程序

+1

你的意思是「與其他應用程序創建的」?例如,你的意思是說你有一個名爲「Foo」的文件,由Word,Excel和記事本創建 - 因此文件擴展名爲foo.doc,foo.xls和foo.txt? – Goyuix 2011-04-23 19:08:12

回答

1

作爲了部分答案,因爲我不知道你的意思有相同的名稱...

,看文件是否是在同一天創建的,你可以比較每個參考的創建時間屬性:

# Use Get-Item to retrieve FileInfo for two files 
PS C:\> $a = Get-Item 'a.txt' 
PS C:\> $b = Get-Item 'b.txt' 
# Compare the DateTime field when they were created 
PS C:\> $a.CreationDate -eq $b.CreationDate 
False 
# Compare just the 'Date' aspect of each file ignoring the time 
PS C:\> $a.CreationDate.Date -eq $b.CreationDate.Date 
True 

您會注意到創建日期包含一個時間元素,因此除非它們確實完全相同,否則可能得不到預期的結果。要去除時間元素,只需將.Date屬性添加到任何DateTime字段。

比較對操作系統日期和時間:

# store the OS Date and Time for easier reference 
PS C:\> $now = [DateTime]::Now 
PS C:\> $today = [DateTime]::Today 
# Compare using the stored values 
PS C:\> $a.CreationDate.Date -eq $now 
False 
PS C:\> $a.CreationDate.Date -eq $today 
True 
相關問題