![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 that operator). |
My System Specs![]() |
| | #3 (permalink) |
| | 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 aforementioned comparison line. |
My System Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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} # 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 Quote: > Move-Item *.vis -Destination '$ArcDir\$monthago\' MkDir $strMonthAgo Move-Item *.vis -Destination $ArcDir\$strMonthAgo\ -- Kiron |
My System Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||