Writing a Cleanup Script to Clear a Model Folder
IMPORTANT NOTE!!! - before you review this post and/or use the script contained within, it is very important to understand that you use the script at your own risk. This post introduces a way to clean your model folders and delete all model results. This knowledge of course carries the risk that the script can be used irresponsibly, and potentially delete files that should not be deleted. Aquanty does not bear any responsibility for potential misapplications of this script, so BE CAREFUL!
This post introduces a simple method to clean up model folders without manually deleting individual files. While HydroGeoSphere typically overwrites results, there are cases where returning a model folder to its initial state is necessary—whether due to incorrect parameters, changes in output times, or preparing files for sharing.
This week we aren’t highlighting any particular grok command. Instead, we wanted to introduce a simple method for you to clean up your model folders without having to manually delete any individual files.
Most of the time HydroGeoSphere is able to simply overwrite your results, but we’ve all been in a situation where you needed to get your model folder back to its initial state. Maybe you ran a model with the wrong set of parameters, and you want to delete the results before running the model again. Or perhaps you changed the model output times, in which case HydroGeoSphere won’t overwrite many of those older/obsolete output times. Or maybe you just want to share a model with someone and it is easier to wipe out the results before sending the input files. Whatever the case may be, if you want to return your model folder to its initial state before you ran grok.exe, you can consider using the Windows PowerShell script below.
Again, please note that the script introduced below should be used very carefully, since it does carry the risk of deleting files that you would rather keep!
If you are new to using Windows PowerShell, we recommend reviewing this tutorial before proceeding:
https://www.windowscentral.com/how-create-and-run-your-first-powershell-script-file-windows-10#run_powershell_script_windows10
The script below will delete the following objects in a model folder upon execution:
All scratch files (i.e. input files generated by grok.exe)
All .dbg files (i.e. general purpose output files for debugging created by grok.exe and phgs.exe)
All local_runtime_XXXX.dat files (i.e. output files for each domain/output time)
All progress.dat files (i.e. Tecplot formatted file of CPU time vs system time)
Here is the PowerShell script itself (instructions to use the script appear below):
# Use this input parameter to hold the model prefix. param( [Parameter(Mandatory)] [string]$prefix ) $ErrorActionPreference = 'Stop' # Remove all prefixo. files excluding certain files. To exclude files named # prefixo.f1, prefixo.f2, prefixo.f3, for example, set $exclude = 'f1|f2|f3'. # To not exclude any files set $exclude = ''. $exclude = '' if ($exclude) { $re = '^' + $prefix + 'o\.(?!' + $exclude + ')(.*)$' } else { $re = '^' + $prefix + 'o\.(.*)$' } get-childitem | where-object { $_.Name -match $re } | remove-item # Remove all scratch files. $re = '^scratch_([a-zA-Z]+)$' get-childitem | where-object { $_.Name -match $re } | remove-item # Remove all .dbg files. $re = '^([a-zA-Z]+)\.dbg$' get-childitem | where-object { $_.Name -match $re } | remove-item # Remove local_runtime_XXXX.dat file. $re = '^local_runtime_([0-9]+)\.dat$' get-childitem | where-object { $_.Name -match $re } | remove-item # Remove progress.dat file. remove-item 'progress.dat' -ErrorAction Ignore
There are two small tasks that must be completed before you can use this PowerShell script.
First, you will need to update the PowerShell execution policy so that PowerShell will allow you to run scripts created on the device. Follow these simple steps:
Type ‘Windows PowerShell’ into the start menu
Right-click ‘Windows PowerShell’ and select ‘Run as administrator’
Click ‘Yes’ to open Windows PowerShell
Type the following command to allow scripts to run: ‘Set-ExecutionPolicy RemoteSigned’
Press Enter
Type ‘Y’ for Yes
Press Enter
The execution policy has now been updated. Close Windows PowerShell.
Note: this setting will apply to all subsequent PowerShell sessions unless updated.
The image below illustrates what Windows PowerShell should look like after updating he execution policy:
Next, you will need to save the script above into a PowerShell cmdlet file (.ps1). Follow these steps:
BEFORE
Open your favourite text editor (e.g. Notepad)
Copy and paste the entire PowerShell script above into the text editor
Note: if you would like to retain any particular files you can specify specific file types from the script.
See comments on lines 7-9 of the script for further info
Save the file as ‘cleanup.ps1’ in the project folder
Note: the ‘.ps1’ file type identifies the file as a PowerShell cmdlet file.
You have now created the PowerShell cmdlet (similar to an executable file) and updated the execution policy to allow you to run new scripts. To use the script follow these steps:
AFTER
Browse to the model folder that you would like to clean up
i.e. where you saved the ‘cleanup.ps1’ file
Type 'powershell' into the address bar, and hit Enter
The Windows PowerShell command line opens and the current directory will already be the desired project folder
Type '.\cleanup.ps1' and hit enter
Note: the ‘.\’ before ‘cleanup.ps1’ indicate that this is a trusted command. If you don’t include the ‘.\’ the script won’t run, and a simple error message will be displayed
Type the model prefix and hit Enter
The PowerShell script requires one argument, namely the name or prefix of the model
Note: in the image below I am cleaning up the ‘abdul_snowmelt’ verification problem
After running the ‘cleanup.ps1’ PowerShell script you should notice that all the model output files in the selected project folder have been deleted. The images below illustrate the ‘abdul_snowmelt’ verification problem’s folder before and after running the ‘cleanup.ps1’ PowerShell script.
We do hope you find this script useful, but once again I must note that Aquanty does not bear any responsibility for potential misapplications of this script, so BE VERY CAREFUL!