|
|
||||||
|
#1
|
|
|
|
|
Hallo guys and happy new year to everybody. :D
Sometimes to make tests I need to assign different creation dates between a specific range to my files. I'm looking for a way to do it with powershell. I found this article 'Generate Random Dates in PowerShell « saadware' (http://blog.saadware.com/2008/06/12/...in-powershell/) but i don't know how to apply this code to each file within a folder. Thanks in advance. |
|
|
|
#2
|
|
|
|
|
On Dec 30, 10:29 am, sardinian_guy <gu> wrote:
> Hallo guys and happy new year to everybody. :D > Sometimes to make tests I need to assign different creation dates > between a specific range to my files. I'm looking for a way to do it > with powershell. > > I found this article > 'Generate Random Dates in PowerShell « saadware' > ([..]) > > but i don't know how to apply this code to each file within a folder. > Thanks in advance. > > -- > sardinian_guy Hi, If you install the PowerShell Community Extensions from http://www.codeplex.com/powershellcx - there is a Cmdlet called Set- FileTime which will let you do this: ps> dir c:\files\ -rec -inc *.txt | set-filetime -created (get- randomdate) (get-randomdate is your function) - Oisin |
|
#3
|
|
|
|
|
'Oisin (x0n) Grehan [MVP Wrote:
> ;924303']On Dec 30, 10:29*am, sardinian_guy <gu> > wrote:> > > > > If you install the PowerShell Community Extensions from > 'PowerShell Community Extensions - Home' > ([..]) - there is a Cmdlet called Set- > FileTime which will let you do this: > > ps> dir c:\files\ -rec -inc *.txt | set-filetime -created (get- > randomdate) > > (get-randomdate is your function) > > - Oisin Thank you very much for your answer. I'll try these extensions. :D Is there however a way to solve it without installing anything? |
|
#4
|
|
|
|
|
The general behaviour is:
[vPodans] $a = Get-Item saferlog.txt [vPodans] $a.CreationTime pirmdiena, 2008. gada 29. decembri 13:07:27 [vPodans] function Get-RandomDate { >> [DateTime]$theMin = "1/1/2008" >> [DateTime]$theMax = [DateTime]::Now >> $theRandomGen = new-object random >> $theRandomTicks = [Convert]::ToInt64( ($theMax.ticks * 1.0 - >> $theMin.Ticks * 1.0 ) * $theRandomGen.NextDouble() + $th eMin.Ticks * 1.0 ) >> new-object DateTime($theRandomTicks) >> } >> [vPodans] $a.CreationTime = Get-RandomDate [vPodans] $a.CreationTime tresdiena, 2008. gada 2. julija 2:18:02 [vPodans] $a.CreationTime = Get-RandomDate [vPodans] $a.CreationTime piektdiena, 2008. gada 30. maija 21:59:08 [vPodans] you just create function (from http://blog.saadware.com/2008/06/12/...in-powershell/), and using it you just assign a new value for file CreationTime property. Enjoy! |
|
#5
|
|
|
|
|
sardinian_guy wrote:
> Thank you very much for your answer. I'll try these extensions. :D > Is there however a way to solve it without installing anything? >Hi, in the comments of this blog entry you will find a sample dealing with Dotnet [System.IO.File]::SetCreationTime(... which may help: <http://blogs.msdn.com/powershell/archive/2006/11/23/processing-eml-files-with-select-string-and-setcreationtime.aspx> But I may be wrong since I'm quite new to PoSh. HTH Matthias |
|
#6
|
|
|
|
|
Vadims Podans;924520 Wrote:
[..] > and using it you just assign a new value for file CreationTime > property. > > Enjoy! > -- > WBR, Vadims Podans > PowerShell blog - 'www.sysadmins.lv' ([..]) > > "sardinian_guy" <guest> rakstÄ«ja ziņojumÄ > "news:910"> > > Thank Vadims for your precious help. :) This code gci | % {$(get-item $_).creationtime = get-randomdate } works. However I made some proof and the dates don't seem so random. If I have few files, the same date occurs for several files. Is it the right behaviour or is it possible to have something more "randomly"? Thanks again |
|
#7
|
|
|
|
|
No it shouldn't work that way but the random generator seems to work
off the current time. 2 calls during the same milisecond result in the same value. sticking this in your function should clear up the problem with minimal impact sleep -Milliseconds 1 sardinian_guy wrote: [..] |
|
#8
|
|
|
|
|
RickB;925562 Wrote:
> No it shouldn't work that way but the random generator seems to work > off the current time. > 2 calls during the same milisecond result in the same value. > sticking this in your function should clear up the problem with > minimal impact > > sleep -Milliseconds 1 > > sardinian_guy wrote:> > > Thank you Rick. Adding sleep cmdlet solved the problem. Thanks again. Happy new year to everybody. Bye, Nicola. :D |
|
|
| Similar Threads | |
| How can I generate a list of random dates from dates I specify How can I generate a list of random dates from dates I specify |
|
| Copying files with Creation Dates preserved? Is there any way to copy files from an external drive onto a local drive, making sure that the files' Creation Dates are preserved during the copy, and not simply set to the... |
|
| Does synchronizing files alter file creation dates? If I am using outlook and word at home and then put those files on a flash drive, take it to work and sync it up with my network drive, should this have any effect on the... |
|
| Modify / Creation Dates on files being copied to a CDR I'm experiencing a problem whereby files that are copied to a CDR or CDRW are being tagged with creation AND modification registering the current date. I'm using XP... |
|
|
All times are GMT. The time now is 07:06 AM. | Privacy Policy
|