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 - Working om directories

Reply
 
Old 11-14-2008   #1 (permalink)
tERRY


 
 

Working om directories

I'm writing a script to archive data files from a directory more than 30 days
old and delete the directory. 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\PerfMon Web\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.
#Read a new dir for each group of monitored systems
$Systems = Get-Content "C:\WindowsPowerShell\PS_Scripts\MonitoredSystems.txt"

# Step 4: Recursively CD to each dir. Then test for directories more than 30
days old.
Foreach ($System in $Systems)
{$DataDirs = Get-ChildItem | Select-Object FullName
ForEach($DataDir in $DataDirs)
{CD $DataDir
If ($DataDir.CreationTime.ToString('MMddyyyy') -gt $Monthago)
# Step 5: Test for the "combine_out" dir.
{$VisDirs = Get-ChildItem | Select-Object Name
# If the vis dir exists exists, CD to it.
If($VisDir.Name -eq "combine-out")
{$VisDir = "C:\WindowsPowerShell\PerfMon Web\Daily Data Analysis\" +
$System + $DataDir + "combine-out"
CD "combine-out"

# Step 6: Get the vis file name.
$FileNames = Get-ChildItem | Select-Object Name
$VisFile = (Get-ChildItem *.vis).Name

# Step 7: If $VisFile not null move the file to an archive dir with the
same name.
If ($VisFile -ne "")
# Create the archive dir.
{$ArcDir = 'C:\WindowsPowerShell\PerfMon Web\Archive\' + $System
SL $ArcDir
MkDir $Monthago
$ArcDir = $ArcDir + "\" + $Monthago

# Move the *.vis file.
Move-Item $VisFile -Destination $ArcDir
}
}
}
# Step 8: remove the original data dir.
rmdir $DataDir
}
}

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

In step four the statement reading the subdirectories isn't working as
expected. It works at the command line but not in the script. The value of
$DataDirs is null and that generates an error in the comparison two lines
down. Suggestions?

My System SpecsSystem Spec
Old 11-14-2008   #2 (permalink)
Rob Campbell


 
 

RE: Working om directories

I'm not sure if it's what's producing the results you're getting, but doing
doing that kind of date calculation on string data in mmddyyyy format is not
going to deal with data that crosses a year boundary very well.

"tERRY" wrote:
Quote:

> I'm writing a script to archive data files from a directory more than 30 days
> old and delete the directory. 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\PerfMon Web\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.
> #Read a new dir for each group of monitored systems
> $Systems = Get-Content "C:\WindowsPowerShell\PS_Scripts\MonitoredSystems.txt"
>
> # Step 4: Recursively CD to each dir. Then test for directories more than 30
> days old.
> Foreach ($System in $Systems)
> {$DataDirs = Get-ChildItem | Select-Object FullName
> ForEach($DataDir in $DataDirs)
> {CD $DataDir
> If ($DataDir.CreationTime.ToString('MMddyyyy') -gt $Monthago)
> # Step 5: Test for the "combine_out" dir.
> {$VisDirs = Get-ChildItem | Select-Object Name
> # If the vis dir exists exists, CD to it.
> If($VisDir.Name -eq "combine-out")
> {$VisDir = "C:\WindowsPowerShell\PerfMon Web\Daily Data Analysis\" +
> $System + $DataDir + "combine-out"
> CD "combine-out"
>
> # Step 6: Get the vis file name.
> $FileNames = Get-ChildItem | Select-Object Name
> $VisFile = (Get-ChildItem *.vis).Name
>
> # Step 7: If $VisFile not null move the file to an archive dir with the
> same name.
> If ($VisFile -ne "")
> # Create the archive dir.
> {$ArcDir = 'C:\WindowsPowerShell\PerfMon Web\Archive\' + $System
> SL $ArcDir
> MkDir $Monthago
> $ArcDir = $ArcDir + "\" + $Monthago
>
> # Move the *.vis file.
> Move-Item $VisFile -Destination $ArcDir
> }
> }
> }
> # Step 8: remove the original data dir.
> rmdir $DataDir
> }
> }
>
> # Return to the default dir
> SL 'C:\WindowsPowerShell\PerfMon Web\Daily Data Analysis'
>
> In step four the statement reading the subdirectories isn't working as
> expected. It works at the command line but not in the script. The value of
> $DataDirs is null and that generates an error in the comparison two lines
> down. Suggestions?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
FTP whole directories? Vista General
merge directories Vista General
Problems with Directories Vista General
Expand Sub Directories Vista General
Another question about directories 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