Batch File Question


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
 
Ad

Advertisements

V

VanguardLH

Gene Wirchenko said:
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
%1 through %9 are the replaceable variables in the command line in the
shell you are currently within. %* will output them all. If there are
more than 9 parsed strings used a variables then you have to use SHIFT
to shift leftward the list of variables.

%0 will give you the string used to execute the command. So having:

echo Command was: %0

will show you what you entered in the command interpreter for the
program to run. If you don't specify a path, like you relied upon the
PATH environment variable or AppPath registry entries to find the
executable, then you won't see the path in the %0 value, either. If I
run testme.bat by just entering "testme" and it runs because it was
found via PATH or AppPath then %0 contains just "testme". If I run
"c:\batch\testme.bat" then %0 contains "c:\batch\testme.bat".

%0 contains whatever you gave to the command interpreter (command.com or
cmd.com). %CD% expands to a string listing the current directory.
Well, if your .bat file is found elsewhere via PATH or AppPath then %CD%
is of no value in finding where was the .bat file you ran.

So have you tried %~dp0 as the replaceable variable to show the path for
the executable (.bat file) from which that variable is defined? Try:

echo This batch file is located at: %~dp0

You are using a modifier on the %0 replaceable variable. To see the
list of modifiers usable with replaceable variables, run:

for /?

and look under the "In addition, substitution of FOR variable references
has been enhanced" section.

Thanks for the question. I remember doing this, didn't have it used in
my current selection of .bat files, but remembered it was doable and had
to reeducate myself.
 
G

Gene Wirchenko

[snipped remainder of original post]

[snipped response's infodump of stuff that it is reasonable to assume
I already know given the contents of my batch file]
So have you tried %~dp0 as the replaceable variable to show the path for
the executable (.bat file) from which that variable is defined? Try:
Now, this bit is very useful.
echo This batch file is located at: %~dp0

You are using a modifier on the %0 replaceable variable. To see the
list of modifiers usable with replaceable variables, run:
No, I am not. I assign to namecopy and work with it.
for /?

and look under the "In addition, substitution of FOR variable references
has been enhanced" section.
That was useful.
Thanks for the question. I remember doing this, didn't have it used in
my current selection of .bat files, but remembered it was doable and had
to reeducate myself.
Sincerely,

Gene Wirchenko
 
G

Gene Wirchenko

set namecopy=%0 ....
echo namecopy=%namecopy%
echo assigning %namecopy:~0,1%
set drive=%namecopy:~0,1%
echo Drive is apparently %drive%.
namecopy is assigned correctly. The assigning echo displays the
first character of namecopy. The assignment gets nothing as shown by
the next echo.

Sincerely,

Gene Wirchenko
 
P

Philip Herlihy

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
This may not help you, but the Prompt command can be set to display the
current drive:
PROMPT $N

Typing:
PROMPT
... resets the prompt to normal

You can add a space thus:
PROMPT $N$S

I've tried to read this back (using the FOR /F construction) but the
prompt doesn't appear either on stdout or stderr (to use Unix terms).

Of course, it's only now I think to Google it, and it turns out to be
easy.

Try this:

ECHO %CD:~0,1%
 
Ad

Advertisements

C

Char Jackson

This may not help you, but the Prompt command can be set to display the
current drive:
PROMPT $N

Typing:
PROMPT
.. resets the prompt to normal

You can add a space thus:
PROMPT $N$S

I've tried to read this back (using the FOR /F construction) but the
prompt doesn't appear either on stdout or stderr (to use Unix terms).

Of course, it's only now I think to Google it, and it turns out to be
easy.

Try this:

ECHO %CD:~0,1%
I just spent a few seconds playing with this and found that it's
pretty cool, although I don't remember ever needing this bit of
functionality.

echo %CD% prints the whole drive:path, while the second half of
"%CD:~0,1% is used to limit how many characters will be echoed, with
zero meaning start from the beginning, and 1 meaning print 1
character. Probably obvious to everyone else who cared, but it wasn't
obvious to me until I messed around with it.
 

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

Top