Reputation: 147
I was wondering if somebody could help. I have written a java program that loads a .cvs on a usb flash drive. This program is likely to be used on several different windows laptops but the program uses a fixed file locataion i.e :F drive.
I was wondering if there was way that when the flash drive is plugged in the Letter assignment is fixed to something like drive :T so that no matter which port or computer the drive is plugged into it can be automatically read by the java program. I wanted to do this in a batch file on the usb so that the user does not have to manually set the computer to change the flash drive. As some of the program users are very old and unlikely to be able to do this even if provided with step by step instructions.
I have never written a batch file before so any help would be great
kind regards
Upvotes: 0
Views: 1912
Reputation: 70951
This batch will search for a drive containing a "key" file or folder, and subst drive #: (yes, this is a valid drive letter, and rare enough to not collide with anything) to the correct drive. Just use drive #: in your program. If batch file has exit in doing its work, errorlevel will be 0, otherwise errorlevel will be 1
@echo off
setlocal enableextensions
rem if our special drive is assigned, release it
if exist #:\ subst #: /d >nul
rem if there are problems, exit
if errorlevel 1 goto endSearch
rem search drives for "key" file/folder
set "keyFile=\data\mySpecialFile.csv"
for %%d in (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) do (
vol %%d: >nul 2>nul && if exist "%%d:%keyFile%" (
rem map our drive
subst #: %%d:\ > nul 2> nul
rem if everything ok end search
if not errorlevel 1 goto endSearch
)
)
:endSearch
rem cleanup and return errorcode as necessary
endlocal && if exist "#:%keyFile%" ( exit /b 0 ) else (exit /b 1)
Upvotes: 0
Reputation: 7105
You can use subst
to do this:
subst T: %~d0\
as long as you know T: isn't in use. Since you're hardcoding your Java program for a location, I don't think that's an issue.
Upvotes: 1