Solved

VB Script, copy folders and subfolders

Posted on 2015-02-18
9
119 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 56

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 56

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 56

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 56

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

this article is a guided solution for most of the common server issues in server hardware tasks we are facing in our routine job works. the topics in the following article covered are, 1) dell hardware raidlevel (Perc) 2) adding HDD 3) how t…
In this article you will get to know about pros and cons of storage drives HDD, SSD and SSHD.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

623 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