I have a batch file that outputs a path. How can I use the batch file in Windows cmd to change the current directory?
What have I tried:
F:\>cd my-path.bat
The system cannot find the path specified.
F:\>cd (my-path.bat)
The system cannot find the path specified.
F:\>my-path.bat | cd
F:\
The process tried to write to a nonexistent pipe.my-path.bat content:
@ECHO OFF
@ECHO F:\MyPath\ 3 4 Answers
for /f "delims=" %d in ('my-path.bat') do cd /d "%~d"Note: In a batch file you have to replace % by %%
4For different drivers you need /d in your cd command
If your folder/path/name have any space/special character, use “double-quotes” too
@echo off
echo\"F:\path to your folder"
:: or add/replace echo\"F:\path to your folder" to cd /d ::
cd /d "F:\path\to\folder"An alternative would be to get the output in a loop, where the bat would not be changed, just the command line...
for /f tokens^=* %i in ('^<con: "my-path.bat"')do cd/d "%~i"- Remembering that loop variables
%iin bat are%%i:
for /f tokens^=* %%i in ('^<con: "my-path.bat"')do cd/d "%%~i"- You can use arguments (
%*) to variable run your command:
@echo off
<con: call %* "F:\path to your folder" 2>nul || echo\"F:\path to your folder"The 2>nul suppression any possible error, and if the last command do not return 0, one simple echo will take action...
Using argument in your bat:
my-path.bat cd /dSome further reading:
[√] 'Cd /d'
[√] 'For'
[√] 'For /F'
[√] Conditional Execution
||and&&[√] How can I pass arguments to a batch file
[√] Understanding start, '2>nul', 'cmd', and other symbols in a batch file
Why not simply change your Batch file to:
@echo off
cd F:\MyPath\ 2 Building on LeRouteur's answer, the following might be a useful compromise:
@echo off
if "%~1" == "." cd /d F:\mypath && goto :eof
echo F:\mypathRunning just my-path will echo the path, as it currently does, but running my-path . (i.e. with a single full-stop as a parameter) will change to that path.
If you dislike having the path entered twice, this can be avoided by using an environment variable, but to do so "cleanly" (without polluting your existing environment) needs a little thought:
@echo off
setlocal
set "MYPATH=F:\mypath"
if "%~1" == "." ( endlocal && cd /d "%MYPATH%" goto :eof
)
echo "%MYPATH%"The use of setlocal means any changes to the environment (e.g. MYPATH) will only be made locally: running endlocal or reaching the end of the batch-file will revert to the original environment (see setlocal /? and endlocal /?).
Using setlocal is very common in batch-files so that any environment variables they use/change don't "pollute" the main environment. The problem is that the current directory is part of what is localized: if you run cd while setlocal is in effect, you will return to your starting directory when the script exits.
One solution is to put the endlocal and cd commands on the same line. The way batch-files are parsed means that the evaluation of MYPATH happens while the environment is still "local" (so it still has the right value), but the execution of the cd command (using the value that was obtained in the first pass) doesn't happen until after endlocal has executed, meaning the directory will be changed when it returns to the command-line.
The way I define MYPATH, and use its value (enclosed in double-quotes) allows for the path to contain spaces. Perhaps not necessary here, but a good habit to get into when writing batch-files.
As a final option, you could replace cd /d with pushd. The latter "pushes" the current directory onto a stack before changing to the new one (see pushd /?). When you are finished doing whatever you need to do in MYPATH you can then run popd to return to whichever directory you originally ran my-path . from.