Getting Folder date/time (Full Version)

All Forums >> [Scripting Technologies] >> Windows PowerShell



Message


gmaynard -> Getting Folder date/time (7/3/2008 3:57:43 PM)

I am trying to change the equivalent of a folder's $folder.creationtime and $folder.LastAccessTime. Unfortunately, these properties only appear to apply to files. Any ideas on how I can modify these attributes?




martylist -> RE: Getting Folder date/time (7/3/2008 8:44:18 PM)


The CreationTime and LastAccessTime properties apply to folders as well as files.

>get-command md

CommandType     Name     Definition
-----------     ----     ----------
Function        md       param([string[]]$paths); New-Item -type director...

>$folder = md "C:\TestFolder"
>$folder.CreationTime

Thursday, July 03, 2008 6:36:19 PM

>$folder.CreationTime = "Wednesday, July 02, 2008 6:36:19 PM"
>$folder.CreationTime

Wednesday, July 02, 2008 6:36:19 PM

>$folder.LastAccessTime

Thursday, July 03, 2008 6:36:19 PM

>$folder.LastAccessTime = "07/01/2008 1:00:00 AM"
>$folder.LastAccessTime

Tuesday, July 01, 2008 1:00:00 AM


Post an example of what you're trying to do.




gmaynard -> RE: Getting Folder date/time (7/7/2008 10:00:44 AM)

The line of code is:
get-Childitem $Path2 -include * -recurse | foreach ($_) {$_.creationtime = $OrgCreateTime; $_.LastAccessTime = $OrgModTime}

When my script gets to this line, I get the following errors:
Exception setting "LastAccessTime": "Cannot convert null to type "System.DateTime"." At H:\Scripts\Powershell\ITMUcabparse3.ps1:374 char:94 + get-Childitem $Path2 -include * -recurse | foreach ($_){$_.creationtime = $OrgCreateTime; $_.L <<<< astAccessTime = $OrgModTime}
Exception setting "CreationTime": "Cannot convert null to type "System.DateTime"."At H:\Scripts\Powershell\ITMUcabparse3.ps1:374 char:60 + get-Childitem $Path2 -include * -recurse | foreach ($_){$_.c <<<< reationtime = $OrgCreateTime; $_.LastAccessTime = $OrgModTime}

When I look at $Path2, the files have the date/time of $OrgModTime, but the folders are still set to the time that the folders were created.




jhicks -> RE: Getting Folder date/time (7/7/2008 1:13:12 PM)

It looks like the variable $OrgModTime isn't getting set properly so when you try to use it, PowerShell complains.  Are you trying to touch all folders and files with a new creation time?




gmaynard -> RE: Getting Folder date/time (7/7/2008 1:18:37 PM)

I am trying to set all files and folders in $Path2 with the same time as another reference file. I think $OrgModTime  is being set correctly because the files under $Path2 are being reset to the desired date/Time. It is only the folders that are not getting it.




gmaynard -> RE: Getting Folder date/time (7/7/2008 1:25:19 PM)

I mentioned in my first post that it does not appear that these properties (creationtime & LastAccessTime) are available under folders because a "$file | Get-member" lists these properties, but "$folder | get-member" does not.




jhicks -> RE: Getting Folder date/time (7/7/2008 1:42:31 PM)

Are you trying to do something like this?

$file="c:\test\test.txt"

$org=Get-Item $file
Get-ChildItem "c:\test\foo" -recurse | foreach {
$_.CreationTime=$org.CreationTime
$_.LastWriteTime=$org.LastWritetime
$_.LastAccesstime=$org.LastAccessTime
}

all the files and subfolders under c:\test\foo will be modified with the corresponding property values of test.txt.




gmaynard -> RE: Getting Folder date/time (7/7/2008 2:11:32 PM)

Pretty close. Actually my code looks like this

$InputPath = "C:\blah\file.txt"
$Path2 = "C:\blahBlah"
$OrgCreateTime = $InputPath.creationtime
$OrgModTime = $InputPath.LastAccessTime
get-Childitem $Path2 -include * -recurse | foreach ($_) {$_.creationtime = $OrgCreateTime; $_.LastAccessTime = $OrgModTime}





gmaynard -> RE: Getting Folder date/time (7/7/2008 2:20:58 PM)

Well Marty, your method sort of works when creating a folder, but it does not appear to work on folders that already exist.

PS C:\> $testfolder = md "E:\test"
PS C:\> $testfolder.CreationTime
Monday, July 07, 2008 2:13:06 PM

PS C:\> $testfolder.LastAccessTime
Monday, July 07, 2008 2:13:11 PM

PS C:\> $Winfolder = "C:\Windows"
PS C:\> $Winfolder.CreationTime
PS C:\> $Winfolder.LastAccessTime
PS C:\> $testfolder.CreationTime = $Winfolder.CreationTime
Exception setting "CreationTime": "Cannot convert null to type "System.DateTime "." At line:1 char:13 + $testfolder.C <<<< reationTime = $Winfolder.CreationTime





jholbach -> RE: Getting Folder date/time (7/7/2008 2:24:03 PM)

Try

$Winfolder = get-item "C:\Windows"





gmaynard -> RE: Getting Folder date/time (7/7/2008 2:31:32 PM)

Thanks jholbach, that worked:

PS C:\> $testfolder = md "E:\test"
PS C:\> $testfolder.CreationTime
Monday, July 07, 2008 2:13:06 PM

PS C:\> $Winfolder = get-item "C:\Windows"
PS C:\> $testfolder.CreationTime
Tuesday, September 27, 2005 8:59:20 AM

PS C:\> $testfolder.CreationTime = $Winfolder.CreationTime
PS C:\> $testfolder.CreationTime
Tuesday, September 27, 2005 8:59:20 AM


So why is it working for my files, but not my folders in my code? Is it possible the date/Time format is different between files and folders?




gmaynard -> RE: Getting Folder date/time (7/7/2008 2:51:51 PM)

I think I answered my own question. I created the folder structure of $Path2, but I copied the files. Since they are within the same volume, the files retained their original values, but the folders are not being changed because I did not assign my orginal file with the "get-item" command.

Thanks for your help guys.




martylist -> RE: Getting Folder date/time (7/7/2008 5:02:44 PM)

You're welcome.  Sounds like you have it working, but just to clarify, when you set the variable to a folder name in quotes ("C:\Windows") that just puts a string of text inside the variable, not a real folder object.  For example: >$Winfolder = "C:\Windows"
>$Winfolder.GetType() IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object >$Winfolder.LastAccessTime
>$Winfolder = Get-Item "C:\Windows"
>$Winfolder.GetType() IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DirectoryInfo                            System.IO.FileSystemInfo
>$Winfolder.LastAccessTime Wednesday, June 11, 2008 3:08:07 AM  




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.1914063