Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: how to find current directory from which batch file is executed  (Read 24852 times)

0 Members and 1 Guest are viewing this topic.

khurram Ilyas

    Topic Starter


    Greenhorn
    • Certifications: List
    • Experience: Beginner
    • OS: Windows 7
    how to find current directory from which batch file is executed
    « 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?

    Salmon Trout

    • Guest
    Re: how to find current directory from which batch file is executed
    « Reply #1 on: December 03, 2011, 05:23:40 AM »
    The parameter %~dp0 contains the batch script's drive and path



    Fred6677



      Rookie

      Thanked: 1
      • Experience: Beginner
      • OS: Unknown
      Re: how to find current directory from which batch file is executed
      « Reply #2 on: December 03, 2011, 05:48:25 AM »
      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 computer

      Salmon Trout

      • Guest
      Re: how to find current directory from which batch file is executed
      « Reply #3 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?

      Fred6677



        Rookie

        Thanked: 1
        • Experience: Beginner
        • OS: Unknown
        Re: how to find current directory from which batch file is executed
        « Reply #4 on: December 03, 2011, 06:06:08 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? 

        Salmon Trout

        • Guest
        Re: how to find current directory from which batch file is executed
        « Reply #5 on: December 03, 2011, 06:10:35 AM »

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

        Each one, when run, will know which directory it is in.

        Salmon Trout

        • Guest
        Re: how to find current directory from which batch file is executed
        « Reply #6 on: December 03, 2011, 08:39:13 AM »
        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



        khurram Ilyas

          Topic Starter


          Greenhorn
          • Certifications: List
          • Experience: Beginner
          • OS: Windows 7
          Re: how to find current directory from which batch file is executed
          « Reply #7 on: December 04, 2011, 12:34:56 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