Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Checking dir creation time

Reply
 
Old 09-16-2008   #1 (permalink)
tERRY


 
 

Checking dir creation time

I'm writing a script to archive dat files more than a month old but the date
comparison isn't working. Here's the script:
# Archive_Vis_Data.ps1
# Archive Visualizer data in the "Daily Data Analysis" directory

# Step 1: set the starting location
SL "C:\WindowsPowerShell\Test\Daily Data Analysis"

# Step 2: calculate the date 30 days ago
$monthago = ( get-date ).AddDays(-30).ToString('MMddyyyy')

# Step 3: Read the "MonitoredSystems.txt" file to get a list of data
directories.
# Recursively CD to each dir. Then test for directories more than 30 days
old.
# Then test for the "combine_out" dir. If it exists, CD to it and move the
vis
# file to an archive dir with the same date.

#Read a new dir for each group of monitored systems
$systems = Get-Content C:\WindowsPowerShell\PS_Scripts\MonitoredSystems.txt
Foreach ($System in $Systems)
{$SrvDir = "C:\WindowsPowerShell\Test\Daily Data Analysis\" + $System
CD $SrvDir
$DataDir = Get-ChildItem | Where {$_.CreationTime.ToString('MMddyyyy') -gt
$monthago}
{If(Get-ChildItem | Where ($_.Name -eq "combine_out"))
{

# Create the archive dir.
$ArcDir = 'C:\WindowsPowerShell\Test\Archive\' + $system
CD $ArcDir
MkDir $monthago

# Switch to the data dir and move the vis file.
SL $DataDir
CD combine_out
Move-Item *.vis -Destination '$ArcDir\$monthago\'
}
}
}

# Return to the default dir
SL 'C:\WindowsPowerShell\Test\Daily Data Analysis'

It's the line $DataDir = where the date comparison is.

Suggestions?
--
Terry Edwards

My System SpecsSystem Spec
Old 09-16-2008   #2 (permalink)
RickB


 
 

Re: Checking dir creation time

On Sep 16, 9:31*am, tERRY <tE...@xxxxxx> wrote:
Quote:

> I'm writing a script to archive dat files more than a month old but the date
> comparison isn't working. Here's the script:
> # Archive_Vis_Data.ps1
> # Archive Visualizer data in the "Daily Data Analysis" directory
>
> # Step 1: set the starting location
> SL "C:\WindowsPowerShell\Test\Daily Data Analysis"
>
> # Step 2: calculate the date 30 days ago
> $monthago = ( get-date ).AddDays(-30).ToString('MMddyyyy')
>
> # Step 3: * * * Read the "MonitoredSystems.txt" file to get a list of data
> directories.
> # * * * * * * * * * * * Recursively CD to each dir.Then test for directories more than 30 days
> old.
> # * * * * * * * * * * * Then test for the "combine_out" dir. If it exists, CD to it and move the
> vis
> # * * * * * * * * * * * file to an archive dir withthe same date.
>
> #Read a new dir for each group of monitored systems
> $systems = Get-Content C:\WindowsPowerShell\PS_Scripts\MonitoredSystems..txt
> Foreach ($System in $Systems)
> * * * * {$SrvDir = "C:\WindowsPowerShell\Test\Daily Data Analysis\" + $System
> * * * * CD $SrvDir
> * * * * $DataDir = Get-ChildItem | Where {$_.CreationTime.ToString('MMddyyyy') -gt
> $monthago}
> * * * * * * * * {If(Get-ChildItem | Where ($_.Name -eq "combine_out"))
> * * * * * * * * * * * * {
>
> # * * * * * * * * * * * Create the archive dir.
> * * * * * * * * * * * * $ArcDir = 'C:\WindowsPowerShell\Test\Archive\' + $system
> * * * * * * * * * * * * CD $ArcDir
> * * * * * * * * * * * * MkDir $monthago
>
> # * * * * * * * * * * * Switch to the data dir and move the vis file.
> * * * * * * * * * * * * SL $DataDir
> * * * * * * * * * * * * CD combine_out
> * * * * * * * * * * * * Move-Item *.vis -Destination '$ArcDir\$monthago\'
> * * * * * * * * * * * * }
> * * * * * * * * }
> * * * * }
>
> # Return to the default dir
> SL 'C:\WindowsPowerShell\Test\Daily Data Analysis'
>
> It's the line $DataDir = where the date comparison is.
>
> Suggestions?
> --
> Terry Edwards
I think you just need to change -gt to -lt (or switch the arguments to
that operator).
My System SpecsSystem Spec
Old 09-16-2008   #3 (permalink)
RickB


 
 

