views:

47

answers:

1

Hi Guys,

I need a script that can gather folders/files information from large drives (600GB to 1TB). The info that I will need are:

  • Full name/path of the file
  • File Size
  • Date Created
  • Date Modified
  • Date last accessed.

So far I have the code below:

dir 'e:\' -recurse | 
     select FullName,Length,CreationTime,LastWriteTime,LastAccessTime | 
     Export-CSV e:\test\testit.csv -notype

Would it be possible to get the script adapted so as it can search on ‘If modified date is equal to ‘xxx’ days or older, then output data to csv..

The xxx would be the figure – i.e 365 for 1 year or more old.. or xxx would be the figure – i.e 730 for 2 years or more old..

Also would it be possible to modify the headers for the columns that it outputs on CSV file? Thanks

+1  A: 

For the first part of question, you might use

# fixed date
Get-ChildItem c:\temp\ | ? { $_.LastWriteTime -lt [datetime]'2009-02-23' }
# variable depending on current date; it returns only items that are older than 365 days
Get-ChildItem c:\temp\ | ? { $_.LastWriteTime -lt [datetime]::Now.AddDays(-365) }

As for modifying the headers.. I is possible to select items with different names but with the same content (~rename the properties):

Select-Object @{Name='ItemName'; Expression={$_.FullName } }, 
        @{Name='Write'; Expression={$_.LastWriteTime } }, 
        @{Name='Access'; Expression={$_.LastAccessTime } }

Note that there is a script block where you can do whatever you want. You can e.g. format the dates {$_.LastAccessTime.ToString('yyyy-MM-dd') }


If I put it togeter (there are only 3 properties 'renamed')

Get-ChildItem e:\ -rec | 
    ? { $_.LastWriteTime -lt [datetime]::Now.AddDays(-365) } |
    Select-Object @{Name='ItemName'; Expression={$_.FullName } }, 
        @{Name='Write'; Expression={$_.LastWriteTime } }, 
        @{Name='Access'; Expression={$_.LastAccessTime } } |
    Export-Csv e:\test\testit.csv -notype
stej
Thanks mate! that worked perfect :)now another thing is that I have some documents which are encrypted, is there a way that it could either skip those documents or actually go through them as well?Thanks
Khalid
You are welcome ;) It depends on what 'encrypted' means.
stej
bascially at work 'my documents' folder is encrypted so that the files can only be accessed by myself or an administrator.
Khalid
Hm, I don't know now. Try to ask a new question with more info and maybe with more information I'll find some answer or somebody else.
stej
sorry again, another quick issue, if the file name/path is longer than 248 characters it gives an error, is there any way to prevent it from that?Thanks
Khalid
What is the error message?
stej
Get-ChildItem : The specified path, file name, or both are too long. the fully qualified file name must be less than 260 characters and the directory must be less than 248 characters
Khalid
From what I know you can't really work around that one, all the System.IO .Net classes respect the traditional MAX_PATH value(which is 260 chars). For some info check http://www.codinghorror.com/blog/2006/11/filesystem-paths-how-long-is-too-long.html
stej