하위 디렉토리에 있는 파일을 통해 반복되는 배치 파일

John Wachira 2023년3월20일 Batch Batch Loop
하위 디렉토리에 있는 파일을 통해 반복되는 배치 파일

이 문서에서는 하위 디렉터리의 파일을 반복하는 Batch 스크립트를 작성하는 방법을 설명합니다. 개념을 설명하기 위해 예제를 다룰 것입니다.

하위 디렉토리에 있는 파일을 통해 반복되는 배치 파일

아래 그림과 같은 디렉토리 구조가 있다고 가정해 봅시다.

- Main directory (Contains our .bat file and some top-level directories)
  - Sub-directory
    - Search Directory
      - A bunch of files (Files we want to loop through)

Main directory에서 아래 명령을 사용하여 파일 및 폴더 목록을 얻을 수 있습니다.

for /f %%f in ('dir /b /r *') do echo %%f

그러나 Search Directory에 있는 파일을 원합니다. 다음은 Search Directory에 있는 파일 구조의 예입니다.

C:\Users\pc\Search16\0045\search\FP585.txt

이전 예제 명령에서 설명한 것처럼 /f를 사용하는 대신 /R을 사용하여 아래 그림과 같이 모든 하위 디렉토리의 모든 파일을 반복할 수 있습니다.

@echo off
for /R %%f in (*.txt) do echo %%f

위의 명령은 모든 하위 디렉토리에서 .txt 파일 확장자를 가진 모든 파일 목록을 표시합니다.

결론적으로 위에서 설명한 것처럼 모든 하위 디렉터리의 파일을 반복하는 Batch 명령을 작성할 때 /f/R로 대체할 수 있습니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

관련 문장 - Batch Loop