LoginSignup
0
0

新しい .mp4 ファイルが特定のフォルダに作成されるのを監視して、別フォルダに移動

Last updated at Posted at 2024-01-25

プログラム内のコードを手順に織り込んで説明します。

MP4WatchAutoMove.py プログラムの環境構築手順:

  1. Pythonのインストール:

  2. 必要なライブラリのインストール:

    • コマンドライン(またはターミナル)を開き、以下のコマンドを実行して必要なライブラリ watchdog をインストールします。
    pip install watchdog
    
  3. プログラムファイルのセットアップ:

    • 以下のプログラムコードを MP4WatchAutoMove.py という名前で新しいファイルに保存してください。
    import os
    import time
    import shutil
    from watchdog.observers import Observer
    from watchdog.events import FileSystemEventHandler
    
    class MyHandler(FileSystemEventHandler):
        def __init__(self, source_folder, destination_folder):
            self.source_folder = source_folder
            self.destination_folder = destination_folder
    
        def on_created(self, event):
            if event.is_directory:
                return None
    
            elif event.src_path.endswith('.mp4'):
                original_file_name = os.path.basename(event.src_path)
                new_file_name = "Visla0.mp4"
    
                # 検出時間と元のファイル名の記録
                detection_time = time.ctime()
                with open("MP4WatchAutoMove_log.txt", "a") as log_file:
                    log_file.write(f"Detected file: {original_file_name} at {detection_time}\n")
    
                print(f"{original_file_name} detected.")
                time.sleep(180)  # 3分待機
    
                dst_path = os.path.join(self.destination_folder, new_file_name)
                shutil.move(event.src_path, dst_path)
    
                # 移動時間と新しいファイル名の記録
                move_time = time.ctime()
                with open("MP4WatchAutoMove_log.txt", "a") as log_file:
                    log_file.write(f"Moved file: {original_file_name} to {new_file_name} at {move_time}\n")
    
                print(f"Moved {original_file_name} to {new_file_name}.")
    
                # 30秒待機してファイルをタッチ
                time.sleep(30)
                touch_file_path = "U:\\TTS\\B_Visla.mp4.txt"
                with open(touch_file_path, "a"):
                    os.utime(touch_file_path, None)
    
    if __name__ == "__main__":
        source_folder = "C:\\Users\\ABC\\Downloads"  # 移動元のフォルダへのパス
        destination_folder = "U:\\TTS"  # 移動先のフォルダへのパス
    
        event_handler = MyHandler(source_folder, destination_folder)
        observer = Observer()
        observer.schedule(event_handler, source_folder, recursive=False)
    
        # 監視開始のメッセージを出力
        start_message = "Starting MP4WatchAutoMove. Monitoring for new .mp4 files in Downloads folder."
        print(start_message)
        with open("MP4WatchAutoMove_log.txt", "a") as log_file:
            log_file.write(start_message + "\n")
    
        observer.start()
    
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()
    
  4. フォルダの設定:

    • プログラム内の以下の行で、source_folderdestination_folder 変数に、それぞれのフォルダのパスを設定してください。
    source_folder = "C:\\Users\\ABC\\Downloads"  # 移動元のフォルダへのパス
    destination_folder = "U:\\TTS"  # 移動先のフォルダへのパス
    
    • source_folder には新しい .mp4 ファイルが作成されるフォルダのパスを指定し、destination_folder には .mp4 ファイルを移動するフォルダのパスを指定します。
  5. プログラムの実行:

    • コマンドラインまたはターミナルを開き、プログラムが保存されているフォルダに移動します。
    • 次に、以下のコマンドを実行してプログラムを起動します。
    python MP4WatchAutoMove.py
    

    プログラムは新しい .mp4 ファイルを監視し、検出・移動・ファイルのタッチの操作を行います。

以上の手順に従うことで、MP4WatchAutoMove.py プログラムを実行するための環境が整います。プログラムをカスタマイズする場合は、フォルダパスや待機時間などを調整してください。

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