Re: Checking dir creation time

On Sep 16, 9:31*am, tERRY <tE...@xxxxxx> wrote:
Quote:

> I'm writing a script to archive dat files more than a month old but the date
> comparison isn't working. Here's the script:
> # Archive_Vis_Data.ps1
> # Archive Visualizer data in the "Daily Data Analysis" directory
>
> # Step 1: set the starting location
> SL "C:\WindowsPowerShell\Test\Daily Data Analysis"
>
> # Step 2: calculate the date 30 days ago
> $monthago = ( get-date ).AddDays(-30).ToString('MMddyyyy')
>
> # Step 3: * * * Read the "MonitoredSystems.txt" file to get a list of data
> directories.
> # * * * * * * * * * * * Recursively CD to each dir.Then test for directories more than 30 days
> old.
> # * * * * * * * * * * * Then test for the "combine_out" dir. If it exists, CD to it and move the
> vis
> # * * * * * * * * * * * file to an archive dir withthe same date.
>
> #Read a new dir for each group of monitored systems
> $systems = Get-Content C:\WindowsPowerShell\PS_Scripts\MonitoredSystems..txt
> Foreach ($System in $Systems)
> * * * * {$SrvDir = "C:\WindowsPowerShell\Test\Daily Data Analysis\" + $System
> * * * * CD $SrvDir
> * * * * $DataDir = Get-ChildItem | Where {$_.CreationTime.ToString('MMddyyyy') -gt
> $monthago}
> * * * * * * * * {If(Get-ChildItem | Where ($_.Name -eq "combine_out"))
> * * * * * * * * * * * * {
>
> # * * * * * * * * * * * Create the archive dir.
> * * * * * * * * * * * * $ArcDir = 'C:\WindowsPowerShell\Test\Archive\' + $system
> * * * * * * * * * * * * CD $ArcDir
> * * * * * * * * * * * * MkDir $monthago
>
> # * * * * * * * * * * * Switch to the data dir and move the vis file.
> * * * * * * * * * * * * SL $DataDir
> * * * * * * * * * * * * CD combine_out
> * * * * * * * * * * * * Move-Item *.vis -Destination '$ArcDir\$monthago\'
> * * * * * * * * * * * * }
> * * * * * * * * }
> * * * * }
>
> # Return to the default dir
> SL 'C:\WindowsPowerShell\Test\Daily Data Analysis'
>
> It's the line $DataDir = where the date comparison is.
>
> Suggestions?
> --
> Terry Edwards
I'm pretty sure you just need -lt rather than -gt in the
aforementioned comparison line.
My System SpecsSystem Spec
Old 09-16-2008   #4 (permalink)
cerna.zelva


 
 

Re: Checking dir creation time

I think the comparison is not working because you compare strings, not
dates. Try:
$monthago = ( get-date ).AddDays(-30)
....
$DataDir = Get-ChildItem | Where {$_.CreationTime -gt $monthago}

(i cut the ToString method)

On 16 Záø, 16:31, tERRY <tE...@xxxxxx> wrote:
Quote:

