Powershell: Tự động hóa việc phân loại ảnh và tài liệu

 1. Tự động phân loại hình ảnh theo tháng


Mở powershell và paste đoạn code này:

# 1. Đường dẫn đến thư mục chứa hình:Sửa lại theo thực tế

$sourcePath = "C:\Users\PKUser\Pictures\Screenshots"

# Chuyển đến thư mục đó

Set-Location $sourcePath

# 2. Lấy danh sách các file hình ảnh (jpg, png, heic, v.v.)

$files = Get-ChildItem -File | Where-Object { $_.Extension -match "jpg|jpeg|png|heic|webp" }

foreach ($file in $files) {

    # 3. Lấy tháng và năm từ ngày tạo file (CreationTime)

    $month = $file.CreationTime.ToString("MM")

    $year = $file.CreationTime.Year

    $folderName = "$month$year" # Kết quả: 072025


    # 4. Kiểm tra nếu thư mục chưa có thì tạo mới

    if (!(Test-Path $folderName)) {

        New-Item -ItemType Directory -Name $folderName

    }


    # 5. Di chuyển file vào thư mục tương ứng

    Move-Item -Path $file.FullName -Destination $folderName

    Write-Host "Đã dọn dẹp: $($file.Name) -> $folderName" -ForegroundColor Cyan

}

Write-Host " Hình ảnh đã được phân loại theo tháng." -ForegroundColor Green


 

=>Lưu code này vào notepad dưới đuôi PS1, sử dùng task scheduler để lên lịch tự động dọn file

2. Phân loại file theo Type:

 


# 1. Đường dẫn đến thư mục cần dọn dẹp . Sửa lại theo thực tế

$sourcePath = "C:\Users\PKUser\Downloads"

Set-Location $sourcePath


# 2. Map chi tiết theo danh sách đuôi file, chỉnh sửa lại theo chu cầu:

$groupMap = @{

    "Word"         = ".docx", ".doc", ".pdf", ".txt", ".ini"

    "Excel"       = ".xlsx", ".xls", ".csv", ".scv", ".dbf"

    "PPT"    = ".pptx", ".ppt"

    "Image"        = ".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg", ".avif", ".img"

    "Video"           = ".mp4"

    "MP3"        = ".mp3", ".wav", ".wma"

    "ZIP"             = ".zip", ".rar"

    "APP"         = ".exe", ".msi", ".apk"

    "Web"        = ".html", ".css", ".json", ".geojson", ".xml"

    "cad" = ".dxf", ".drawio", ".glb", ".onx", ".cmz", ".cur"

    "PBI"   = ".pbix"

    "Khac"            = ".download",".shp", ".shx", ".prj",".iqy", ".msapp"

}


# 3. Quét và di chuyển file

foreach ($folderName in $groupMap.Keys) {

    $extensions = $groupMap[$folderName]

    # Lấy file, bao gồm cả xử lý chữ hoa/thường (như .DOCX và .docx)

    $files = Get-ChildItem -File | Where-Object { $extensions -contains $_.Extension.ToLower() }


    if ($files) {

        if (!(Test-Path $folderName)) {

            New-Item -ItemType Directory -Name $folderName

        }

        

        foreach ($file in $files) {

            try {

                Move-Item -Path $file.FullName -Destination $folderName -Force -ErrorAction Stop

                Write-Host "Ok: $($file.Name) -> $folderName" -ForegroundColor Cyan

            } catch {

                Write-Host "Loi file $($file.Name): $($_.Exception.Message)" -ForegroundColor Red

            }

        }

    }

}

Write-Host "Đã phân loại files theo Type" -ForegroundColor Green

=>Lưu code này vào notepad dưới đuôi PS1, sử dùng task scheduler để lên lịch tự động dọn file

Nhận xét