Running a batch file from a USB drive when changing a drive letter

So, I made a batch script package, and it runs several portable programs (e.g. prog1.exe, prog2.exe, etc.). The problem is that whenever I connect the USB drive to another computer, the drive letters change, which gives me errors when I run my .bat file. Please help me find a solution. Thanks.

+4
source share
3 answers

%~d0gives the current drive letter (including a colon). If the batch file is contained on a USB drive, you can use it.

So for example, instead

E:\PortablePrograms\ProgramName.exe

you write

%~d0\PortablePrograms\ProgramName.exe

... or you could do something like this

::change directory to the script directory drive
pushd %~d0
::navigate from the drive to the relevant path(s)
cd PortablePrograms
::execute any programs
ProgramName.exe
SecondProgramName.exe
::just because I like to pair my pushes with pops; not required
popd
+13
source

Here's how I can get the latest removable drive on the list.

    @echo off

    :: Drivetypes
    ::  0=Unknown
    ::  1=No Root Directory
    ::  2=Removable(USB,Firewire)
    ::  3=Local Disk (Internal Hard Drive)
    ::  4=Network Drive(\\Server\share\)
    ::  5=Compact Disk (CD DVD)
    ::  6=Ram Disk
    for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=2" 
    get name /format:value') do set driveletter= %%d
    echo %driveletter%
    pause
+1

you can use the command-line argument %1, %2as the input path and change the file bat.

0
source

All Articles