This drive in batch files

H

Howard Brazee

I run a batch file that is in a memory card (copying some documents).

I'd like that batch file know the drive that it was run from.

For instance, I want to replace J: below with a variable like
%USERPROFILE% so when I run J:\HOME.BAT or if I run H:\HOME.BAT it
will work the same.

xcopy "%USERPROFILE%\My Documents\My Pictures\Home\*.*" j:\Home\*.*
/Y/S
 
P

Pennywise

|>I run a batch file that is in a memory card (copying some documents).
|>
|>I'd like that batch file know the drive that it was run from.
|>
|>For instance, I want to replace J: below with a variable like
|>%USERPROFILE% so when I run J:\HOME.BAT or if I run H:\HOME.BAT it
|>will work the same.
|>
|>xcopy "%USERPROFILE%\My Documents\My Pictures\Home\*.*" j:\Home\*.*
|>/Y/S

Do you always copy to X:\Home\ ?

If so just run xcopy from that directory, by default it copies to the
directory it's run from, if none is mention'd
 
P

Pegasus \(MVP\)

Howard Brazee said:
I run a batch file that is in a memory card (copying some documents).

I'd like that batch file know the drive that it was run from.

For instance, I want to replace J: below with a variable like
%USERPROFILE% so when I run J:\HOME.BAT or if I run H:\HOME.BAT it
will work the same.

xcopy "%USERPROFILE%\My Documents\My Pictures\Home\*.*" j:\Home\*.*
/Y/S

You simply omit the destination drive letter:
xcopy "%USERPROFILE%\My Documents\My Pictures\Home\*.*" \Home\*.*
/Y/S
 
H

Howard Brazee

You simply omit the destination drive letter:
xcopy "%USERPROFILE%\My Documents\My Pictures\Home\*.*" \Home\*.*
/Y/S

That will work only if I haven't changed drives in my batch
processing. But this batch program calls another one which does some
clean-up changing drives.

If I could save my drive letter of the batch file, I could either go
back to it, or include it in a command.
 
H

Howard Brazee

|>For instance, I want to replace J: below with a variable like
|>%USERPROFILE% so when I run J:\HOME.BAT or if I run H:\HOME.BAT it
|>will work the same.
|>
|>xcopy "%USERPROFILE%\My Documents\My Pictures\Home\*.*" j:\Home\*.*
|>/Y/S

Do you always copy to X:\Home\ ?
Yes

If so just run xcopy from that directory, by default it copies to the
directory it's run from, if none is mention'd

OK, I start a complex batch file, which calls another batch file that
does cleanup. So I need to make sure I'm back before I run the
XCOPY. How do I determine where "back" is?
 
P

Pegasus \(MVP\)

Howard Brazee said:
That will work only if I haven't changed drives in my batch
processing. But this batch program calls another one which does some
clean-up changing drives.

If I could save my drive letter of the batch file, I could either go
back to it, or include it in a command.

Your original post was rather vague on this point. Saving
the drive letter from where the batch file was launched is
no problem, if this is what you require:

@echo off
@echo off
echo The current drive letter is %cd:~0,1% (without colon)
echo The current drive letter is %cd:~0,2% (with colon)
echo The current drive letter is %cd:~0,3% (with colon and slash)
echo The name of the batch file is %0
 
H

Howard Brazee

Your original post was rather vague on this point. Saving
the drive letter from where the batch file was launched is
no problem, if this is what you require:

@echo off
@echo off
echo The current drive letter is %cd:~0,1% (without colon)
echo The current drive letter is %cd:~0,2% (with colon)
echo The current drive letter is %cd:~0,3% (with colon and slash)
echo The name of the batch file is %0

Thanks. A quick test.bat of:
@echo off
echo The current drive letter is %cd:~0,1% (without colon)
echo The current drive letter is %cd:~0,2% (with colon)
echo The current drive letter is %cd:~0,3% (with colon and slash)
echo The name of the batch file is %0
set x=%cd:~0,3%
echo %x%
pause

gave me:
The current drive letter is C (without colon)
The current drive letter is C: (with colon)
The current drive letter is C:\ (with colon and slash)
The name of the batch file is "C:\BELFRY\TEST.BAT"
C:\
Press any key to continue . . .

Perfect!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Synchronizing files 18
Need help on Bat file 9
What is wrong with this BAT File? 3
Batch File 6
Batch file 4
batch file not running 5
XCOPY question 1
Backup Batch File 7

Top