DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

questorfla

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

where var1 = list of names (20 or so)<Loop A>
and
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.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Xcopy Batch to run a loop inside of another loop loop
« Reply #1 on: October 18, 2014, 11:05 AM »
Code: (generic)
REM start in the Root folder
@echo off
rem For each User folder do:
for /d %%Y in (*) do (
rem Change to User folder
  pushd "%%~Y"
rem For each Client folder do:
  for /d %%U in (*) do (
    robocopy "%%~U\cache" "C:\backupdata\%%~Y+%%~U\cache" /e
    robocopy "%%~U\images\login" "C:\backupdata\%%~Y+%%~U\images\login" /e
  )
rem Return to parent folder (\Name)
   popd
)
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Original address: https://www.donationcoder.com/forum/index.php?topic=39238.0