We help IT Professionals succeed at work.

VB Script: move-copy Folders and Subfolders

Luis Diaz
Luis Diaz asked
on
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”
Comment
Watch Question

Top Expert 2014

Commented:
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.
Luis DiazIT consultant

Author

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 support
Commented:
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

Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
(haven't had chance to look at this one yet...)

~bp
Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
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
Luis DiazIT consultant

Author

Commented:
I tested both scripts and its works perfectly.
Thank you again!!
Luis DiazIT consultant

Author

Commented:
@Bill I just modified your variable

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

At all the DestFolder have this subfolder.

Thank you again!