Solved

VB Script, copy folders and subfolders

Posted on 2015-02-18
9
106 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
  • 5
  • 4
9 Comments
 
LVL 52

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 52

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
 

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
Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

 
LVL 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now