Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to write a Powershell Script that checks a certain folder for .zip files. The .zip files contain information from certain backups. These .zip files lie within a folder tree for different plants that our company owns. Every day there is supposed to be a new .zip file in the bottom of this folder tree that is retained for a total of 7 days.

Ideally, each folder should contain 7 consecutive days of backups equaling 7 .zip files in each folder. Sometimes there are more than 7 and sometimes there are 0 depending on the issue the plant is experiencing.

I want (and am having trouble) writing a script that checks these folders for these .zip files and lets me know if there aren't any files in there from the last seven days. (It is okay if there aren't exactly 7).

So far I have the following code:

Get-ChildItem -filter "*.zip" -recurse -path C:\ArcherBackup\ | where {($_.CreationTime -gt ((get-date).AddDays(-7)))}

But this code brings up a list of every file that I have to manually look through.

I would like the script to more or less highlight a directory that does not contain a .zip file that is at least 7 days old.

Any assistance or guidance is appreciated.

share|improve this question

2 Answers 2

Your snippet will return all the files created within the last seven days.

$_.CreationTime -gt ((get-date).AddDays(-7))

I think you want to return files that are older than seven days, so change -gt to -lt:

$_.CreationTime -lt ((get-date).AddDays(-7))

When using comparisons, it's a good idea to always use less than operators, that way, the conditional increases from left to right, like an x-axis, and it helps in readability/understanding.

0 -lt x -and x -lt 10

Makes it obvious that x should come between 0 and 10.

share|improve this answer
    
Thanks for the input. I don't really care if there are files in there older than 7 days. I'm more concerned that there are ANY .zip files in there created in the last 7 days. I'm not sure how to implement your last line of code. –  cyborgcommando0 Jul 5 '13 at 20:49
    
@cyborgcommando0 Hmm. Your code should work. Can you show a directory listing and script output for what isn't working? –  splattered bits Jul 5 '13 at 20:58
    
I may be doing this wrong. I am not great with Powershell yet. The code I have so far is: Get-ChildItem -filter "*.zip" -recurse -path C:\ArcherBackup\ | where {($_.CreationTime 0 -lt x -and x -lt 10 ((get-date).AddDays(-7)))} Which gives me several errors. –  cyborgcommando0 Jul 5 '13 at 21:00
    
@cyborgcommando0 The 0 -lt x -and x -lt 10 is just an example, and doesn't belong in your code. Again, if I could see an example directory listing that contains files that match your criteria, that would be helpful. Please edit your question with that information. –  splattered bits Jul 5 '13 at 22:11

var 1, command line, sub directory that does not contain a .zip file that is at least 7 days old:

powershell $tm=(get-date).AddDays(-7);ls "C:\ArcherBackup" -fo -r^|?{$_.PSIsContainer}^|%{ls "$($_.FullName)" -fo -fi *.zip}^|?{$_.CreationTime -lt $tm}^|group Directory^|FT -H Name

var 2, command line, sub directory that does not contain a .zip file that is at least 7 days old;

except hiden files and directory:

powershell $tm=(get-date).AddDays(-7);ls "C:\ArcherBackup" -r^|?{$_.PSIsContainer}^|%{ls "$($_.FullName)" -fi *.zip}^|?{$_.CreationTime -lt $tm}^|group Directory^|FT -H Name

empty sub folders without files:

powershell $d=ls -r^|?{$_.PSIsContainer};$d^|?{$_.GetFiles().Count -eq 0}^|FT -H FullName

sub folders without zip-files:

powershell $d=ls -r^|?{$_.PSIsContainer};$d^|?{$_.GetFiles('*.zip').Count -eq 0}^|FT -H FullName

sub folders without zip-files;

sub directory that does not contain a .zip file that is at least 7 days old:

powershell $d=ls "C:\ArcherBackup" -fo -r^|?{$_.PSIsContainer};$tm=(get-date).AddDays(-7);$d^|%{ls "$($_.FullName)" -fo -fi *.zip}^|?{$_.CreationTime -lt $tm}^|group Directory^|FT -Au -H Name, Count;$d^|?{$_.GetFiles('*.zip').Count -eq 0}^|FT -H FullName|findstr [/s]| more

|findstr [/s]| - delete empty string in pipe

FT- alias Format Table

FT -H FullName - output format, select only FullName

-Au alias -Autosize

-H -hide header,

? - alias where - alias Where-Object

% - alias foreach - alias ForEach-Object

-fo - alias -force, fi - alias -filter,-r - alias -recurse

^ - escape character | - current shell is not processed as a pipeline-command

group - alias Group-Object

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.