Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #check date and select files in N: from bfeore today's date
- $todayDate = Get-Date -UFormat "%m / %d / %Y"
- $FKPLC1 = Get-ChildItem -Path "F:\KPLC1"
- $FKPLC2 = Get-ChildItem -Path "F:\KPLC2"
- $NKPLC1 = Get-ChildItem -Path "N:\KPLC1"
- $NKPLC2 = Get-ChildItem -Path "N:\KPLC2"
- $copied1 = @()
- $copied2 = @()
- #For each item in F:\KPLC1, if the creation date is before today's date, copy it to N:\KPLC1 and add it to an array named "copied"
- foreach ($item in $FKPLC1) {
- if ($_.CreationDate -lt $todayDate) {
- copy-item -path "F:\KPLC1\$_" -Destination "N:\KPLC1"
- $copied1 += ,"$_"
- } else {
- "There is nothing to copy in KPLC1"
- }
- }
- #For each item in F:\KPLC2, if the creation date is before today's date, copy it to N:\KPLC2 and add it to an array named "copied"
- foreach ($item in $FKPLC2) {
- if ($_.CreationDate -lt $todayDate) {
- copy-item -path "F:\KPLC2\$_" -Destination "N:\KPLC2"
- $copied2 += ,"$_"
- } else {
- "There is nothing to copy in KPLC1"
- }
- }
- #Check that the items copied are in the correct directories, and if they are, delete them. (Ignoring any extra items that might be in N:\ but not in F:\)
- #Display items in F that are not in N and assign those into an array
- $missing1 = $NKPLC1 | Where {$FKPLC1 -NotContains $_}
- #Display items in F that are not in N and assign those into an array
- $missing2 = $NKPLC2 | where {$FKPLC2 -NotContains $_}
- #if there are one or more items in the array, copy those items to N: and then delete the originals, otherwise delete all items in F:\KPLC1 that were in copied1
- if ($missing1.Count -gt 0) {
- $missing1 | copy-item -Path "F:\KPLC1\$_" -Destination "N:\KPLC1"
- } else {
- $copied1 | remove-item -Path "F:\KPLC1\$_"
- }
- #if there are one or more items in the array, copy those items to N: and then delete the originals, otherwise delete all items in F:\KPLC2 that were in copied2
- if ($missing2.Count -gt 0) {
- $missing2 | copy-item -Path "F:\KPLC2\$_" -Destination "N:\KPLC2"
- } else {
- $copied2 | remove-item -Path "F:\KPLC2\$_"
- }
- read-host "Press ENTER when finished."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement