0

I have a bat file I want to run to install some files onto another flash drive.

The flash drive I want to install on is always called "Main Drive". Sometimes its on G:\ sometimes D:... etc.

I know the folder I want to install into it's called, "Temp". My path would look like this...

Main Drive\Temp\

Normally I would installing something on here by using the path... G:\Temp\ BUT since I don't know which drive this flash drive will be in, I believe I have to use the Flash drives name.

I looked on here for a similar problem, but wasn't able to come up with a solution for this specific problem.

Here is what I have so far... If I change "Main Drive" to the actually drive it works, however I can't always know which drive it will be on.

echo Installing Folders...
xcopy InstallApp\* "Main Drive"\Temp\ /s /i
pause
goto :EOF

Any thoughts?

user3622460
  • 1,231
  • 5
  • 23
  • 42

2 Answers2

0

The following will result in %_driveFound% pointing to the last drive it finds that has the volume name specified by %_volume%. Note that there can be more than one drive with the same volume name! If that's a problem for you, you may need to detect it and let the user decide, or maybe look for a particular serial number instead? Note the use of robocopy /MIR instead of xcopy.

@setlocal ENABLEEXTENSIONS
@rem @set prompt=$G

@set _volume=Main Drive
@set _drives=Z: Y: X: W: V: U: T: S: R: Q: P: O: N: M: L: K: J: I: H: G: F: E: D: C: B: A:
@set _driveFound=

@for %%G in (%_drives%) do @call :FindVolumeName %%G "%_volume%"
@if defined _driveFound (@goto :_InstallIt)
@echo Volume named %_volume% not found.
@exit /b 0

:FindVolumeName
@set _driveLetter=%1
@set _driveLetter=%_driveLetter:~0,1%
@if not exist %1\ @exit /b 0
@vol %1 | @findstr /i /c:"Volume in drive %_driveLetter% is %~2" > NUL
@if %ERRORLEVEL% equ 0 @set _driveFound=%1
@exit /b 0

:_InstallIt
@set _dest=%_driveFound%\Temp\
@echo Installing to %_dest% ...
@rem Remove the @echo from the following line when you are sure source and dest paths are correct!
@echo @robocopy InstallApp\* %_dest% /MIR
@pause
@exit /b 0
jwdonahue
  • 6,199
  • 2
  • 21
  • 43
0

I would expect something along these lines to work (untested):

@Echo Off
Set "Dest="
For /F "Skip=1" %%A In ('WMIC Volume Where "Label='Main Drive'" Get DriveLetter 2^>Nul'
) Do For %%B In (%%A) Do Set "Dest=%%B\Temp"
If Not Defined Dest Exit /B
Echo Installing Folders...
If Not Exist "%Dest%\" MD "%Dest%"
XCopy "InstallApp\*" "%Dest%" /S /I 2>Nul
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39