# [SOLVED] MS DOS help, drive letter substitute or other solution to avoid repeating



## DeviaAnimus (Oct 27, 2010)

First of all, I'm new to this so I don't know very much about MS DOS.

So... I recently made a batch file that copies a folder from a usb device to my computer, it uses if exist to determine what drive letter the usb device it lies on is assigned and it's doing that by repeating some lines for each letter.

I'm wondering if I can use something instead of the drive letter so that it won't have to try every letter or if there is another way to avoid the repeating.

Here's some of the code:

```
:L

if exist L:\Batchfile.bat goto Lyes

if not exist L:\Batchfile.bat goto M

:Lyes

if exist C:\Users\Name\Desktop xcopy L:\Source C:\Users\Name\Desktop\Destination /i /h /y

if exist C:\Users\Name\Desktop\Destination goto startmsg

if exist D:\Tempdata xcopy L:\Source D:\Tempdata\Destination /i /h /y

if exist D:\Tempdata\Destination goto startmsg

goto end

:M

if exist M:\Batchfile.bat goto Myes

if not exist M:\Batchfile.bat goto N

:Myes

if exist C:\Users\Name\Desktop xcopy M:\Source C:\Users\Name\Desktop\Destination /i /h /y

if exist C:\Users\Name\Desktop\Destination goto startmsg

...
```

And this repeats for all letters...


----------



## TheOutcaste (Mar 19, 2009)

*Re: MS DOS help, drive letter substitute or other solution to avoid repeating*

Welcome to TSF!

If you are running the batch file by double clicking it, the batch file already knows what drive letter it is on. The *d*rive and *p*ath to the batch file is stored in the *%0* parameter. use *%~dp0* to access it.
If the user name may contain a space, you need to surround the path with double quotes.
If you add a trailing slash to the Destination folder, you avoid having Xcopy ask if the destination is a file or folder.

```
If Exist "C:\Users\Name\Desktop" Xcopy /IHY %~dp0Source "C:\Users\Name\Desktop\Destination\"
If Exist "C:\Users\Name\Desktop\Destination" Goto startmsg
If Exist D:\Tempdata Xcopy /IHY %~dp0Source D:\Tempdata\Destination\
If Exist D:\Tempdata\Destination Goto startmsg
Goto end
```
You can replace *C:\Users\<name>* with *%Userprofile%* and it will save to the currently logged on user, so it will work with any user account.

```
If Exist "%Userprofile%\Desktop" Xcopy /IHY %~dp0Source "%Userprofile%\Desktop\Destination\"
If Exist "%Userprofile%\Desktop\Destination" Goto startmsg
If Exist D:\Tempdata Xcopy /IHY %~dp0Source D:\Tempdata\Destination\
If Exist D:\Tempdata\Destination Goto startmsg
Goto end
```
See *Call /?* for a list of all the parameter modifiers


----------



## DeviaAnimus (Oct 27, 2010)

*Re: MS DOS help, drive letter substitute or other solution to avoid repeating*

Thanks that worked out great and thanks for the other tips as well.


----------



## TheOutcaste (Mar 19, 2009)

*Re: MS DOS help, drive letter substitute or other solution to avoid repeating*

You're Welcome!

I'll mark this Solved then. For future reference, you can mark your own threads *Solved* by using the *Thread Tools* at the Top Right of this thread (above the first post)

Jerry.


----------



## DeviaAnimus (Oct 27, 2010)

ok, I'll do that.


----------

