[windows] batch shell

spring·2020년 11월 9일

배치 쉘은 파일로 적는게 아니라 쉘(명령 프롬프트)에서 직접 실행하는 것이다.

리눅스와 다르게 윈도우의 배치는 파일과 쉘에서의 문법이 조금 다를때가 있다.

1. 파일명에 prefix 일괄 붙이기

Shell

for /f "tokens=*" %a in ('dir /b') do ren "%a" "WhateverYouWant_%a"

File

for /f "tokens=*" %%a in ('dir /b') do ren "%%a" "WhateverYouWant_%%a"

2. 하위 디렉토리 용량 확인

powershell용

Get-ChildItem -Directory | ForEach-Object {
    $sizeBytes = (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum
    $unit = "Bytes"
    
    if ($sizeBytes -ge 1TB) {
        $sizeBytes /= 1TB
        $unit = "TB"
    } elseif ($sizeBytes -ge 1GB) {
        $sizeBytes /= 1GB
        $unit = "GB"
    } elseif ($sizeBytes -ge 1MB) {
        $sizeBytes /= 1MB
        $unit = "MB"
    } elseif ($sizeBytes -ge 1KB) {
        $sizeBytes /= 1KB
        $unit = "KB"
    }

    [PSCustomObject]@{
        Folder = $_.Name
        Size   = "{0:N2} {1}" -f $sizeBytes, $unit
    }
} | Format-Table -AutoSize

cmd용

powershell -command "Get-ChildItem -Directory | ForEach-Object { $sizeBytes = (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum; $unit = 'Bytes'; if ($sizeBytes -ge 1TB) { $sizeBytes /= 1TB; $unit = 'TB' } elseif ($sizeBytes -ge 1GB) { $sizeBytes /= 1GB; $unit = 'GB' } elseif ($sizeBytes -ge 1MB) { $sizeBytes /= 1MB; $unit = 'MB' } elseif ($sizeBytes -ge 1KB) { $sizeBytes /= 1KB; $unit = 'KB' }; [PSCustomObject]@{ Folder = $_.Name; Size = '{0:N2} {1}' -f $sizeBytes, $unit } } | Format-Table -AutoSize"

xcopy 관련

xcopy <src> <dst>.* 을 쓰면 파일인지 디렉토리 인지 묻지 않는다.

profile
Researcher & Developer @ NAVER Corp | Designer @ HONGIK Univ.

0개의 댓글