LoginSignup
0
0

vbファイルを削除

Posted at
Dim targetFolder, fs, folder, file, currentDate, sevenDaysAgo

' 対象のフォルダパスを指定
targetFolder = "C:\Path\To\Your\Folder"

' FileSystemObject を作成
Set fs = CreateObject("Scripting.FileSystemObject")

' 現在の日付を取得
currentDate = Date



' 7日前の日付を計算
sevenDaysAgo = DateAdd("d", -7, currentDate)

' 対象フォルダを開く
Set folder = fs.GetFolder(targetFolder)

' フォルダ内の各ファイルに対して処理
For Each file In folder.Files
    ' ファイルの更新日を取得
    fileDate = file.DateLastModified
    
    ' 更新日が7日前よりも古い場合は削除
    If fileDate < sevenDaysAgo Then
        fs.DeleteFile file.Path
        WScript.Echo "File deleted: " & file.Path
    End If
Next

' メッセージ表示(任意)
WScript.Echo "Files older than 7 days have been deleted."
Imports System.IO

Module Module1
    Sub Main()
        Dim targetFolder As String = "C:\Path\To\Your\Folder"
        Dim currentDate As DateTime = DateTime.Now
        Dim sevenDaysAgo As DateTime = currentDate.AddDays(-7)

        Dim directoryInfo As New DirectoryInfo(targetFolder)

        For Each file In directoryInfo.GetFiles()
            Dim fileDate As DateTime = file.LastWriteTime

            If fileDate < sevenDaysAgo Then
                Try
                    File.Delete(file.FullName)
                    Console.WriteLine("File deleted: " & file.FullName)
                Catch ex As Exception
                    Console.WriteLine("Error deleting file: " & ex.Message)
                End Try
            End If
        Next

        Console.WriteLine("Files older than 7 days have been deleted.")
    End Sub
End Module
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0