Link to home
Start Free TrialLog in
Avatar of bottomlinemg
bottomlinemg

asked on

loop through directories and move files

I need help creating a vbs that will
loop through all subdirecotories of a directory called images and move all the files within all the subdirectories to the main directory called images.
once the directory is empty it should delete it  (but never delete files - only move them to the main directory)

can someone help me with this
ASKER CERTIFIED SOLUTION
Avatar of divyeshhdoshi
divyeshhdoshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is easier to do in batch than in vbscript; recursing subdirectories with a WMI query is possible (as in http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0218.mspx) but cumbersome.

Copy the script below into a new file with a .cmd extension.  Change the value of the "root" variable to the path of the images directory.  Running the script will move all subfiles up to the images folder and delete any empty folders.

If there are any duplicate files then they will not be moved and their folders will not be deleted.


@echo off
setlocal enabledelayedexpansion
 
set root=c:\images
 
for /F "tokens=* usebackq" %%G in (`dir "%root%" /A:D /B`) do (
 for /F "tokens=* usebackq" %%H in (`dir "%root%\%%G" /A:-D /B /S`) do (
  echo N|move /-Y "%%H" "%root%"
 )
)
 
for /F "tokens=* usebackq" %%G in (`dir "%root%" /A:D /B`) do (
 set contents=
 for /F "tokens=* usebackq" %%H in (`dir "%root%\%%G" /A:-D /B /S 2^>NUL`) do (
  set contents=%%H
 )
 if not defined contents rd /q /s "%root%\%%G"
)

Open in new window