locked
Looking for a script that will look at directory count the number of files for last 2 days and tell me if they differ by more than 25% RRS feed

  • Question

  • The subject pretty much says it all.  We have a bunch of field devices that are supposed "phone home" their log files daily.  I just want a script that looks at the log directory counts the total # of files by the datetime stamps on the file for the previous 2 days and tells me if the 2 days differed by more than 25%.  This would indicate a problem somewhere that needs to be investigated.  We may add or subtract devices or some devices may not be able to phone home due to various reasons like a dead battery, etc.  is why the numbers aren't the same every day, but they should be in the ball park of each other.  25% change in the number of files needs to be looked into by our support folks.

    Thursday, October 18, 2018 7:44 PM

All replies

  • Hint:

    @{n="Files";e={(dir |?{$_.LastWriteTime -gt [datetime]::Today.AddDays(-2) -and $_.LastWriteTime -lt [datetime]::Today.AddDays(-1)}).count}}
    

    Friday, October 19, 2018 6:40 AM
  • I think you really want:

    $day1 = (dir $path |?{$_.LastWriteTime -gt [datetime]::Today.AddDays(-3) -and $_.LastWriteTime -lt [datetime]::Today.AddDays(-2)}).Count

    The math is:

    1 - ($day1/$day2) -gt .25


    \_(ツ)_/



    Friday, October 19, 2018 6:46 AM
  • Hi Jason,

    Thanks for your question.

    I agree with Jrv. You can't post script request here. Try to look for scripts in Script Gallery or Script Center.

    I can give you some suggestions.

    1. "Get-ChildItem" cmdlet can help you.

    $1=(Get-ChildItem -Path C:\test1\ | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}).count
    $2=(Get-ChildItem -Path C:\test1\ | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-2)}).count

    2. Try to compare the value of these two variables.

    $1 is the number of last day's files. $2 is the number of last two day's files.

    Best Regards,

    Lee


    Just do it.

    Friday, October 19, 2018 7:11 AM
  • Hi Jason,

    Thanks for your question.

    I agree with Jrv. You can't post script request here. Try to look for scripts in Script Gallery or Script Center.

    I can give you some suggestions.

    1. "Get-ChildItem" cmdlet can help you.

    $1=(Get-ChildItem -Path C:\test1\ | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}).count
    $2=(Get-ChildItem -Path C:\test1\ | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-2)}).count

    2. Try to compare the value of these two variables.

    $1 is the number of last day's files. $2 is the number of last two day's files.

    Best Regards,

    Lee


    Just do it.

    This is completely wrong.  It is both logically wrong and arithmetically wrong.  Please try to understand how to get a single days files.  You are getting one collection of a days files and a collection of two days files.  To find out how much a number has grown you need to compare two single different days.

    The better way to state the question is: "By how much has day 2 grown when compared to day 1?"

    This is why the above hints have included a beginning time and an end time.


    \_(ツ)_/

    Friday, October 19, 2018 7:23 AM
  • Hi Jrv,

    When I post my comments, I have not seen your reply. What I want is the comparison of $1 and "$day2=$2-$1". Your answer is correct, I need to add beginning time and end time.

    Best Regards,

    Lee


    Just do it.

    Friday, October 19, 2018 7:35 AM
  • Hello,

    JRV, did u deleted my posts? Did u see anything abusive in it? .. Did u found my point, that your math formula is not entirely correct, irelevant?


    • Edited by Mekac Friday, October 19, 2018 9:44 AM
    Friday, October 19, 2018 9:44 AM
  • Sorry for the confusion.

    Post split and moved as they were very much OFF TOPIC and the split didn't make much sense as a split.

    No - it was nothing anyone posted it is just not fair to the OP to ramble on as we have done.  Open a new topic to pursue this further.

    The current thread is pretty much complete as an answer to the OP. 

    If you want to continue the discussion then we can open a new discussion.

    The split is here: https://social.technet.microsoft.com/Forums/en-US/bc641ef8-9407-415a-91ac-bc49921697e4/extracting-percent-difference?forum=Offtopic


    \_(ツ)_/


    • Edited by jrv Friday, October 19, 2018 9:49 AM
    Friday, October 19, 2018 9:48 AM
  • Thank you.  I apologize for posting the script request here, but very much appreciate the replies.  I also thought of a new wrinkle.  I need to comare "Monday"s count to "Friday"s count and ignore the weekend file counts.  

    Friday, October 19, 2018 12:54 PM
  • Thank you.  I apologize for posting the script request here, but very much appreciate the replies.  I also thought of a new wrinkle.  I need to comare "Monday"s count to "Friday"s count and ignore the weekend file counts.  

    Again yu are asking for others to write a script for you.

    Start by learning how to work with dates. Post back with a specific question when you get stuck.

    help Get-Date -online

    Search for articles on PowerShell and dates.


    \_(ツ)_/

    Friday, October 19, 2018 4:51 PM