Методы удаления файлов с определенным расширением на разных языках программирования

Чтобы удалить файлы с определенным расширением, вы можете использовать различные языки программирования и платформы. Вот несколько примеров на разных языках:

  1. Python (с использованием модуля os):

    import os
    def remove_files_with_extension(directory, extension):
    for filename in os.listdir(directory):
        if filename.endswith(extension):
            file_path = os.path.join(directory, filename)
            os.remove(file_path)
    # Usage example:
    remove_files_with_extension('/path/to/directory', '.txt')
  2. Bash (с помощью команды поиска):

    find /path/to/directory -type f -name "*.txt" -delete
  3. PowerShell:

    $extension = ".txt"
    Get-ChildItem -Path "C:\path\to\directory" -Recurse | Where-Object { $_.Extension -eq $extension } | Remove-Item
  4. JavaScript (Node.js):

    const fs = require('fs');
    const path = require('path');
    function removeFilesWithExtension(directory, extension) {
    fs.readdir(directory, (err, files) => {
    if (err) throw err;
    files.forEach(file => {
      if (path.extname(file) === extension) {
        fs.unlink(path.join(directory, file), err => {
          if (err) throw err;
          console.log(`${file} was deleted.`);
        });
      }
    });
    });
    }
    // Usage example:
    removeFilesWithExtension('/path/to/directory', '.txt');