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 all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
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

Batch file for Windows

  • 20-02-2019 5:27pm
    #1
    Registered Users Posts: 10


    I'm writing a batch script to automatically remove specific folders older then 7 days to keep my disk from getting full. so that it will run when free disk space is below 20GB? Can anyone do one for me as I'm clueless to it


Comments

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




  • Moderators, Arts Moderators, Regional Abroad Moderators Posts: 11,076 Mod ✭✭✭✭Fysh


    Unless you're on a truly ancient version of windows, do it in powershell - lots more people using it, plus the built-in help functions mean you can actually more or less teach yourself.

    Assuming that your folders are all in a subdirectory called C:\DeleteMe you could use this to identify them:
    get-childitem -path "C:\DeleteMe" | Where-Object {$_.Mode -match "^d"}
    

    Get-childItem doesn't differentiate between files and folders, so the results of that are piped through to Where-Object, which checks the Mode attribute. Only results with a mode starting with d (folders) are included.

    If you want to limit your script to folders created more than 7 days ago, you'd expand it to:
    $today=Get-Date;
    get-childitem -path "C:\DeleteMe" | Where-Object {($_.Mode -match "^d") -and ($_.CreationDate -lt $today.AddDays(-7))}
    

    If you instead want to limit your script to folders last changed more than 7 days ago, you'd replace the CreationDate attribute with LastModified:
    $today=Get-Date;
    get-childitem -path "C:\DeleteMe" | Where-Object {($_.Mode -match "^d") -and ($_.LastModified -lt $today.AddDays(-7))}
    

    Once you know the folders you want to get rid of, you can use a foreach loop to iterate through each of them and delete them. You can also use a try-catch statement to catch errors.
    $logfile="c:\Users\MyUsername\Desktop\Foldercleanup.txt";
    $today=Get-Date;
    "$($today): Running folder cleanup on C:\DeleteMe" | out-file -Filepath $logfile -append
    $folderlist= get-childitem -path "C:\DeleteMe" | Where-Object {($_.Mode -match "^d") -and ($_.LastModified -lt $today.AddDays(-7))};
    foreach ($folder in $folderlist) {
    	try {
    		Remove-Item -Path $folder.FullName -Recurse -Force -ErrorAction Stop;
    		"Deleted folder $($folder.FullName)!" | Out-file -Filepath $logfile -append
    	} catch {
    		"Could not delete folder $($folder.FullName), error was $($_.Exception.Message)" | Out-File -Filepath $logfile -append
    	}
    }
    

    This builds on the list of folders from earlier, iterating through each folder and attempting to delete it. Using the "-Recurse" switch means the Remove-Item command tries to delete all child items in the folder first (required for it to succeed, as you can't delete a parent folder that still has child files or folders), and using Force to allow deletion of hidden and read-only files. The try-catch statement allows you to record any problems deleting files or folders along the way.

    That should be enough to get you started. A few commands you'll find helpful if you're learning PowerShell:
    • Get-Help - Use in the form "Get-Help Remove-Item" and you'll see information about how to use that command. If you're very new, use "Get-Help Remove-Item -full" or "Get-Help Remove-Item -examples" and you'll see a lot of useful information about how you can use the command.
    • Get-Command - Use in the form "Get-Command | Where-Object {$_.Name -match "Item"}" to see all commands containing the word "Item" in their name. Then you can use get-help to see how to use that command.
    • Get-Member - Use in the form "$folderlist | Get-Member" to see a list of all the attributes and methods available in a given object type. This will let you work out things like how to tell if a file is older than 7 days, and so on.

    As the previous post says, a Google search will show you plenty of results - but tbh if you're looking to learn, figure out how to explain what you want to do and search somewhere like StackOverflow - there are lots of very helpful people out there who'll put the time in to help you solve problems.


Advertisement