Last time, I have finished generating the file of the deletion target list. The contents of the file look like this.
\TMP\200104\delete.txt
C:\TMP\IMG_2607(1).jpg
C:\TMP\IMG_2608(1).jpg
\TMP\200105\delete.txt
C:\TMP\IMG_2610(1).jpg
C:\TMP\IMG_2610(2).jpg
C:\TMP\IMG_2611(1).jpg
There is delete.txt in each year / month (yyyymm) folder that contains the full path of the file you want to delete line by line, so just read this and delete it. Since it is still in the testing stage, I will write the process for the test folder.
Python : 3.8.3
DeleteAll.py
import glob
import os
if __name__ == "__main__":
files = glob.glob("C:\\tmp\\**\\delete.txt", recursive=True)
for file in files:
with open(file, "r") as f:
for line in f.readlines():
os.remove(line.strip())
Mr. glob. Why does'\ n'stick at the end when reading with readlines ()? (A little disturbing)
C# : 8.0(Microsoft Visual Studio Community 2019)
DeleteAll.cs
using System;
using System.IO;
using System.Text;
namespace DeleteAll
{
class DeleteAll
{
static void Main()
{
var files = Directory.GetFiles(@"c:\tmp", "delete.txt", SearchOption.AllDirectories);
foreach (var file in files)
{
foreach (var line in File.ReadAllLines(file, Encoding.GetEncoding("Shift-JIS")))
{
File.Delete(line);
}
}
}
}
}
I don't usually think about it, but when I put it side by side with Python, the parentheses seemed to be very disturbing ...
VisualBasic : 6.0
DeleteAll.bas
Option Explicit
Private colFiles As Collection
Public Sub Main()
Set colFiles = New Collection
Call GetFiles("C:\TMP\")
Call DeleteFiles(colFiles)
End Sub
Private Sub GetFiles(ByVal strSearchPath As String)
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
Dim objSubFolder As Folder
For Each objSubFolder In FSO.GetFolder(strSearchPath).SubFolders
Call GetFiles(objSubFolder.Path)
Next
Dim objFile As file
For Each objFile In FSO.GetFolder(strSearchPath).files
If LCase(objFile.Name) = "delete.txt" Then
Call colFiles.Add(FSO.BuildPath(strSearchPath, objFile.Name))
End If
Next
End Sub
Private Sub DeleteFiles(ByVal files As Collection)
Dim FSO As FileSystemObject
Dim vntFile As Variant
Set FSO = New FileSystemObject
For Each vntFile In files
With FSO.GetFile(vntFile).OpenAsTextStream(ForReading)
Do
Kill .ReadLine()
Loop Until (.AtEndOfLine())
End With
Next
End Sub
In the old-fashioned Hungarian notation. It is very troublesome to implement recursive search by yourself. With this, it is important to bind without using FileSystemObject. The variable declaration in GetFiles () is written to declare Dim just before it appears, but in the old VB6 source, it is customary to write the declaration collectively at the beginning of the method. I think it's the style of people who flowed C-> VB, but I often see shit code that has 500 lines per method only for sources written like that.
DeleteAll.bat
@echo off
cd \tmp
for /f "usebackq" %%i in (`dir /s /b delete.txt`) do (for /f %%j in (%%i) do del %%j)
How simple a batch file is! Instead, the format is a bit esoteric.
This completes this case. Now, what are we going to do next?
Recommended Posts