Loop through files (Recurse subfolders)  시스템프로그래밍 

2013.06.18. 18:42  수정  삭제

복사http://blog.naver.com/pwk0810/40191317244

전용뷰어 보기

Loop through files (Recurse subfolders)

  Syntax
       FOR /R [[ drive :] path ] %% parameter IN ( set ) DO command

 Key
    drive : path : The folder tree where the files are located.

    set : A set of one or more files.  Wildcards must be used.
                  If (set) is a period character (.) then FOR will
                  loop through every folder.

   command : The command(s) to carry out, including any
                  command-line parameters.

    %% parameter : A replaceable parameter:
                  in a batch file use %%G (on the command line %G) 

This command walks down the folder tree starting at [drive:]path, and executes the DO statement against each matching file. 

If the [drive:]path are not specified they will default to the current drive:path.

Unlike some other variants of the FOR command you must include a wildcard (either * or ?) in the 'set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard eg if you are looping through multiple folders to find the exact filename myfile.txt you could instead specify myfile.t?t 

Examples:

Delete every .bak file in every subfolder starting at C:\temp 

C:\> FOR /RC:\temp\ %%G IN (*.bak) DO del %%G

List all the subfolders under C:\Work

FOR /R "C:\Work\" %%G in (.) DO ( 
Pushd %%G 
Echo now in %%G 
Popd ) 
Echo "back home"

FOR is an internal command. 

“Just think how happy you would be if you lost everything you have right now, and then got it back again” - Frances Rodman

Related: 

FOR - Loop commands 
FOR - Loop through a set of files in one folder 
FOR /D - Loop through several folders 
FOR /L - Loop through a range of numbers 
FOR /F - Loop through items in a text file 
FOR /F - Loop through the output of a command 
FORFILES - Batch process multiple files 
IF - Conditionally perform a command 
Powershell: ForEach-Object - Loop for each object in the pipeline 
Equivalent bash command (Linux): for - Expand words , and execute commands

'SYSTEM PROGRAMMING' 카테고리의 다른 글

CREATE PROC dbo.UP_INSERT_BOARDNEW  (0) 2015.08.05
C# 문법 특징  (0) 2015.08.05
I/O Completion Port 모델  (0) 2015.08.05
윈도우즈 기반 I/O 모델  (0) 2015.08.05
세마포어(Semaphore) 기반의 동기화  (0) 2015.08.05
Posted by 뉴암스테르담
l