Last accessed folder information

How to get the last access  files greater than 1 year along with size of files.
Who is Participating?
Bill PrewCommented:
Not sure if you were interested in VBS, but since this question is tagged in that zone, here's an idea of what you can do there.  I typically like to send the output of scans like this to a CSV and then load in to Excel for sorting, formatting, printing, etc.  So, save the following as a VBS, and then run it passing it the starting folder for the output.  Redirect the output to a CSV file, and then open in Excel.

cscript //nologo EE28306189.vbs c:\yourfolder > report.csv

' Define needed filesystem object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Get folder name to list off the command line, make sure it's valid
If (WScript.Arguments.Count > 0) Then
    strFolder = Wscript.Arguments(0)
    If Right(strFolder, 1) = "\" Then strFolder = Left(strFolder, Len(strFolder)-1)
    If Not objFSO.FolderExists(strFolder & "\") Then
        WScript.Echo "Specified folder does not exist."
    End If
    WScript.Echo "No folder name specified to list."
End If

' Header row
Wscript.Echo Quote("Path") & "," & Quote("Size") & "," & Quote("Accessed")

' Call recursive routine to list old files
ListFiles objFSO.GetFolder(strFolder)

' Recursively list files older than number of days specified
Sub ListFiles(objFolder)
    ' List any files older than specified time from this folder
    For Each objFile In objFolder.Files
        If DateDiff("y", objFile.DateLastAccessed, Now) > 0 Then 
            Wscript.Echo Quote(objFile.Path) & "," & objFile.Size/1024 & "," & Quote(objFile.DateLastAccessed)
        End If

    ' List all older files in any subfolders of this one
    For Each objSubFolder In objFolder.Subfolders
        ListFiles objSubFolder
End Sub

' Add surrounding double quotes to a string
Function Quote(s)
   Quote = Chr(34) & s & Chr(34)
End Function

Open in new window

The following will get files that were last accessed more than a year ago.  Size is returned by default, so no need to do anything special.
$startfolder = "c:\whatever"
$age = (Get-Date).AddYears(-1)
Get-ChildItem $startfolder -Recurse | Where {$_.LastAccessTime -lt $age}

Open in new window

Output gives like below:
  Directory: E:\Software\SQL2008\x64\setup\sql_engine_core_inst_msi\pfiles\sqlservr\mssql.x\mssql\binn\dlltmp32

Mode                LastWriteTime     Length Name                                                                                                                                      
----                -------------     ------ ----                                                                                                                                      
-a---          4/3/2010   7:21 AM      73568 dws5mxvv.dll                                                                                                                              
-a---          4/3/2010   7:21 AM      47456 lo7viuxq.dll                                                                                                                              

    Directory: E:\Software\SQL2008\x64\setup\sql_engine_core_inst_msi\pfiles\sqlservr\mssql.x\mssql\binn\dlltmp64

Mode                LastWriteTime     Length Name                                                                                                                                      
----                -------------     ------ ----                                                                                                                                      
-a---          4/3/2010   6:27 AM      79200 4a0g_qam.dll                                                                                                                              
-a---          4/3/2010   6:27 AM      77152 a34tgahi.dll                                                                                                                              

    Directory: E:\Software\SQL2008\x64\setup\sql_engine_core_inst_msi\pfiles\sqlservr\mssql.x\mssql\binn\template

Mode                LastWriteTime     Length Name                                                                                                                                      
----                -------------     ------ ----                                                                                                                                      
-a---          4/2/2010   1:11 PM    4194304 master.mdf                                                                                                                                
-a---          4/2/2010   1:11 PM     524288 mastlog.ldf                                                                                                                              
-a---          4/2/2010   1:11 PM    1310720 model.mdf                                                                                                                                
-a---          4/2/2010   1:11 PM     524288 modellog.ldf                                                                                                                              
-a---          4/2/2010   1:11 PM   11534336 msdbdata.mdf                                                                                                                              
-a---          4/2/2010   1:11 PM     524288 msdblog.ldf  

instead i like have full pathname and size in kb and last accessed time not lastwritetime
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

You just need to use Select-Object cmdlet to get the required properties..
Get-ChildItem "E:\Files" -Recurse | 
  Where {$_.LastAccessTime -lt (Get-Date).AddYears(-1) -and !($_.PSIsContainer) } |
  Select FullName,LastAccessTime,@{N="Size KB";E={"{0:N2}" -f ($_.Length /1KB)}} |
Export-Csv C:\report.csv -nti

Open in new window

The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 char
 i'm getting error like above.
Bill PrewCommented:
Who's solution were you replying to?

Long filenames are a problem typically in any scripting language, and at best you're probably only going to be able to skip over those.

To deal with the character limitation you may use robocopy. But with robocopy you cannot use the property. You can refer the following link to find a sample function which use robocopy. In the code you need replace /MaxAge and /MinAge with /MAXLAD and /MINLAD to use LastAccessTime instead of LastWriteTime.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.