반응형
$sourceDir = Get-Location

# 디렉토리가 존재하는지 확인
if (-Not (Test-Path $sourceDir)) {
    Write-Host "지정한 폴더가 존재하지 않습니다."
    exit
}

# 파일 목록 가져오기 (파일만)
$files = Get-ChildItem -Path $sourceDir -File

foreach ($file in $files) {
    # 파일 이름에서 앞 6자리 추출 (예: 202103)
    $prefix = $file.Name.Substring(0,6)

    # 폴더 이름을 yyyy_MM 형식으로 변경 (예: 2021_03)
    $folderName = $prefix.Substring(0,4) + "_" + $prefix.Substring(4,2)
    $targetDir = Join-Path -Path $sourceDir -ChildPath $folderName

    # 대상 폴더가 없으면 생성
    if (-Not (Test-Path $targetDir)) {
        New-Item -Path $targetDir -ItemType Directory | Out-Null
    }

    # 파일을 해당 폴더로 이동
    $targetPath = Join-Path -Path $targetDir -ChildPath $file.Name
    Move-Item -Path $file.FullName -Destination $targetPath -Force
}

Write-Host "파일 정리가 완료되었습니다."

Powershell 에서 파일 정리하기 귀찮아서 생성한 스크립트

네트워크 드라이버 (UNC 경로) 사용하려면 아래와 같이 $sourceDir 수정해서 실행하면 됨

$sourceDir = "\\192.168.1.100\Photos"

반응형

+ Recent posts