2

I have a batchfile that looks like this:

IF EXIST icon.ico (
    attrib -S -H -R desktop.ini
    echo f | xcopy /H /Y "%~dp0desktop.ini.txt" desktop.ini
    attrib +S +H +A desktop.ini
)

This works perfectly for my needs. My question is how do I make this run in all sub-folders?

1 Answer 1

5

How do I a run command in all subdirectories in a batch file?

You can use for /r for this.

Use following batch file:

for /r "starting_directory" %%a in (.) do (
  pushd %%a
  if exist icon.ico (
    attrib -s -h -r desktop.ini
    echo f | xcopy /h /y "%~dp0desktop.ini.txt" desktop.ini
    attrib +s +h +a desktop.ini
    ) 
  popd 
  )

for /r - 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)

Source for /r - Loop through files (Recurse subfolders).


Further Reading

1
  • I already figured this out before seeing your answer. Your solution is identical to mine, thanks! Apr 3, 2015 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .