0

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

user2965258
  • 137
  • 3
  • 12
  • [this?](http://ss64.org/viewtopic.php?id=410) – npocmaka Nov 07 '13 at 15:00
  • kind of but that looks like it just looks for the first free drive letter to assign the drive to, im wanting to always assign this usb to a fixed drive letter not the next available letter – user2965258 Nov 07 '13 at 15:12

2 Answers2

1

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.

Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
0

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)
MC ND
  • 69,615
  • 8
  • 84
  • 126