Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Transfer data from external hard drive to another drive using variable drive path

Posted on 2014-02-19
12
Medium Priority
?
433 Views
Last Modified: 2014-02-20
Hi All,

I need to retreive data from a hard drive from computers that failed. My plan is to plug the hard drive as a external drive and retrieve the data to another external or local disk C: . Is there a way I would like to have the batch file ask for the drive letter where the data is stored before running and backup to current drive?

Please advice.

Thanks,
Vicki
0
Comment
Question by:Vicki05
[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
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 39871353
Try something like this; set Source and Target to the required paths. The script is currently in test mode and will only display the robocopy command it would normally run. Remove the uppercase ECHO in line 15 to run it for real.
@echo off
setlocal
:Loop
set Drive=
set /p Drive=Please enter the drive letter for the backup: 
if "%Drive%"=="" goto :eof
if not "%Drive:~-1%"==":" set Drive=%Drive%:
dir Drive >NUL 2>&1
if errorlevel 1 (
	echo Drive %Drive% not found.
	goto :Loop
)
set Source=C:\
set Target=%Drive%\
ECHO robocopy.exe %Source% %Target% /e /r:0 /w:0

Open in new window

0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39871358
In a BAT file you can just use the SET /P command to prompt for the drive letter and assign it to a variable.  Something like this:

@echo off
set Drive=
set /p "Drive=Please enter drive letter:"
if "%Drive%" NEQ "" (
  REM Place whatever copy commands you need here...
  xcopy "%Drive%:\*.*" Z:\
)

Open in new window

~bp
0
 
LVL 85

Expert Comment

by:oBdA
ID: 39871363
Correction, sorry; missed some percent signs in line 8:
@echo off
setlocal
:Loop
set Drive=
set /p Drive=Please enter the drive letter for the backup: 
if "%Drive%"=="" goto :eof
if not "%Drive:~-1%"==":" set Drive=%Drive%:
dir Drive >NUL 2>&1
if errorlevel 1 (
	echo Drive %Drive% not found.
	goto :Loop
)
set Source=C:\
set Target=%Drive%\
ECHO robocopy.exe %Source% %Target% /e /r:0 /w:0

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 39871979
You could always give a little bit of a helping hand to choosing the drive too with a command at the top of your script to show some info, .e.g

@echo off
WMIC logicaldisk get caption, volumename, description, size, freespace
Set Drive=
set /p Drive =  etc.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39871986
Or even:

@echo off
echo Set objShell = CreateObject^( "Shell.Application" ^) > "%temp%\getpath.vbs"
echo set objFolder = objShell.BrowseForFolder^( 0, "Select a drive", ^&H10^&, ""^)>> "%temp%\getpath.vbs"
echo wscript.echo objFolder.Self.Path >> "%temp%\getpath.vbs"

for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getpath.vbs"') do set folder=%%a
echo Now do copy from %folder:~0,2%
pause
0
 

Author Comment

by:Vicki05
ID: 39873755
Hi oBdA,

I am getting drive letter not found.

Hi Bill,
I am able to continue with the method you have suggested however, I need to be able to enter the source drive and be able to backup to current drive which can be a external drive as well.

Is that a possibility?
 Something like
xcopy /q/y "%Drive%:\Test\Backup\*.*"  %Current_Drive%:\
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39874159
For the current drive you can just not specify a drive, i.e. \path\file is for the current drive or you can get the current directory using %cd%.  From that %cd~0,2% should be the drive letter.

Steve
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39874327
Hi Bill,
I am able to continue with the method you have suggested however, I need to be able to enter the source drive and be able to backup to current drive which can be a external drive as well
.
My solution certainly allowed that, just adjust the destination of whatever copy command you use to specify the destination relative to whatever will be the current directory.

~bp
0
 
LVL 85

Expert Comment

by:oBdA
ID: 39874669
Did you see my second comment? The first version was missing the percent sign around "Drive" in line 6.
If the script is stored on the external drive, you can just use %~dp0 to access the drive.
@echo off
setlocal
:Loop
set Drive=
set /p Drive=Please enter the drive letter to backup: 
if "%Drive%"=="" goto :eof
if not "%Drive:~-1%"==":" set Drive=%Drive%:
dir Drive >NUL 2>&1
if errorlevel 1 (
	echo Drive %Drive% not found.
	goto :Loop
)
set Source=%Drive%\
set Target=%~dp0\
ECHO robocopy.exe %Source% %Target% /e /r:0 /w:0

Open in new window

0
 

Author Closing Comment

by:Vicki05
ID: 39875249
Thanks for your help.
0
 

Author Comment

by:Vicki05
ID: 39875258
Hi oBdA,

When I run the batch file, I get a error when I enter the drive letter. Please try to run it yourself.



@echo off
setlocal
:Loop
set Drive=
set /p Drive=Please enter the drive letter to backup:
if "%Drive%"=="" goto :eof
if not "%Drive:~-1%"==":" set Drive=%Drive%:
dir Drive >NUL 2>&1
if errorlevel 1 (
      echo Drive %Drive% not found.
      goto :Loop
)
set Source=%Drive%\
set Target=%~dp0\
ECHO robocopy.exe %Source% %Target% /e /r:0 /w:0
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39875272
Line 8 had % % missing too.  %Drive% instead of Drive

Steve
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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