How to assign fix drive letter in batch file? (SOLVED)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
newbie
Posts: 24
Joined: 29 Sep 2010 01:30

How to assign fix drive letter in batch file? (SOLVED)

#1 Post by newbie » 16 Oct 2010 00:47

Hello, I would like to know how to assign fix drive letter in batch file? For example, pendrive, it could be e:, f:, g:, etc.So i would like to assign with fix drive letter for the usb drive even its a different a pendrive it still read as the same drive letter, need help???tq
Last edited by newbie on 17 Oct 2010 09:34, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to assign fix drive letter in batch file?

#2 Post by aGerman » 16 Oct 2010 07:17

To different things:
1st) If you have to work on your pen drive you don't need the drive letter. Use relative pathes. Eg.: instead of F:\data\xyz.exe you could use \data\xyz.exe as well.
If you have a batch file on your pen drive, you will find the drive letter in %~d0

2nd) If you work on the local hard drive and you have to determine what drive letter your pen drive got on the current computer, then it is realy complicated.
You could place an emty file on your pen drive (eg. "find.me"). Now you could loop over the drive letters to find it.

Code: Select all

@echo off &setlocal
for %%a in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  if exist "%%a:\find.me" set "DriveLetter=%%a:"
)
echo %DriveLetter%
pause



Regards
aGerman

newbie
Posts: 24
Joined: 29 Sep 2010 01:30

Re: How to assign fix drive letter in batch file?

#3 Post by newbie » 17 Oct 2010 09:33

Thnx a lot, it did solve my prob. :D

Post Reply