Donnerstag, 10. März 2016

Moving Contents of subdirectories in a Windows batch or on the command line

Imagine you encounter a directory scruture like this:
In each of these yearly folders, there are many subfolders. My goal was to flatten this structure so that all of these subfolders are in th current folder (in other words, one level higher that they are now). It turns out that this is not so easy in Windows because the command move does not intepret * to include folders. Superuser.com to the rescue! User Rik proposed this nifty solution that works like a charm:
 @echo off  
 for /d %%d in ("*") do (  
  for /d %%e in ("%%d\*") do (  
   move "%%e" .  
  )  
 )  
Note: this code only works for subdirectories. If the yearly folders directly contain files, they will not be copied. In this case, use this code:
 @echo off  
 for /d %%d in ("*") do (  
  for /d %%e in ("%%d\*") do (  
   move "%%e" .  
  )  
  move "%%d\*" .  
 )  
Thanks a lot, Rik!

Keine Kommentare:

Kommentar veröffentlichen