avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday June 17, 2024, 5:22 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Xcopy Batch to run a loop inside of another loop loop  (Read 2716 times)


  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Xcopy Batch to run a loop inside of another loop loop
« on: October 12, 2014, 06:38 PM »
xcopy (var1)\(var2)\cache\*.* C:\backupdata\(var1)(+var2)
next to end

where var1 = list of names (20 or so)<Loop A>
var2 = list of their clients (up to 20)<LOOP B>

Tis is to create an exact copy of the files inside Only the cache folder stored in the backup above.  My problem is I always get lost in the "Loop-d-loop Logic" options.  (For x in ?= etc.) :-\ :-[
The folders exists in the past as stated each (name) may have from 0 to 20 (client) folders and each client folder has a (cache) folder.
I need to copy the complete contents of that folder to another one at C:\backupdata\"(name)+(client)"\cache\*.*
If the file already exists in that folder it should overwrite but none ever deleted.  After copying every subfolder and file in that "cache", it should shift to inputting the next client until it has run through all of them for that (name).  These names are all subfolder names in the root folder the batch starts in (or it can be one level down)
Root\names\clients\cache The structure is just like that.  Some (name)s may not have any (client)s but all (client)s have cache folders

The only reason for doing it this way is to avoid keeping backups of all the Other folders and files that are inside each client that are all standard.  I'm still trying to get inside the 2nd loop. :(  Any pointers.  I know it is probably something simple.  I tried pulling the variable from a list but it would be better if they could be pulled from the run itself since they are all names in a directory listing anyway.

The current process works fine but wastes a ton of time and space making redundant copies of unneeded files.


  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,643
    • View Profile
    • Donate to Member
Re: Xcopy Batch to run a loop inside of another loop loop
« Reply #1 on: October 18, 2014, 06:05 AM »
Code: Text [Select]
  1. REM start in the Root folder
  2. @echo off
  3. rem For each User folder do:
  4. for /d %%Y in (*) do (
  5. rem Change to User folder
  6.   pushd "%%~Y"
  7. rem For each Client folder do:
  8.   for /d %%U in (*) do (
  9.     robocopy "%%~U\cache" "C:\backupdata\%%~Y+%%~U\cache" /e
  10.     robocopy "%%~U\images\login" "C:\backupdata\%%~Y+%%~U\images\login" /e
  11.   )
  12. rem Return to parent folder (\Name)
  13.    popd
  14. )