Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the current directory of a batch file, and then use it for the path?

I have a batch file that I intend to distribute to our customers to run a software task.

We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.

Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.

So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.

But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.

like image 575
Ryan Barber Avatar asked Apr 27 '13 17:04

Ryan Barber


People also ask

How do I find the location of a batch file?

You can get the name of the batch script itself as typed by the user with %0 (e.g. scripts\mybatch. bat ). Parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the batch script (e.g. W:\scripts\ ) and %~f0 will return the full pathname (e.g. W:\scripts\mybatch. cmd ).

What is the current directory in a batch file?

It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3 , then run the batch, the current directory will be c:\dir3 .

How do I change the working directory in a batch file?

The general syntax for this command is CD /D [DRIVER:][PATH] or CD [YOUR_PATH] . Here, /D is used to change the current drive. If you want to set a directory in the same drive as the current directory, then you can use the format CD [YOUR_PATH] .

How do I get the current directory in DOS?

MS-DOS and Windows command line current directory To list the files in the current directory use the dir command, and if you want to change the current directory, use the cd command. You can use the chdir command by itself to print the current directory in MS-DOS and the Windows command line.


1 Answers

There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:

.\mydocuments\folder\mybat.bat .\mydocuments\folder\subfolder\file.txt 

And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:

@Echo OFF REM Do anything with ".\Subfolder\File1.txt" PUSHD ".\Subfolder" Type "File1.txt" Pause&Exit 

Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:

@Echo OFF Echo Launch dir: "%~dp0" Echo Current dir: "%CD%" Pause&Exit 
like image 90
ElektroStudios Avatar answered Oct 02 '22 18:10

ElektroStudios