VB Script: move-copy Folders and Subfolders

Hello experts,

I have a “Infolder” which contains sub folder:
Subf1
Sub2
Sub3
....

Each sub folder Contain a subfolder named "inconsistency-data" and xls files (it also contains other folders and files)

I would like to

Set a DesFolder
Create if they don't exist and

Subf1
Sub2
Sub3
....
 And copy just the subfolder "inconsistency-data" with all xls files if xls files already exist, the script should overwrite it.

Logs / script requirements:
1-Log file should be generated at InFolder and named as following "log-transfer-file"
2-Infolder should be defined as should be always the same in which is located the vbscript (Currentdir variable will be appreciate)
3-If Desfolder doesn't exist log output should  be: Now & Desfolder doesn't exist
4-If one of the Subfolder doesn't have the sub-subfolder "inconsistency-data" log output should Now Folder(Path) doesn't contains the sub-sub folder "inconsistency-data"
5-If one of inconsistency-data" folder doesn’t contains " log output should Now Folder(Path) & “inconsistency-data” is empty
6-If condition 3,4,5 are false logoutput should Now “SUCCESS: folders and files have been properly transferred”
LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
Have you tried using Xcopy or Robocopy?  Both commands are capable of creating folders in the process of copying.

Invoking a command from VBScript is usually handled with a wscript.shell object's .Run() method.
LD16Author Commented:
Hello Aikimark,

Thank you for this recommendation. Could you please provide a code so I customise it based on the requirements?

Thank you for your help.
NVITEnd-user supportCommented:
How about a .bat version?

Note: DirRoot is the parent folder of InFolder and DestFolder.  Change it to fit your need.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS

set DirRoot=c:\ParentOfInFolder

set DirIn=%DirRoot%\InFolder
set DirTgt=%DirRoot%\DestFolder
set FNLog=%DirIn%\log-transfer-file.txt

if not exist "%DirIn%" echo %date% %time% Missing source dir "%DirIn%">>%FNLog%"

for /d %%a in ("%DirIn%") do (
   if not exist "%%~a\sub*" (
      echo %date% %time% Missing source dir "%%~a\sub*">>%FNLog%"
   ) else (
      for /d %%A in ("%%~a\sub*") do (
         if not exist "%%~A\inconsistency-data" (
            echo %date% %time% Missing source dir %%~A\inconsistency-data>>%FNLog%"
         ) else (
            if not exist "%DirTgt%\%%~nA\inconsistency-data" md "%DirTgt%\%%~nA\inconsistency-data"
            xcopy /s /y "%%~A\inconsistency-data" "%DirTgt%\%%~nA\inconsistency-data"
            if %errorlevel% equ 0 (
               echo %date% %time% SUCCESS: "%%~A\inconsistency-data" folders and files have been properly transferred>>%FNLog%"
            ) else (
               echo %date% %time% FAILED: "%%~A\inconsistency-data" error during COPY>>%FNLog%"
            )
         )
      )
   )
)
goto :eof

Open in new window

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Bill PrewIT / Software Engineering ConsultantCommented:
(haven't had chance to look at this one yet...)

~bp
Bill PrewIT / Software Engineering ConsultantCommented:
I think this covers everything, give it a test and let me know if you have any questions or problems.  Adjust Base and Dest folder paths as needed.

' Option to display some information to the console (STDOUT) during processing for testing
blnDebug = True

' Text file I/O constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'Create the file system object for creating folders:
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Define folders and files to work with
strBaseDir = objFSO.GetAbsolutePathName(".") & "\Base"
strDestDir = objFSO.GetAbsolutePathName(".") & "\Dest"
strLogFile = strBaseDir & "\Log-Transfer-File.txt" 
strDataDir = "inconsistency-data"
strFileFilter = ".xls"

' Make sure input folder exist
If Not objFSO.FolderExists(strBaseDir) Then
   Wscript.Echo Now & " ERROR: Input folder """ & strBaseDir & """ does not exist."
   Wscript.Quit
End If

' Open log file for appending
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' Make sure destination folder exist
If Not objFSO.FolderExists(strDestDir) Then
   objLog.WriteLine Now & " ERROR: Destination folder """ & strDestDir & """ does not exist."
   objLog.Close
   Wscript.Quit
End If

' Process first level folders in source folder
intWarnings = 0
For Each objFolder In objFSO.GetFolder(strBaseDir).SubFolders
   If blnDebug Then Wscript.Echo Now & " DEBUG: Processing folder """ & objFolder.Path & """."
   strDataFolder = objFolder.Path & "\" & strDataDir

   ' Process our data folder in this subfolder
   If objFSO.FolderExists(strDataFolder) Then

      ' Process all files in this data folder
      intFiles = 0
      For Each objFile In objFSO.GetFolder(strDataFolder).Files
         If blnDebug Then Wscript.Echo Now & " DEBUG: Processing file """ & objFile.Path & """."

         ' Make sure it matches the files we want to copy
         If LCase(Right(objFile.Name, Len(strFileFilter))) = LCase(strFileFilter) Then
            strDestFolder = strDestDir & "\" & objFolder.Name
            ' Create dest folder if needed
            If Not objFSO.FolderExists(strDestFolder) Then
               If blnDebug Then Wscript.Echo Now & " DEBUG: Creating folder """ & strDestFolder & """."
               objFSO.CreateFolder strDestFolder
            End If
            ' Copy file
            If blnDebug Then Wscript.Echo Now & " DEBUG: Copy file """ & objFile.Path & """ to """ & strDestFolder & "\" & objFile.Name & """."
            objFSO.CopyFile objFile.Path, strDestFolder & "\" & objFile.Name, True
            intFiles = intFiles + 1
         End If
      Next

      ' Warn if no matching files in this data folder
      If intFIles = 0 Then
         objLog.WriteLine Now & " WARNING: Data folder """ & strDataFolder & """ does not contain any matching files."
         intWarnings = intWarnings + 1
      End If
   Else
      ' Warn if our data folder doesn't exist in this subfolder
      objLog.WriteLine Now & " WARNING: Data folder """ & strDataFolder & """ does not exist."
      intWarnings = intWarnings + 1
   End If
Next

' Log overall job status
If intWarnings = 0 Then
   objLog.WriteLine Now & " SUCCESS: Folders and files have been properly transferred."
Else
   objLog.WriteLine Now & " WARNING: Warnings found during copy, check log entries."
End If

' Wrap up
objLog.Close

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
I tested both scripts and its works perfectly.
Thank you again!!
LD16Author Commented:
@Bill I just modified your variable

strDestFolder = strDestDir & "\" & objFolder.Name & "\" & strDataDir

At all the DestFolder have this subfolder.

Thank you again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.