Batch File to Loop Through Folders and Process If Specific File Found

batchbatch-file

I have a large number of websites running the same CMS that I want to update as a batch. As the update process just requires copying the updated files into the folder of the given sites, a batch files seems suitable to the task.

Is there a good method for iterating through a list of folders one level deep (don't need to check deeper), checking if a specific file exists in that folder, and if found, run some commands (in this case, I will have it run a command to copy from a destination to the folder)?

Best Answer

In this case, you can use Forfiles command ( It's native in Windows 7/2008/Vista and must install from Resource Kit in Windows Server 2003 ).

Just list all your folder in a text file. Then using a for loop to iteract each folder, feeding them to Forfiles command with /p param, search for specify filename with /m param. If matching, execute command with /c param.

Anyway, I advice you using some more powerfull tools like PowerShell.