Get the current directory of the launched bat issue - batch-file

I have a bat file mybat.bat in a folder Myfolder
I want to set the current directory to Myfolder path
If I try
#echo on
cd\%0
pause
I get as current directory
Myfolder\mybat.bat
but I need only
Myfolder
How should I edit my bat to fix the problem?

You want either
cd /d "%~dp0"
or
pushd "%~dp0"

This is another method:
cd Myfolder\mybat.bat\..

Related

Change logfile path with batch file

From the documentation of Unity you can change the logfile path by adding
-logFile <pathname>
I tried doing that with a batch file:
setlocal
cd /d %~dp0
Game.exe -batchmode -logFile<D:\Test\>
This just starts the Windows Command Prompt and nothing happens. Also i don't know how to change the <pathname> of the -logfile so that it uses the path of the batch file.
Thanks to all your comments, i solved it today:
setlocal
cd /d "%~dp0"
Game.exe -logFile ./Logs/output_log.txt
this is working fine for me.

Batch File - going back two steps in a directory path

I am creating a batch file i am on a path
C:\Validation\docs\chm
I want to move back to the
C:\Validation part
which is in %DialogPath%
This was entered by the user but when i write
CD /D %DialogPath%
An error occurs that tells this path does not exists
The direct answer to your question would be
cd ..\..
But cd /D C:\Validation also works.
The problem is more likely with the variable than then command.
Until you give more details as to the script in question, we can only guess to what the problem may be.
However, since you are changing the current directory only for a limited time you should be using the pushd and popd commands.
Example: (Run this .bat script to see how pushd and popd work!)
:: Hide Commands
#echo off
:: Display Current Working Directory
echo Current Directory = %CD%
:: Create folders for demonstration purposes only
rd /Q "%Temp%\Test" 2>nul & mkdir "%Temp%\Test" & mkdir "%Temp%\Test\Subfolder"
:: Change the Working Directory
pushd "%Temp%"
:: Display Current Working Directory
echo Current Directory = %CD%
pushd "%Temp%\Test\Subfolder"
:: Display Current Working Directory
echo Current Directory = %CD%
:: Revert back to the previous Working Directory
popd
:: Display Current Working Directory
echo Current Directory = %CD%
:: Revert back to the previous Working Directory
popd
:: Display Current Working Directory
echo Current Directory = %CD%
pause
For help type pushd /? or popd /? into the command prompt.
You can move up one path with cd ... If you do that twice, you will land in the C:\Validation directory.
In your example it would look like this:
C:\Validation\docs\chm> cd ..
C:\Validation\docs> cd ..
C:\Validation>

create a folder name variable dos command

i want to create a batch file
#echo off
set /p name="Type folder name(s):
md %name%
cd p:\%name%
all lines work, but i cannot change the directiory to cd p:\%name% when i run the script
thank you.
You need cd /d to change to a directory on another drive:
cd /d p:\%name%

How to return to current working directory after cdin'g out in a batch file?

I have a batch file and I want to do the following...
cd C:\some\directory
some_command.cmd
# return to the directory I started in here
python some_script.py
As you can see in the commented line, I need some way to return to the directory I started in. How can I do this?
You can try pushd and popd commands
see http://en.wikipedia.org/wiki/Pushd_and_popd
set OLDDIR=%CD% rem current directory
rem your script
chdir /d %OLDDIR% rem go to OLDDIR

How can I change the root path in a .bat file?

In a .bat file, How can I change the root path to be c:\temp\code
cd /d c:\temp\code
I'd suggest pushd over cd in this case. That way you can restore the previous directory with popd at the end. Unless a batch file should actually change the path even after it has been run, I'd always restore it at the end of the batch:
#echo off
rem change current directory
pushd C:\Temp\Code
rem ...
rem something your batch needs to do
rem ...
rem restore old working directory
popd
Sometimes, doing cd c:\temp\code only doesn't work if you're in another drive. This way works all the time:
c:
cd c:\temp\code

Resources