Page 1 of 1

Using a batch file to backup data to an external drive help

Posted: 25 Apr 2012 12:30
by Mobjack
Hi everyone. I am hoping someone on this forum can assist me with this issue.
I have a batch file that we use at work to backup computers. Currently, all of the files associated with the batch file are meant to be put on an external drive. When the batch it run, it creates an additional dir on the external drive where it puts all of the files.
My problem is this. We want to be able to run the batch file from the main drive (C:\) and have it backup to the external drive without hard coding the external drive letter. Can anyone explain to me how to make it prompt for the drive where you want the backup folder to be created and the data moved to?

Here is the first part of my backup batch file:

REM Data Transfer Tool Ver 2.0.0

@echo off

mode con cols=140
mode con lines=400

cls

color 0A


echo BACKUP UTILITY
echo.
echo Machine Name: %COMPUTERNAME% User Name: %USERNAME%
echo.
echo Ready to BACKUP laptop data (Press [CTRL] C to Abort)...
echo.
cd\
pause

echo.
echo START TIME: %TIME%
echo.

if exist UserData\RestoreLog.txt erase UserData\RestoreLog.txt
if exist UserData\BackupLog.txt erase UserData\BackupLog.txt


echo Creating a restore point if Windows Scripting Host Security Settings Allows...
echo.
BIN\CreateRestorePoint.vbs

if exist UserData\%USERNAME%. (rd UserData\%USERNAME% /s/q)
mkdir UserData\%USERNAME%


I hope that makes sense. Thanks in advance for any help or suggestions.

Tripp

Re: Using a batch file to backup data to an external drive h

Posted: 25 Apr 2012 13:37
by Fawers
This is untested.

Code: Select all

:prompt
fsutil fsinfo drives
echo,
set /p "driveletter=Drive letter: "
set driveletter=%driveletter:~0,1%:
dir %driveletter% >nul 2>&1^
||echo Drive %driveletter% is not available.&&pause&&exit /b

Another way:

Code: Select all

:prompt
fsutil fsinfo drives
echo,
set /p "driveletter=Drive letter: "
set driveletter=%driveletter:~0,1%:
if not exist %driveletter% (
echo Drive %driveletter% is not available.
pause
exit /b
)

Edit: made some mistakes. Fixed

Re: Using a batch file to backup data to an external drive h

Posted: 25 Apr 2012 13:40
by foxidrive
All references to the utilities will need to be changed too.
It can be done but not without seeing the batch file and knowing what is going on in it.

Is the problem in copying the tools to the external drive, or having to manually launch the backup from the external drive?
You can launch it from c: in a batch file by detecting which drive letter is being used and then using the c: batch file to launch the backup batch file.

Re: Using a batch file to backup data to an external drive h

Posted: 25 Apr 2012 13:52
by Mobjack
@Fawers: Thanks, I will give that a go.

@foxidrive: Currently we run the batch from an external drive and the data is backed up to the drive the batch file is run from. (For example, the batch files reside on the D: drive and that is also where it creates the UserData directory for the files to be put in.) What we want to do it run the batch file from the C: drive and be able to select the drive letter of the external drive via user input. We dont want to assume the external drive will be D: or E: or whatever. I apologize if I'm explaining this crazy. I hear in my mind how I want it to sound, but it doesn't come out that way in my posts. :lol:

Re: Using a batch file to backup data to an external drive h

Posted: 25 Apr 2012 14:57
by foxidrive
Mobjack wrote:@foxidrive: Currently we run the batch from an external drive and the data is backed up to the drive the batch file is run from. (For example, the batch files reside on the D: drive and that is also where it creates the UserData directory for the files to be put in.) What we want to do it run the batch file from the C: drive and be able to select the drive letter of the external drive via user input. We dont want to assume the external drive will be D: or E: or whatever.


You can place a file in the root of the external drive and then detect the drive letter from a batch file.
Place a blank text file in the root of the external drive called backup.flag and run this batch file from c:

Code: Select all

@echo off
for %%a in (d e f g h i j k l m n o p q r s t u v w x y z ) do if exist "%%a:\backup.flag" set drv=%%a
echo the USB drive is on drive %drv%:
pushd "%drv%:\"
call "normal backup USB.bat"
popd