Solved

VB Script, copy folders and subfolders

Posted on 2015-02-18
9
115 Views
Last Modified: 2015-02-19
Hello,

I need a Vb Script to copy all the folders and subfolders from C:\ and D:\ into a new folder with the current date in my external hard drive (E:\).
0
Comment
Question by:LD16
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 40616349
I would strongly recommend doing this in a BAT script rather than VBS, allowing you to use either XCOPY or ROBOCOPY to do the heavy lifting of the copy operation.  VBS has the ability to copy folders, but you won't have any visibility to what files are copied, etc.

Also, if you are copying all of C:\ then you will need to run with admin privs, and even then you will likely hit files that are not able to be copied because they are special system files, or open, etc.  Is that going to be all right?  If you really want a full backup of the system then perhaps a true backup solution / utility would be better suited.

Happy to fashion either BAT or VBS approach, but wanted to fully understand the need, etc.

~bp
0
 

Author Comment

by:LD16
ID: 40616396
Hello Bill,

Thank you for your comment.
If a bat file is the most convenient solution, I am in, as soon as I can easily modify the config and add log file.

Concerning the admin permissions, we can add On error Resume Next and additionnally a log file to list folders which haven't been copy due to permissions.

Regards,
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 40616556
Okay, give this BAT script a test and see how it goes.

@echo off
setlocal EnableDelayedExpansion

REM Build a date stamp for backup folder in YYYYMMDD
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
set Stamp=%Stamp: =0%

REM Define possible backup drives, and the root for backup folders
set FromDrives=C,D
set DestDir=E:\

REM define robocopy options
set Options=/XJ /E /R:1 /W:1 /NP

REM Create the directory for todays backups
if not exist "%DestDir%%Stamp%\" md "%DestDir%%Stamp%\"

REM Do the backups
for %%A in (%FromDrives%) do (
  robocopy %%~A:\ %DestDir%%Stamp%\%%~A\ %Options%  /LOG:"%DestDir%%Stamp%\CopyLog-%%~A.txt"
/LOG:"%DestDir%%Stamp%\CopyLog-%%~A.txt"
)

Open in new window

~bp
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:LD16
ID: 40616834
Hello Bill,

I test the bat.file but it takes a lot of time to copy all the folders and subfolders, so I want to change the following variable:

set FromDrives=C,D

Is there a way to put and specific path to copy folders, sub folders and files in this variable such as :

set FromDrives=C:\Users\LD16\Documents, C:\Program Files

Thank you in advance.
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 40617247
Give this a try (adjusting your folders as needed) and see how it goes.

@echo off
setlocal EnableDelayedExpansion

REM Build a date stamp for backup folder in YYYYMMDD
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
set Stamp=%Stamp: =0%

REM Define possible backup drives, and the root for backup folders
set FromDirs="B:\EE\EE28619127\C","B:\EE\EE28619127\D"
set DestDir=E:\

REM define robocopy options
set Options=/XJ /E /R:1 /W:1 /NP

REM Create the directory for todays backups
if not exist "%DestDir%%Stamp%\" md "%DestDir%%Stamp%\"

REM Do the backups
for %%A in (%FromDirs%) do (
  robocopy "%%~A" "%DestDir%%Stamp%%%~pnA" %Options%  /LOG+:"%DestDir%%Stamp%\CopyLog.txt"
)

Open in new window

~bp
0
 

Author Comment

by:LD16
ID: 40618452
Hello Bill,

I tried your last bat file and it works.
However I have something strange.

In my FromDirs I use
set FromDirs="C:\Users\LD16\Documents\1.DATA","C:\Users\LD16\Desktop\Scripts-revised"

and I've Checked the output and I have

"E:\20150219\LD16\SESA249410\Documents\1" instead of having "C:\Users\LD16\Documents\1.DATA". Do you know why?

for the second folder I have the correct structure:"C:\Users\LD16\Desktop\Scripts-revised"
0
 

Author Comment

by:LD16
ID: 40618469
Hello Bill,

To improve the script is there a way to add a confirm message at the begining of your bat file

"Are you sure that you want to backup the following folders " & " "  &  FromDirs & "into" & DestDi

if vbNo exit sub otherwise launch the script

And at the end of the script

MsgBox "Backup has been finished".


Thank you for your help.
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40618662
This should handle the folders with extensions.

This will also prompt to continue, but keep in mind that this is a BAT script, so there is no pretty msgbox capability.  Depending on how you run it could yield better or worse results on the prompting.

@echo off
setlocal EnableDelayedExpansion

REM Build a date stamp for backup folder in YYYYMMDD
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%
set Stamp=%Stamp: =0%

REM Define possible backup drives, and the root for backup folders
set FromDirs="B:\EE\EE28619127\C","B:\EE\EE28619127\D"
set DestDir=E:\

REM Confirm to proceed
echo Backing up from folders: %FromDirs%
echo Backing up to folder: %DestDir%%Stamp%
set Sure=N
set /P "Sure=Are you sure (Y/N): "
if %Sure% NEQ "Y" exit /B

REM define robocopy options
set Options=/XJ /E /R:1 /W:1 /NP

REM Create the directory for todays backups
if not exist "%DestDir%%Stamp%\" md "%DestDir%%Stamp%\"

REM Do the backups
for %%A in (%FromDirs%) do (
  robocopy "%%~A" "%DestDir%%Stamp%%%~pnxA" %Options%  /LOG+:"%DestDir%%Stamp%\CopyLog.txt"
)

Open in new window

~bp
0
 

Author Comment

by:LD16
ID: 40618819
Great.

Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More or less everybody in the IT market understands the basics of Networking, however when we start talking about Storage Networks, things get a bit dizzier, and this is where I would like to help.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question