Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Give a folder a maximum size

  • 11-07-2020 7:17am
    #1
    Registered Users, Registered Users 2 Posts: 832 ✭✭✭


    Hi,
    I want to give a folder a maximum size so that when it is full it replaces the old data. If it had enough capacity to hold 100 files it would always hold the most recent 100 files if that makes sense.
    Is this possible and if so how would I set it up?
    Thanks for the help


Comments

  • Registered Users, Registered Users 2 Posts: 4,257 ✭✭✭smuggler.ie


    Hi,
    I want to give a folder a maximum size so that when it is full it replaces the old data. If it had enough capacity to hold 100 files it would always hold the most recent 100 files if that makes sense.
    Is this possible and if so how would I set it up?
    Thanks for the help
    For such, i think script only...
    See if this suites
    https://devblogs.microsoft.com/scripting/how-can-i-delete-the-oldest-file-from-a-folder-if-and-when-that-folder-exceeds-a-specified-size/

    edit: no idea what the brag is about coce and pepsi :D


  • Registered Users, Registered Users 2 Posts: 832 ✭✭✭studdlymurphy


    For such, i think script only...
    See if this suites
    https://devblogs.microsoft.com/scripting/how-can-i-delete-the-oldest-file-from-a-folder-if-and-when-that-folder-exceeds-a-specified-size/

    edit: no idea what the brag is about coce and pepsi :D

    Thanks, so im now in a little over my head.

    Couple of questions:
    where does this script run from? what program?
    How do I schedule it as a task to run every hour or day?

    if my folder name and location is c:\documents\testfolder I have bolded where I think this should go in teh script
    and the files are named testfilexx.log
    and the folder max size is 1Gb I have bolded where I think this should go
    then what sections of the code do I need to change?


    Code Below.........................

    strOldestFile = “”

    dtmOldestDate = Now

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)

    Set objFolder = objFSO.GetFolder(“C:\Scripts”) ; this should be the folder location?
    intFolderSize = Int((objFolder.Size / 1024) / 1024) ; this should be the 1Gb size?
    If intFolderSize >= 25 Then

    Set colFiles = objFolder.Files
    For Each objFile in colFiles

    strFile = objFile.Path

    dtmFileDate = objFile.DateCreated

    If dtmFileDate < dtmOldestDate Then

    dtmOldestDate = dtmFileDate

    strOldestFile = strFile

    End If

    Next
    objFSO.DeleteFile(strOldestFile)

    End If


  • Registered Users, Registered Users 2 Posts: 4,257 ✭✭✭smuggler.ie


    Not a "Script Guy" by any means and have not tested this particular(might do).
    I know...., author bragging about irrelevant crap just make it harder to understand...he must be on coke at the time :D


    where does this script run from?
    ##you can save file in...lets say... c:\my_scripts or at you discretion.

    what program?
    ##copy/paste script into PowerShell ISE (its build in OS, find in start menu search), modify as needed, "Save As" folder_cleanup.ps1 file in c:\my_scripts
    ## copy/paste script into notepad, modify as needed, "Save As" folder_cleanup.vbs file in c:\my_scripts

    How do I schedule it as a task to run every hour or day?
    ##set task in task scheduler to run script file(folder_cleanup.vbs) at desired intervals/triggers

    Set objFolder = objFSO.GetFolder(“c:\documents\testfolder”)

    If intFolderSize >= 1024 Then
    ## as per article, 25 was MB's, 1Gb=1024MB, line above was just conversion from bytes


    I would highly recommend to test script on some junk folder for several days before applying to production...


    Edit: in this script file name is irrelevant - script will delete any "oldest" file by creation date if folder size >= set size.


  • Registered Users, Registered Users 2 Posts: 4,257 ✭✭✭smuggler.ie


    Update:

    nah, above script wont work from powershell, its VBScript and some commands not accepted/compatible.
    sorry for confusion


    "Save as" it in notepad with .vbs extension in ANSI encoding(default)

    Furthermore, there are some errors in it ready - ALL inverted comma's need to be replaced to normal ""

    Let me test...


  • Registered Users, Registered Users 2 Posts: 4,257 ✭✭✭smuggler.ie


    works OK
    keep in mind - it "purge" files, you wont be able to restore them as they not placed in recycling bin

    note: i edited post #4


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 832 ✭✭✭studdlymurphy


    works OK keep in mind - it "purge" files, you wont be able to restore them as they not placed in recycling bin


    Thank you, ill see if I can get it going and report back


  • Registered Users, Registered Users 2 Posts: 36,170 ✭✭✭✭ED E


    Be careful here, a small error with something like this could cause a lot of data loss. eg You try to trim C:\Users\Studdly\Downloads but accidentally do one level above and wipe your entire home folder.

    I'd be looking at how you generate these files if possible as Windows doesnt offer this level of quotas natively.


  • Registered Users, Registered Users 2 Posts: 4,257 ✭✭✭smuggler.ie


    Caution is second fortune...
    I ran script several times and, as expected, it only purge one file at the time of execution - the oldest, down to seconds of creation, providing set folder size exceeded.

    Again, this script only encounter date/time, it doesn't care of name, file type , etc.

    From that, task frequency need to be set accordingly how many files are generated in that folder per day/hour/etc to maintain designated folder size.


Advertisement