Solved

VB Script, copy folders and subfolders

Posted on 2015-02-18
9
109 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 53

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 53

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your 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 53

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 53

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

18 Experts available now in Live!

Get 1:1 Help Now