- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- Labs The future of collective knowledge sharing
- About the company

New search experience powered by AI
Stack Overflow is leveraging AI to summarize the most relevant questions and answers from the community, with the option to ask follow-up questions in a conversational format.
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
Batch File Assign Drive Letter
I have found this to work and added the shutdown and restart explorer to reflect drive changes.
I cannot get it tweaked to allow search by drive letter instead of drive description.
My reason for needing to change drive letter is due to the spreadsheets inside the flash media need locked onto a specific locked directory where user error is much lower.
This is a small part of a larger batch file which checks the current drive for correct letter assignment before allowing access to the folders inside.
- drive-letter

- Hi all, please keep discussion in the comments section civil (I've cleaned up the thread thus far). – josliber ♦ May 30, 2018 at 1:33
- Josliber - Understood And Thank You My Apologies! – TruckDriverOnPC May 30, 2018 at 2:48
- Compo - I Will Get The Code Added In Which I Have Had The Most Success So Far. I Appreciate The Comment And Also I Seen You Fixed My Code Pasted In Earlier Thank You For That As I Said I Am New And Just Got Enough Understanding To Paste Code Correctly. – TruckDriverOnPC May 30, 2018 at 2:50
- 4 @TruckDriverOnPC - it's not correct English writing style to capitalize the first letter of every word. This makes what you have written more difficult to read. – Bill_Stewart May 30, 2018 at 4:12
- Can you please explain why there's a need to assign a specific drive letter to the inserted USB? I cannot see the need for forcibly assigning it to W: as opposed to it being auto assigned, for instance, to F: . The script should easily be able to set whichever drive letter is assigned to the known label without having to do all of this. Please note, I'm not saying that your step is wrong, it just seems a lot of work for something which may not actually be needed. – Compo May 30, 2018 at 10:39
The thing is that diskpart will always need admin rights to run, so it's better to avoid it if possible. Moreover if you leave the script on the flash drive you can't change the drive letter while running it anyway, because there will always an open handle on the drive so it can't be unmounted. If you force unmounting then the script will also stop running
Another problem is that you don't know if the user entered the correct drive letter. What if they entered a hard drive or another flash drive's letter?
It's better just to leave the drive letter as-is and mount the drive to another drive instead . You can use subst in a batch file instead (which can run by any users) and mount the drive that contains that batch file
if "%NewLetter:~1%"=="" will check if the user entered only letter instead of a longer string, then that drive letter will be checked if being used
If you put the batch file in the root of the flash drive you can simply use subst %NewLetter% . instead of %~d0
- The Only Letter To Be Assigned To This Drive Is A High Letter In This Case W:\ To Prevent User Error If They Have Many Flash Drives Etc On Their PC. I Haven't Really Considered Mounting It That Way. This Drive Will Contain Many Spreadsheets Which Call Upon Each Other During Use. That's Why I Wanted To Assign A Drive Letter. Hope That Can Make Some Sense. Also To Access The Files The Folders Are Hidden In A Maze Where The Only Sane Way To Access Them Is Run A Batch File That Will Verify The Drive Letter Of W:\ To Open Folder Or Bring Up An Error Screen. – TruckDriverOnPC May 30, 2018 at 3:02
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .
Not the answer you're looking for? Browse other questions tagged batch-file assign drive drive-letter or ask your own question .
- The Overflow Blog
- Behind the scenes with OverflowAI Search
- Featured on Meta
- If more users could vote, would they engage more? Testing 1 reputation voting...
- Alpha test for short survey in banner ad slots starting on week of September...
- Temporary policy: Generative AI (e.g., ChatGPT) is banned
- Expanding Discussions: Let's talk about flag reasons
- Collectives Updates to the Community Bulletin in the Right Sidebar
- OverflowAI Search is now available for alpha testing (September 13, 2023)

Hot Network Questions
- How would the transaction chains created Ark pool transactions work given current mempool policy?
- Where is this image of Vader from?
- How can I properly translate possessive form of nouns?
- Why we use vector sum to calculate net potential in AC circuits?
- How to set up a virtual payment card on a phone that a child can use abroad (Poland) to withdraw cash and pay in shops?
- Reconstruct an array by successive insertions
- Daisy chaining APs or connect them into the central router?
- why use template in year_month class?
- Is there a pyramid with all four faces being right triangles?
- How is the Cosmic Microwave Background so big?
- "Premove" in OTB game
- Do you win a Numeric Mahjong?
- kula devi/devata of Shree Rama?
- Should I use a 15 amp or 20 amp breaker for a mini split with max current at 13.0 Amps?
- Civil liability for criminal punishment
- What's the most recent version of AmigaOS?
- 9 year old ATA red belt got beat at tournament
- How can I open these "holes" on the bottom/side of an outdoor GFCI enclosure?
- Is LaTeX an implicit prerequisite in Undergraduate Mathematics?
- general principles of piercing the protective cover of unfrozen microwave ready meals
- What is the force of gesehen haben muss : You [one has] gotta see, You ought to have seen, or you must already have seen?
- Can a verb be made pronominal and keep the meaning of its non-pronominal form as in "Se hacen la compra los unos a los otros"?
- Using Dual Wielding, you can do two weapon fighting with a two handed weapon held in one hand.And can the other hand be a weapon that you just drawup?
- Lawn: Newly sown turf grows at least twice as fast as the "old" turf
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Change a drive letter from Command Prompt or batch script in Windows 8?
I have an external hard drive that I connect often to my computer. At some point it was assigned letter G: and based on that my other software works.
Nowadays, suddenly it gets letter F:
Is there any way I can use a batch script to change the letter from F: to G:. I don't mind running the batch script manually?
If so, what is the code I should use and will it need administrative permissions?
- command-line
- 1 See diskpart and yes you need admin. In theory it should have remembered the drive letter, consider changing it manually on disk management first. – Bob Jul 28, 2013 at 18:31
- @Bob: It could be that it only remembers drive letters after they have been assigned manually at least once. – u1686_grawity Jul 28, 2013 at 19:57
- 1 Thanks! I have changed it a couple of times manually, but everytime it goes back to the old one. – gontadu Aug 6, 2013 at 14:42
- I have noticed that it was remembered when I had one device using a drive letter. When I started using multiple devices with the same drive letter, they get forgot, I have not verified the hypothesis, but I guess Windows 10 remembers the last device assigned to a drive letter. – BtF Aug 2, 2021 at 8:56
There are several methods:
mountvol – use it once to delete an existing letter, then again to assign a new one;
diskpart – interactive;
diskmgmt.msc aka Disk Management – a graphical tool.
Whichever you choose, assigning once should be enough, the assignment will be remembered afterwards.
You must log in to answer this question.
Not the answer you're looking for browse other questions tagged windows windows-8 command-line batch ..
- The Overflow Blog
- Behind the scenes with OverflowAI Search
- Featured on Meta
- If more users could vote, would they engage more? Testing 1 reputation voting...
- Alpha test for short survey in banner ad slots starting on week of September...
Hot Network Questions
- Normalization of the harmonic oscillator propagator
- Is it undefined behaviour to use pointer after allocated memory?
- What should I do if I am strongly burned out at work, but resigning now would cause problems for my coworkers and burn bridges?
- What's the most recent version of AmigaOS?
- To a Bayesian, does a trick coin with two heads have 50% chance of flipping heads if they don't know that it has two heads?
- Is a factory-made wheel being 3.5 mm out of dish a problem?
- Custom Element Type Documentation not working or confusing
- Am I Part of a Failing Group?
- Is LaTeX an implicit prerequisite in Undergraduate Mathematics?
- Is it illegal to have a product delivered to a different ZIP code to pay less sales tax?
- During a total solar eclipse is it possible to see solar flares with the naked eye?
- What attracted Abimelech to an old woman in her 80s or 90s?
- An atmosphere that gets denser with increasing altitude, to support these flying creatures
- In countries using Single Transferable Vote, how are voting results presented?
- What is this unusual syntax for the Commodore SYS command?
- Delete list elements above a certain threshold
- Does the term "vapor pressure" even mean anything in an open system?
- Can “hydrates” of crystals form with other molecules?
- How to set up a virtual payment card on a phone that a child can use abroad (Poland) to withdraw cash and pay in shops?
- How can Ural Airlines fly its A320 from unpaved field?
- Is it possible to redefine the limit operator to print Lim, not lim?
- Iterating the octree while keeping track of the positive neighboring nodes
- Declaration of conformity to EMC Directive
- Shell script to validate fastq issue
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

IMAGES
VIDEO
COMMENTS
1 My goal is to create a batch file that will extract the contents of a zip file to a separate directory. Every file related to this question is on the same flash drive. The following command works just fine with the drive letter i:\ **i:\7z x i:\zips ew_app.zip -oi:\apps ew_app**
:Change_Letter cls @echo ON setlocal echo Type The Name Of The Drive NOT The Letter! set /p Label= set Drive=W set Confirm=1 set Volume= set VolumesFile=%Temp%\Volumes.txt set DiskPartFile=%Temp%\DiskPart.txt echo Retrieving volume information ... echo list volume | diskpart.exe | more +5 | find /v "DISKPART>" >"%VolumesFile%" for /f "tokens=2" ...
volume 0 remove drive letter volume 1 remove drive letter // this way they are both blank so I don't have conflicting drive letters when I change one without the other volume 0 set drive letter="Y" volume 1 set drive letter="X" wait 5 minutes // software will see volume 1 as X drive volume 0 remove drive letter volume 1 remove drive letter volum...
Ask Question Asked 10 years, 1 month ago Modified 8 years, 1 month ago Viewed 8k times 3 I have an external hard drive that I connect often to my computer. At some point it was assigned letter G: and based on that my other software works. Nowadays, suddenly it gets letter F: