How can I change the root path in a .bat file? - batch-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

Related

Batch file to read the root directory it is in

Is there a batch command that can read a root directory without the entire path?
I want a batch file to tell me if its in D:\ or E:\.
I tried to use:
set mypath=%cd%
#echo %mypath%
Pause
But it just says the exact place it is in rather than just the root.
Here's a few options for you which provide the root directory of the scripts current directory:
Using PushD/Popd
PushD\&Call Set "RootDir=%%CD%%"&PopD
Echo(%RootDir%
Pause
Using a For loop
For %%A In (%CD%) Do Set "RootDir=%%~dA\"
Echo(%RootDir%
Pause
Using variable expansion
Set "RootDir=%CD:~,3%"
Echo(%RootDir%
Pause
Edit
After reading your question again, I decided to add a fourth example. This one unlike the other three provides the root directory of the batch files location.
Set "RootDir=%~d0\"
Echo(%RootDir%
Pause
the directory where the batch file is located could be different from the current directory cmd.exe operates in.
TO get the batch file root path use:
for %%a in ("%0") do echo %%~da
To ger the current directory use
echo "%cd:~0,3%"
And let us not forget the &REM trick.
#ECHO OFF
set "root=%cd:\="&rem %
echo %root%

Get the current directory of the launched bat issue

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\..

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>

Running the wrong batch file after copying another to a folder

The issue is that it is calling the original beamthemup2.bat file, not the one copied.
#ECHO OFF
for /d %%X in (*) do (
copy "beamthemup2.bat" "%%X"
#echo "%%X\beamthemup2.bat"
pause
call "%%X\beamthemup2.bat"
)
UPDATE
This is the second bat file. It seems it is copying the and running the correct batch file. Here is the problem though. When running the above batch file first %cd% is returning the path of the first batch file, however if I run the second one by itself in the folder that I wanted it copied to, %cd% returns the correct folder.
#ECHO OFF
for /r %%X in (*) do (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%cd%" "%%X"
#ECHO %cd%
pause
)
Your current working directory is still the directory you started the first batch file from. Running the second batch file doesn't change the working directory.
Try adding cd %~dp0 to your second batch file (after #ECHO OFF). That will cd to the directory the batch file is in.

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

Resources