Batch to choose drive letter

3

I'm creating a menu for deploy images via winpe . Within this one I squeeze some commands and apply them to files that are stored on an external hard drive.

I even managed to make a if exist c:\IMG GOTO and then proceed with the script , but as the lyrics can vary a lot, script gets immense.

Is there any way to assign a value to a variable through a condition, something like:

If exist C:\IMG então VAR=C

And hence apply the commands as VAR:\IMG\image.wim

I would like the variable in the case to match the drive letter that contains the IMG folder.

    
asked by anonymous 31.01.2015 / 04:40

2 answers

2

Use the SET command to define a variable and with EXIST you check if a file or folder exists.

SET "LETRA="                 :: Declara a variável que vai receber a letra do drive

if exist C:\ (               :: Se existir
 SET LETRA=C:\               :: Coloca a letra do drive na variável LETRA
) else (                     :: Se não encontrar
 echo Arquivo nao encontrado
 PAUSE
 EXIT /B
)
echo %LETRA%IMG\image.wim    :: Exibe o caminho da imagem se existir

If you have a need to often check if a file / directory exists, automating this process in a function can save you a few lines of code:

SET "LETRA="                 :: Declara a variável que vai receber a letra do drive
call :ElegerDrive c:\        :: Chama "ElegerDrive" com um argumento, no caso c:\

:ElegerDrive
IF EXIST %~1 (               :: Se o arquivo ou pasta existir
   SET LETRA=%~1             :: Atribui a variável LETRA o argumento
   GOTO :FIM                
) ELSE (
   echo O drive %~1 nao foi encontrado.
   PAUSE
   EXIT /B
)
:Fim
echo %LETRA%IMG\image.wim
PAUSE
    
31.01.2015 / 12:16
0

Can it be simpler?

:: If exist C:\IMG então set VAR=C

@echo off 

(

for /l %%L in (1 1 5) do for /f "tokens=3 delims= " %%i in ('"wmic LogicalDisk where drivetype=%%L get DeviceID,Description "') do if exist "%%i11\*.*" set "_path_IMG=%%i:11%%i" && goto :_continue_:

) 2>nul >nul 

echo/ Drive + \IMG\ N encontrada^^!


goto :_eOf

:_continue_:

echo/ Drive + \IMG\ =  %_path_IMG%

:_eOf
    
18.11.2018 / 22:13