![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||