This drive in batch files

  • Thread starter Thread starter Howard Brazee
  • Start date Start date
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
 
|>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
 
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
 
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.
 
|>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?
 
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
 
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!
 
Back
Top