> I'm writing a script to archive dat files more than a month old but the date
> comparison isn't working. Here's the script:
> # Archive_Vis_Data.ps1
> # Archive Visualizer data in the "Daily Data Analysis" directory
>
> # Step 1: set the starting location
> SL "C:\WindowsPowerShell\Test\Daily Data Analysis"
>
> # Step 2: calculate the date 30 days ago
> $monthago = ( get-date ).AddDays(-30).ToString('MMddyyyy')
>
> # Step 3: * * * Read the "MonitoredSystems.txt" file to get a list of data
> directories.
> # * * * * * * * * * * * Recursively CD to each dir.Then test for directories more than 30 days
> old.
> # * * * * * * * * * * * Then test for the "combine_out" dir. If it exists, CD to it and move the
> vis
> # * * * * * * * * * * * file to an archive dir withthe same date.
>
> #Read a new dir for each group of monitored systems
> $systems = Get-Content C:\WindowsPowerShell\PS_Scripts\MonitoredSystems..txt
> Foreach ($System in $Systems)
> * * * * {$SrvDir = "C:\WindowsPowerShell\Test\Daily Data Analysis\" + $System
> * * * * CD $SrvDir
> * * * * $DataDir = Get-ChildItem | Where {$_.CreationTime.ToString('MMddyyyy') -gt
> $monthago}
> * * * * * * * * {If(Get-ChildItem | Where ($_.Name -eq "combine_out"))
> * * * * * * * * * * * * {
>
> # * * * * * * * * * * * Create the archive dir.
> * * * * * * * * * * * * $ArcDir = 'C:\WindowsPowerShell\Test\Archive\' + $system
> * * * * * * * * * * * * CD $ArcDir
> * * * * * * * * * * * * MkDir $monthago
>
> # * * * * * * * * * * * Switch to the data dir and move the vis file.
> * * * * * * * * * * * * SL $DataDir
> * * * * * * * * * * * * CD combine_out
> * * * * * * * * * * * * Move-Item *.vis -Destination '$ArcDir\$monthago\'
> * * * * * * * * * * * * }
> * * * * * * * * }
> * * * * }
>
> # Return to the default dir
> SL 'C:\WindowsPowerShell\Test\Daily Data Analysis'
>
> It's the line $DataDir = where the date comparison is.
>
> Suggestions?
> --
> Terry Edwards
My System SpecsSystem Spec
Old 09-16-2008   #5 (permalink)
Kiron


 
 

Re: Checking dir creation time

When comparing datetime objects remember this:
<Past> -lt <Present> -and <Future> -gt <Present>

# but this command does not compare [datetime] objects,
Quote:

> $DataDir = Get-ChildItem | Where {$_.CreationTime.ToString('MMddyyyy') -gt $monthago}
# it compares [string] objects, you could get erroneus results
# comparing dates as strings
07032008 -lt 08172008 # OK
07032008 -lt 08172007 # oops!
# comparing datetime objects
[datetime]'07/03/2008' -lt [datetime]'08/17/2008'
[datetime]'07/03/2007' -lt [datetime]'08/17/2007
# it's better practice to compare dates as [datetime] objects:
$DataDir = Get-ChildItem | Where {$_.CreationTime -lt $monthago}

# also define $monthago like this:
$monthago = ( get-date ).AddDays(-30)

# and define the formatted version
$strMonthAgo = $monthago.toString('MMddyyyy')

# and modify these lines:
Quote:

> MkDir $monthago
# single quotes do not expand the variables, no quotes needed
Quote:

> Move-Item *.vis -Destination '$ArcDir\$monthago\'
# to:
MkDir $strMonthAgo
Move-Item *.vis -Destination $ArcDir\$strMonthAgo\

--
Kiron
My System SpecsSystem Spec
Old 09-16-2008   #6 (permalink)
Kiron


 
 

Re: Checking dir creation time

$ typo:
[datetime]'07/03/2007' -lt [datetime]'08/17/2007
# should be:
[datetime]'07/03/2008' -lt [datetime]'08/17/2007'

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Help with Recovery DVD Creation General Discussion
PDF Creation? Vista General
Sort via creation time PowerShell
XML Document creation PowerShell
Trouble grouping System.IO.FileInfo objects by creation day, not time! PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46