Command line : how to get the .cmd file name I'm running

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I wrote a.cmd, which calls b.cmd. Both a.cmd and b.cmd are located in the
same directory (in fact, in the same UNC share). I don't want to tell WHERE
to find b.cmd, because if I change the server or share name, my script won't
work any more. How can I do this ?
I tried to use %CD%, but it seems that if the startup directory is a UNC
share, it is unusable.
Maybe there is a way to get in a variable the complet filename of the .cmd I
am running : this way, I could run a path command to add the server/share
part to the current path. Besides, I could use the filename part to generate
a log file, such as %mybatchfilename%.log.
Can anyone help me ?
 
guru perplexe wrote:
| Hi,
| I wrote a.cmd, which calls b.cmd. Both a.cmd and b.cmd are located in the
| same directory (in fact, in the same UNC share). I don't want to tell
| WHERE to find b.cmd, because if I change the server or share name, my
| script won't work any more. How can I do this ?
| I tried to use %CD%, but it seems that if the startup directory is a UNC
| share, it is unusable.
| Maybe there is a way to get in a variable the complet filename of the
| .cmd I am running : this way, I could run a path command to add the
| server/share part to the current path. Besides, I could use the filename
| part to generate a log file, such as %mybatchfilename%.log.
| Can anyone help me ?

I'm new to batch files to some degree. So I can really only ask questions.
But the first thing that struck me was that both a.cmd and b.cmd are in the
same directory .. you don't need to tell a.cmd where to find b.cmd then do
you? Could you write in routines in a.cmd that simply calls b.cmd ??

Try Catch
 
Hi,
No, I can't. I think Windows searches the current directory and the path. In
my case, the current directory is not correctly set, because it is in fact a
UNC share.
 
guru said:
Hi,
I wrote a.cmd, which calls b.cmd. Both a.cmd and b.cmd are located in the
same directory (in fact, in the same UNC share). I don't want to tell WHERE
to find b.cmd, because if I change the server or share name, my script won't
work any more. How can I do this ?
I tried to use %CD%, but it seems that if the startup directory is a UNC
share, it is unusable.
Maybe there is a way to get in a variable the complet filename of the .cmd I
am running : this way, I could run a path command to add the server/share
part to the current path. Besides, I could use the filename part to generate
a log file, such as %mybatchfilename%.log.
Can anyone help me ?
Hi,

In a.cmd, try this:

%0\..\b.cmd

(in a batch file, %0 is the path to the batch file including the batch
file name)


If that doesn't work, use pushd/popd. Try the following batch file
(change \\server\share\folder to your actual UNC path):

--------------------8<----------------------
@echo off
echo.
pushd "\\server\share\folder"
echo.
cd
echo.
pause
net.exe use
pause
popd
net.exe use
pause
'--------------------8<----------------------
 
guru said:
Hi,
I wrote a.cmd, which calls b.cmd. Both a.cmd and b.cmd are located in the
same directory (in fact, in the same UNC share). I don't want to tell WHERE
to find b.cmd, because if I change the server or share name, my script won't
work any more. How can I do this ?
I tried to use %CD%, but it seems that if the startup directory is a UNC
share, it is unusable.
Maybe there is a way to get in a variable the complet filename of the .cmd I
am running : this way, I could run a path command to add the server/share
part to the current path. Besides, I could use the filename part to generate
a log file, such as %mybatchfilename%.log.
Can anyone help me ?

- - - - - - - - begin screen capture WinXP Pro SP2 - - - - - - - -
C:\cmd>type "\\algonquin\share$\Phil R\test.cmd"
@echo off
echo/This is a test.
echo/
echo/The name of this cmd file is:
echo/%~f0
goto :EOF

C:\cmd>"\\algonquin\share$\Phil R\test"
This is a test.

The name of this cmd file is:
\\algonquin\share$\Phil R\test

C:\cmd>net use
New connections will not be remembered.


Status Local Remote Network

-------------------------------------------------------------------------------
OK J: \\algonquin\APPO-FAS$ Microsoft Windows Network
OK K: \\algonquin\probyn$ Microsoft Windows Network
OK S: \\algonquin\share$ Microsoft Windows Network
OK LPT1 \\algonquin\mephisto Microsoft Windows Network
The command completed successfully.
- - - - - - - - end screen capture WinXP Pro SP2 - - - - - - - -
 
Back
Top