Skip to content

Instantly share code, notes, and snippets.

@martanto
Last active May 22, 2024 08:36
Show Gist options
  • Save martanto/922509c49ff6fdbdb92d826cad8cfebd to your computer and use it in GitHub Desktop.
Save martanto/922509c49ff6fdbdb92d826cad8cfebd to your computer and use it in GitHub Desktop.
Remove empty seismic file directory using powershell
$whatIf = $false
$removeHiddenFiles = $true
$minimumSize = 128 #dalam KB
$global:fileRemoved = 0
$global:lengthRemoved = 0
Function Delete-EmptyFolder($path)
{
Foreach ($subFolder in Get-ChildItem -Force -Literal $path -Directory)
{
Delete-EmptyFolder -path $subFolder.FullName
}
Write-Host ">>> Scanning :: ${path}"
$dirs = [System.IO.Directory]::GetDirectories($path)
$dir_count = $dirs.Count
$files = [System.IO.Directory]::GetFiles($path)
$file_count = $files.Count
if (($dir_count -eq 0) -and ($file_count -eq 0))
{
Write-Host "Remove empty directory : ${path}"
Remove-Item -Path $path -Force -WhatIf:$whatIf
}
if ($file_count -gt 0)
{
Foreach ($file in $files)
{
$length = (Get-Item -Path $file).Length /1KB
if ($length -lt $minimumSize)
{
$global:lengthRemoved = $global:lengthRemoved + $length
$global:fileRemoved = $global:fileRemoved + 1
Write-Host "Remove ${file} : ${length}KB"
Remove-Item -Path $file -Force -WhatIf:$whatIf
}
}
}
}
Write-Host "Running...."
# Run the script
Delete-EmptyFolder -path "E:\Data_Raw\KRAKATAU"
$global:lengthRemoved = $global:lengthRemoved / 1MB
Write-Host "Finish!"
Write-Host "$global:fileRemoved File(s) Removed"
Write-Host "$global:lengthRemoved MB Removed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment