1. PowerShell을 사용한 방법
Get-ChildItem -Path "시작폴더경로" -Filter "*.txt" -Recurse | Get-Content | Set-Content 결과파일.txt
>> 위 명령어는 -Recurse 옵션을 사용하여 모든 서브 폴더의 .txt 파일을 찾아 결과파일.txt로 합침
2. 명령 프롬프트(CMD)에서 for 루프 사용 - 가장 추천
for /r "시작폴더경로" %f in (*.txt) do type "%f" >> 결과파일.txt
>> 윈도우 CLI 가 아닌 배치 파일(.bat)을 만들어 사용할 경우에는 %f 대신 %%f를 사용해야 합니다:
>> 윈도우에서 서브 폴더에 있는 *.dat 파일 합치기

3. PowerShell에서 특정 파일만 선택적으로 합치기:
Get-ChildItem -Path "시작폴더경로" -Filter "*.txt" -Recurse | Where-Object {$_.Name -like "*검색어*"} | Get-Content | Set-Content 결과파일.txt
4. PowerShell 각 파일 사이에 구분자 추가하기
$files = Get-ChildItem -Path "시작폴더경로" -Filter "*.txt" -Recurse
$separator = "`n`n==== 파일: {0} ====`n`n"
$output = @()
foreach ($file in $files) {
$output += [string]::Format($separator, $file.FullName)
$output += Get-Content $file.FullName
}
$output | Set-Content 결과파일.txt
5. PowerShell에서 대용량 파일을 처리할 경우
$writer = [System.IO.StreamWriter]::new("결과파일.txt")
Get-ChildItem -Path "시작폴더경로" -Filter "*.txt" -Recurse | ForEach-Object {
$writer.WriteLine("==== 파일: $($_.FullName) ====")
Get-Content $_.FullName | ForEach-Object { $writer.WriteLine($_) }
}
$writer.Close()
끗.
'OS > Windows' 카테고리의 다른 글
윈도우 - WACS 이용 SSL 발급 자동화 (2) | 2025.04.09 |
---|---|
윈도우 - 특정 포트 확인 및 종료 (0) | 2025.04.09 |
윈도우 - 특정 내용 가지고 있는 파일 찾기 (0) | 2025.03.31 |
바탕화면 바로가기 아이콘 깨짐 (0) | 2025.02.27 |
긴 경로명, 긴 파일명 허용 처리 (0) | 2025.02.27 |