Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
Batch as in Windows batch? – jscott Jun 25 '13 at 17:55
Yes, Windows batch. – Jason C Jun 25 '13 at 19:31
up vote 0 down vote accepted

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.

share|improve this answer
That worked. 'forfiles /p "<basepath>" /s /m <file searched> /c "cmd /c call "%~f0" /callback @path"' And then used @path – Jason C Jun 25 '13 at 20:30

What OS are you running on? Something like this would work for Perl:

opendir DIR, $dirname or die "Couldn't open dir '$dirname': $!";
my @files = readdir(DIR); 
closedir DIR; 

foreach my $file (@files) {
   if ($file =~ /filename/) {
      print "Matched file process";
share|improve this answer
Windows Server 2012 R2 – Jason C Jun 25 '13 at 19:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.