if, for loop

April 10, 2010 at 11:22:03
Specs: Windows 2008 or XP, i7 16GB
Hi all,
I could use some help on a project. I’m backing up a server, with a batch file I wrote. The program backs up to an external USB hard drive. Each hard drive is named Backup Vol-1, Backup Vol-2 etc. The problem is the USB drive migrates. Sometime it’s E drive, while others it’s F,G or H drive. So I want the program to Identify the drive, then backup to that location. The following works OK but I want to condense it, but I don’t know how to make a if, for, loop.

Any help would be appreciated.
Thanks, GregFoX
@echo off

vol f:>C:\ServerBU\testfile.txt
set /p vol_drive=<"C:\ServerBU\testfile.txt"
set var=%vol_drive:~22,6%
del /q C:\ServerBU\testfile.txt
if "%var%"=="BACKUP" goto Valid_Drive
cls

vol e:>C:\ServerBU\testfile.txt
set /p vol_drive=<"C:\ServerBU\testfile.txt"
set var=%vol_drive:~22,6%
del /q C:\ServerBU\testfile.txt
if "%var%"=="BACKUP" goto Valid_Drive
cls

vol d:>C:\ServerBU\testfile.txt
set /p vol_drive=<"C:\ServerBU\testfile.txt"
set var=%vol_drive:~22,6%
del /q C:\ServerBU\testfile.txt
if "%var%"=="BACKUP" goto Valid_Drive
cls

vol c:>C:\ServerBU\testfile.txt
set /p vol_drive=<"C:\ServerBU\testfile.txt"
set var=%vol_drive:~22,6%
del /q C:\ServerBU\testfile.txt
if "%var%"=="BACKUP" goto Valid_Drive
cls

:Valid_Drive
echo var is now %var%

set BackUpDrive=%vol_drive:~17,1%

echo the valid backup drive is now %BackUpDrive% Drive
pause


See More: if, for loop

Report •


#1
April 10, 2010 at 12:01:29
@echo off & setLocal EnableDELAYedeXpansion & set L=
for %%a in (f g h) do (
vol %%a: 2> nul | find "Backup Vol" > nul
if not errorlevel 1 set L=%%a)
if defined L (echo drive letter is !L!) else (echo not found)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
April 11, 2010 at 16:52:13
Thank you, I'll give it a try.
G.

Report •

#3
April 12, 2010 at 15:04:32
Thanks it worked great and saved many lines of code.
Thanks again.
G.

Report •
Related Solutions


Ask Question