如何删除Tesseract中无对应.box文件的.gt.txt与.tif文件
删除无对应.box文件的.gt.txt与.tif文件
适用于Linux/macOS的Shell脚本
遍历所有.gt.txt文件,检查对应的.box文件是否存在,若不存在则删除该.gt.txt及其配对的.tif文件:
# 遍历当前目录下的所有.gt.txt文件 for f in *.gt.txt; do # 提取基础文件名(去掉.gt.txt后缀) base="${f%.gt.txt}" # 检查对应的.box文件是否存在 if [ ! -f "$base.box" ]; then # 删除无.box配对的.gt.txt和.tif文件 rm "$f" "$base.tif" echo "已清理:$f 和 $base.tif" fi done
如果需要递归处理子目录,改用find命令:
find . -name "*.gt.txt" -type f | while read -r gtfile; do base="${gtfile%.gt.txt}" boxfile="$base.box" tiffile="$base.tif" if [ ! -f "$boxfile" ]; then rm "$gtfile" "$tiffile" echo "已清理:$gtfile, $tiffile" fi done
适用于Windows的PowerShell脚本
Windows环境下用以下PowerShell命令批量处理:
Get-ChildItem -Filter *.gt.txt -Recurse | ForEach-Object { $base = $_.BaseName -replace '\.gt$', '' $boxPath = Join-Path $_.DirectoryName "$base.box" $tifPath = Join-Path $_.DirectoryName "$base.tif" if (-not (Test-Path $boxPath)) { Remove-Item $_ -Force if (Test-Path $tifPath) { Remove-Item $tifPath -Force } Write-Host "已删除:$($_.FullName) 和 $tifPath" } }
内容的提问来源于stack exchange,提问作者Dellu
相关产品推荐
相关产品推荐

