myITforum.com Community Forum myITforum.com Community Forum

Home  Forums  Blogs  Live Support chat  Search Articles  Wiki  FAQ  Email Lists  Register  Login  My Profile  Inbox  Address Book  My Subscription  My Forums 

Photo Gallery  Member List  Search  Calendars  FAQ  Ticket List  Log Out

All Forums RSS Feed Subscription:


  


Getting Folder date/time

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> Windows PowerShell >> Getting Folder date/time Page: [1]
Login
Message << Older Topic   Newer Topic >>
Getting Folder date/time - 7/3/2008 3:57:43 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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?
Post #: 1
RE: Getting Folder date/time - 7/3/2008 8:44:18 PM   
martylist

 

Posts: 40
Score: 0
Joined: 6/10/2005
From: Colorado, USA
Status: offline

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.

< Message edited by martylist -- 7/3/2008 8:46:49 PM >

(in reply to gmaynard)
Post #: 2
RE: Getting Folder date/time - 7/7/2008 10:00:44 AM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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.

(in reply to martylist)
Post #: 3
RE: Getting Folder date/time - 7/7/2008 1:13:12 PM   
jhicks

 

Posts: 3
Score: 0
Joined: 12/20/2004
From: Central NY
Status: offline
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?

_____________________________

Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com
follow me: http://www.twitter.com/JeffHicks



(in reply to gmaynard)
Post #: 4
RE: Getting Folder date/time - 7/7/2008 1:18:37 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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.

(in reply to jhicks)
Post #: 5
RE: Getting Folder date/time - 7/7/2008 1:25:19 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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.

(in reply to gmaynard)
Post #: 6
RE: Getting Folder date/time - 7/7/2008 1:42:31 PM   
jhicks

 

Posts: 3
Score: 0
Joined: 12/20/2004
From: Central NY
Status: offline
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.


_____________________________

Jeffery Hicks
Microsoft PowerShell MVP
http://blog.sapien.com
http://www.scriptinganswers.com
follow me: http://www.twitter.com/JeffHicks



(in reply to gmaynard)
Post #: 7
RE: Getting Folder date/time - 7/7/2008 2:11:32 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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}


(in reply to jhicks)
Post #: 8
RE: Getting Folder date/time - 7/7/2008 2:20:58 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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


(in reply to martylist)
Post #: 9
RE: Getting Folder date/time - 7/7/2008 2:24:03 PM   
jholbach

 

Posts: 12
Score: 0
Joined: 8/31/2003
Status: offline
Try

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


(in reply to gmaynard)
Post #: 10
RE: Getting Folder date/time - 7/7/2008 2:31:32 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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?

(in reply to jholbach)
Post #: 11
RE: Getting Folder date/time - 7/7/2008 2:51:51 PM   
gmaynard

 

Posts: 23
Score: 0
Joined: 9/9/2004
Status: offline
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.

(in reply to gmaynard)
Post #: 12
RE: Getting Folder date/time - 7/7/2008 5:02:44 PM   
martylist

 

Posts: 40
Score: 0
Joined: 6/10/2005
From: Colorado, USA
Status: offline
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  

(in reply to gmaynard)
Post #: 13
Page:   [1]
All Forums >> [Scripting Technologies] >> Windows PowerShell >> Getting Folder date/time Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts



  
Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.297