1.

Solve : how to find current directory from which batch file is executed?

Answer»

Hi
I am new to IT.
I have a batch file named myfile.bat placed at D:\myfolder1\

I WANT to know batch script which can identify the directory from which this batch file is currently executing even if the file has been moved to a new location.

is it possible? if yes then how? The parameter %~dp0 contains the batch script's DRIVE and path


Quote from: khurram Ilyas on December 02, 2011, 10:24:05 PM

Hi
I am new to IT.
I have a batch file named myfile.bat placed at D:\myfolder1\

I want to know batch script which can identify the directory from which this batch file is currently executing even if the file has been moved to a new location.

is it possible? if yes then how?

The OS will first  look at the CURRENT directory for myfile.bat
If myfile.bat is not in the current directory, the OS will use the official search path to fo find myfile.bat


C:\Windows\system32>path /?
Displays or sets a search path for executable files.

PATH [[drive:]path[;...][;%PATH%]
PATH ;

Type PATH without parameters to display the current path.
Including %PATH% in the new path setting causes the old path to be
appended to the new setting.

C:\Windows\system32>


C:\>cd  \

C:\>dir /s myfile.bat 
rem the above code will find all locations of myfile.bat on your computerFred, that doesn't actually answer the question which was asked: how can a batch file know its own directory? Quote from: Salmon Trout on December 03, 2011, 05:58:42 AM
Fred, that doesn't actually answer the question which was asked: how can a batch file know its own directory?


What happens when myfile.bat is stored in more than one directory?  Quote from: Fred6677 on December 03, 2011, 06:06:08 AM

What happens when myfile.bat is stored in more than one directory?

Each one, when run, will know which directory it is in.
Oh. Er, Hi Bill!

Now we've got that out of the way, maybe I'd better address the confusion that Bill has introduced.

The OP asked how a batch file may know its own folder. He asked:

Quote
I have a batch file named myfile.bat placed at D:\myfolder1\

I want to know batch script which can identify the directory from which this batch file is currently executing even if the file has been moved to a new location.

This is a typical question asked when the batch file is going to be placed on a medium (CD-ROM, pen drive etc) where the drive letter is not known in advance. Often the batch file is required to copy files from its own folder.

A batch file has access to the %0 parameter which can be used with the standard parameter modifiers so the batch file can get information about itself, its location, etc.

Code: [Select]echo off
echo This batch file's full path and name %~dpnx0
echo This batch file's drive letter is    %~d0
echo This batch file's folder is          %~dp0
echo This batch file's filename is        %~n0
echo This batch file's extension is       %~x0
echo This batch file's file date is       %~t0
echo This batch file's size in bytes is   %~z0
echo.
pause


Code: [Select]S:\Test\batch>dir
 Volume in drive S is USB-S
 Volume Serial Number is 2C51-AA7F

 Directory of S:\Test\batch

03/12/2011  15:34    <DIR>          .
03/12/2011  15:34    <DIR>          ..
03/12/2011  15:34               371 ShowData.bat
               1 File(s)            371 bytes
               2 Dir(s)  168,383,356,928 bytes free

S:\Test\batch>ShowData.bat
This batch file's full path and name S:\Test\batch\ShowData.bat
This batch file's drive letter is    S:
This batch file's folder is          S:\Test\batch\
This batch file's filename is        ShowData
This batch file's extension is       .bat
This batch file's file date is       03/12/2011 15:34
This batch file's size in bytes is   371

Press any key to continue . . .



and you run the batch file the result will be something like this


Quote from: Salmon Trout on December 03, 2011, 05:23:40 AM
The parameter %~dp0 contains the batch script's drive and path
Thanks Salmon Trout!
You provide the best & exact solution of my PROBLEM in only one line.
Thanks a lot


Discussion

No Comment Found