[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3478
  • Last Modified:

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
0
bottomlinemg
Asked:
bottomlinemg
1 Solution
 
divyeshhdoshiCommented:
SUB MOVEFILES(FILE DIR)
        FOREACH FILE F IN DIR.FILES
               IF(F.ISDIRECTORY)
                   MOVEFILE(DIR,F)
                    F.DELETE();
               ELSE
                    F.MOVE(DIR)
      NEXT
END SUB

SUB MOVEFILE(FILE ROOT, FILE CURRENT)
   FOREACH FILE F IN CURRENT.FILES
            IF(F.ISDIRECTORY)
                 MOVEFILE(ROOT,F)
                 F.DELETE()
             ELSE
                  F.MOVE(ROOT)
  NEXT
END SUB

hi THIS CODE MAY HAVE SYNTAX ERRORS,
Regards,
Divyesh Doshi
0
 
Shift-3Commented:
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

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now