Solved

VB Script, copy folders and subfolders

Posted on 2015-02-18
9
105 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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Great.

Thank you!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
In this article we have discussed the manual scenarios to recover data from Windows 10 through some backup and recovery tools which are offered by it.
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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