G
Gene Wirchenko
Dear Win7ers:
I have a problem with a batch file that I am developing. What I
want it to do is print out the drive that the batch file is on. I am
not getting that.
I have a copy in c:\tmp with that the current directory and
another copy in g:\tmp on my XP system. (I have also tried this on my
Win7 system where the drives are c: and i:.) The copies are
identical; I have made very sure of that.
If I give the command
getdrive
it correctly prints the current drive.
If I give the command
c:getdrive
with c: the current drive, it does not pick up the drive letter and so
defaults to the current drive. This means that I get the correct
answer by accident.
If I give the command
g:getdrive
(or i:getdrive) with c: the current drive, it does not pick up the
drive letter and so defaults to the current drive. This means that I
get the wrong answer.
I know exactly where the error occurs, but I have no idea why it
happens. I get the same result in both XP and Win7. When a drive
letter is specified, execution gets into the else. Output is:
@ else
namecopy=_____ (where the underscores are the name of
the batch file (%0) as specified in the command line)
assigning _ (where underscore is the first letter of %0)
Drive is apparently .
Note that the drive letter is not there!
In previous tries, I even had output of
assigning g
Drive is apparently C.
from
g:getdrive
This might have been an artefact from before I added setlocal.
Here is the batch file.
***** Start of Batch File *****
@echo off
setlocal
echo GetDrive Version 1.0.0 of 2012-10-31 by Gene Wirchenko
set namecopy=%0
echo namecopy=%namecopy%
if not "%namecopy:~1,1%"==":" (
set driveletter=%cd:~0,1%
echo set @ if
) else (
echo @ else
echo namecopy=%namecopy%
echo assigning %namecopy:~0,1%
set drive=%namecopy:~0,1%
echo Drive is apparently %drive%.
set driveletter=%cd:~0,1%
if /i "%drive%" geq "A" if /i "%drive%" leq "Z" set
driveletter=%drive%
)
echo Computed drive letter is %driveletter%.
endlocal
***** End of Batch File *****
Note that the "if /i ..." line may wrap to two lines on your
display; it does on mine.
What am I overlooking, please?
Sincerely,
Gene Wirchenko
I have a problem with a batch file that I am developing. What I
want it to do is print out the drive that the batch file is on. I am
not getting that.
I have a copy in c:\tmp with that the current directory and
another copy in g:\tmp on my XP system. (I have also tried this on my
Win7 system where the drives are c: and i:.) The copies are
identical; I have made very sure of that.
If I give the command
getdrive
it correctly prints the current drive.
If I give the command
c:getdrive
with c: the current drive, it does not pick up the drive letter and so
defaults to the current drive. This means that I get the correct
answer by accident.
If I give the command
g:getdrive
(or i:getdrive) with c: the current drive, it does not pick up the
drive letter and so defaults to the current drive. This means that I
get the wrong answer.
I know exactly where the error occurs, but I have no idea why it
happens. I get the same result in both XP and Win7. When a drive
letter is specified, execution gets into the else. Output is:
@ else
namecopy=_____ (where the underscores are the name of
the batch file (%0) as specified in the command line)
assigning _ (where underscore is the first letter of %0)
Drive is apparently .
Note that the drive letter is not there!
In previous tries, I even had output of
assigning g
Drive is apparently C.
from
g:getdrive
This might have been an artefact from before I added setlocal.
Here is the batch file.
***** Start of Batch File *****
@echo off
setlocal
echo GetDrive Version 1.0.0 of 2012-10-31 by Gene Wirchenko
set namecopy=%0
echo namecopy=%namecopy%
if not "%namecopy:~1,1%"==":" (
set driveletter=%cd:~0,1%
echo set @ if
) else (
echo @ else
echo namecopy=%namecopy%
echo assigning %namecopy:~0,1%
set drive=%namecopy:~0,1%
echo Drive is apparently %drive%.
set driveletter=%cd:~0,1%
if /i "%drive%" geq "A" if /i "%drive%" leq "Z" set
driveletter=%drive%
)
echo Computed drive letter is %driveletter%.
endlocal
***** End of Batch File *****
Note that the "if /i ..." line may wrap to two lines on your
display; it does on mine.
What am I overlooking, please?
Sincerely,
Gene Wirchenko