So, I have made a batch script and it executes multiple portable programs (e.g., prog1.exe, prog2.exe, etc). The problem is whenever I connect the USB drive to another computer, the drive letters change, giving me errors when running my .bat file. Please help me find a solution. Thank you.
bat file to your flash drive, without putting it in a folder. You use Notepad to write the autorun. ini file as directed, and then save that to the flash drive. Then, insert it into your computer.
Use the /D switch to change current drive in addition to changing current directory for a drive. Show activity on this post. If you need to go from a device to another (in your case from C:\ to F:\ , you need to type F: before/after you entered your cd command, so it will go on the F device.
%~d0
gives you the current drive letter (including the colon). If the batch file's contained on the USB drive, you can use that.
So, for instance, instead of
E:\PortablePrograms\ProgramName.exe
you would write
%~d0\PortablePrograms\ProgramName.exe
... or you could do something like this
::change directory to the script's directory's 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
This is how I get the last removable drive in a listing.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With